function deletePlayer(theWrapper, thePlaceholder, thePlayerId) { 
        swfobject.removeSWF(thePlayerId);
        var tmp=document.getElementById(theWrapper);
        if (tmp) { tmp.innerHTML = "<div id=" + thePlaceholder + "></div>"; }
}

function createPlayer(thePlaceholder, thePlayerId, theFile, theImage, theEmbed, theLink, argAuto, argUacct, width, height) {
		var embedCode = '<embed type=application/x-shockwave-flash src=http://www.media.chrysler.com/flash/player.swf pluginspage=http://www.macromedia.com/go/getflashplayer flashvars=file='+theFile+'&image='+theImage+' height=290 width=470 ></embed>';
        var linkCode = 'http://www.media.chrysler.com/flash/player.swf?file='+theFile+'&autoStart=false&image='+theImage+'&height=290&width=470';
		
		 jwplayer("placeholder").setup({
        'flashplayer': "flash/player.swf",
        'file': theFile,
        'image': theImage,
        'allowfullscreen':"true", 
        'allowscriptaccess':"always",
		'wmode': "transparent",
		'id':thePlayerId,  
        'name':thePlayerId,
        'controlbar':"bottom",
        'height': height,
        'width': width,
        'plugins': 'sharing, gapro',
    	'sharing.link':linkCode,
		'sharing.code':embedCode,
		'gapro.accountid': argUacct,
    	'gapro.trackstarts':'true',
		'gapro.trackpercentage':'true',
		'gapro.tracktime':'true',
		'gapro.trackcompletes':'true',
		'gapro.trackdetails':'true',
		'gapro.idstring':theFile
    	});
		
}

function changeLayout(argLink){
	var videoHeadArray = ["vidLink1Head","vidLink2Head","vidLink3Head"];
	var videoSubArray = ["vidLink1Sub","vidLink2Sub","vidLink3Sub"];
	var videoDownArray = ["vidLink1Down","vidLink2Down","vidLink3Down"];
	for (i=0;i<=2;i++){
		var argHeadTemp = argLink+"Head";
		
		if (argHeadTemp != videoHeadArray[i]){
			hideDiv(videoHeadArray[i]);
		} 
		else {
			showDiv(videoHeadArray[i]);
		}
	}
	
	for (y=0;y<=2;y++){
		var argSubTemp  = argLink+"Sub";
		
		if (argSubTemp == videoSubArray[y]){
			hideDiv(videoSubArray[y]);
		} 
		else {
			showDiv(videoSubArray[y]);
		}
	}
	
	for (z=0;z<=2;z++){
		var argDownTemp  = argLink+"Down";
		
		if (argDownTemp != videoDownArray[z]){
			hideDiv(videoDownArray[z]);
		} 
		else {
			showDiv(videoDownArray[z]);
		}
	}			
}

// New Functions to implement new ajax functionality.

function initPlayer(videoAppURL, attachedChannels,argUacct) { 
	deletePlayer('featuredvideo', 'placeholder1', 'player1'); 
	retrieveChannelInfo(attachedChannels,videoAppURL,argUacct);
	//changeLayout(argLink);
}


function ajaxSetup() {
	// Global rror handling for .get ajax calls
	// To-Do:  Replace spinning wheels and loading text with explanation if
	// loading fails.
	$.ajaxSetup({
		error:function(x,e){
			if(x.status==0){
				//alert('You are offline!!\n Please Check Your Network.');
			}
			else if(x.status==404){
				//alert('Requested URL not found.');
			}
			else if(x.status==500){
				//alert('Internel Server Error.');
			}
			else if(e=='parsererror'){
				//alert('Error.\nParsing XML Request failed.');
			}
			else if(e=='timeout'){
				//alert('Request Time out.');
			}
			else {
				//alert('Unknow Error.\n'+x.responseText);
			}
		}
	});
}

			
function retrieveChannelInfo(channels,videoAppURL,uacct) {
		$.get('video',{'channels':channels}, function(data) {
			updateRightHandVideo(data,uacct);
		});
}

function updateRightHandVideo(data,uacct) {	
		// Let's parse the first channel we find and the first video for display.
		var videoAppURL = 'http://www.media.chrysler.com';
		var channel = $(data).find("channel:first");
		var channelId = channel.find("id:first").text();
		var channelName = channel.find("name:first").text();
		var vidOne = channel.find("video:first");
		var lowRez = vidOne.find("lowres").text();
		var image = vidOne.find("image").text();
		var theEmbed = vidOne.find("embed").text();
		var theLink = videoAppURL + '/mediacenter.do?method=viewChannel&cid=' + channelId + '&mid=255';
		var download = vidOne.find("download").text();
		if( download == null)
			download = vidOne.find("file").text();
		
		createPlayer('placeholder', 'player', lowRez, image, theEmbed, theLink, 'false', uacct, 270, 170);
	
		// For newsroom.do pages.
		$("#rightvidinfo a").attr("href",theLink);
		$("#rightvidinfo a").text(channelName);
		$("#rightviddownload a").attr("href",download);
		$("#rightviddownload a").text("Download");
	
}

function getAdditionalChannels(channels,videoAppURL) {
		$.get('video',{'channels':channels}, function(data) {
			var otherChannels = $(data).find("root");
			$(".spinner").remove();
			for( i=1;i<otherChannels.size();i++) {
				var name = $(otherChannels[i]).find("name").text();
				$("#additionalChannels"+(i+1) + " a").text(name);
				var cid =  $(otherChannels[i]).find("id:first").text();
				$("#additionalChannels"+(i+1) + " a").attr("href",videoAppURL + "/mediacenter.do?method=viewChannel&cid="+ cid + "&mid=255") ;
			}
			
		});
}

function fetchLatestVideos() {
	
	// This fetches that latest videos for the mediacenter.do 
	// Query string should be videos=latest to trigger proper methods servlet side.
	$.get('video',{'videos':'latest'}, function(data) {
		var videos = $(data).find("video");
		var videoList = '';
		
		// Lets create a string of all containted video id's for proper linkage
		for(i=0;i<videos.size();i++) {
			videoList = videoList + $(videos[i]).find("id").text() + ",";
		}
		
		// Now insert video images and links into DOM.
		for( i=0;i<videos.size();i++) {
			var image = $(videos[i]).find("videoImage").text();
			var videoId = $(videos[i]).find("id").text();
			var videoNum = $(videos[i]).find("videoNum").text();
			var videoTitle = $(videos[i]).find("videoTitle").text();
			var linkTo = "?method=viewVideo&vid=" + videoId + "&videoNo=" + videoNum + "&mid=254&videoGalleryTOList=" + videoList;
			$("#gallerybox" + videoNum).replaceWith("<div class='gallerybox'><div class='galleryimg'><a href='" + linkTo + "'> <img src='" + image + "' width='100' border=''</a></div> <div class='galleryright'><h3><a href='" + linkTo + "'>" + videoTitle + "</a></h3></div></div>");
		}
	});
}

