//global variables declaration
var imgPathPrefix='/wcsstore/ConsumerDirect/';
//alert(imgPathPrefix); 
var space = ' ';
var currCatEntId;
var nextFVId;
var prevFVId;
var currentFetVidName;

//remove the childe elements from a DOM Parent element
function removeChildrenFromNode(node)
{
   if(node == undefined ||
        node == null)
   {
      return;
   }

   var len = node.childNodes.length;

	while (node.hasChildNodes())
	{
	  
	 node.removeChild(node.firstChild);
	}
 
}

//function for play video
function playVideo(index, i)
{
    //alert("playVideo"); 
    document.getElementById('video_name').innerHTML=prodArray[index][i][2];
    document.getElementById('video_shortdsc').innerHTML=prodArray[index][i][3];
    document.getElementById('video_longdsc').innerHTML=prodArray[index][i][6];
    document.getElementById('video_length').innerHTML=prodArray[index][i][4]+' | '+prodArray[index][i][5];
    
    setClipRefreshLoadAndPlay(prodArray[index][i][11]); 
    
}

//function for play video if we pass the productId in the URL of multimedia landing page
function playVideoForProductId()
{
	//alert("playVideoForProductId"); 
	var par = document.getElementById('playlist');
	removeChildrenFromNode(par);
	var aele = document.createElement('a');
	//var aattr = document.createAttribute('href');
	//aele.setAttribute('href','#');
	aele.setAttribute('id','playVidforProductLinkId');
	aele.setAttribute("onclick", "playVideoForProd("+"this"+")");
	aele.setAttribute('id','vl_ex'+'_'+i); 										
    aele.setAttribute('href', prodVidArray[11]);
 	aele.innerHTML='<strong>'+prodVidArray[12]+'<br/>'+prodVidArray[2]+'</strong>'+'<br/><br/>'+prodVidArray[4];
	par.appendChild(aele);
    playVideoForProd(aele);
	setTimeout('fnForcePlay()',400);
}

//function for setup player if we pass the productId in the URL of multimedia landing page
function playVideoForProd(aele)
{	
	//alert("playVideoForProductId"); 
	document.getElementById('video_name').innerHTML=prodVidArray[12];
    document.getElementById('video_shortdsc').innerHTML=prodVidArray[3];
    document.getElementById('video_longdsc').innerHTML=prodVidArray[6];
    document.getElementById('video_length').innerHTML=prodVidArray[4]+' | '+prodVidArray[5];
	$(function() {
	// setup player 
		$f(0).playlist("#playlist").play(0);	
	});
	
	var clip = prodVidArray[11];
	setClipRefreshLoadAndPlay(clip);

}

//function for auto playing the particular video which we passed as the productId in the URL of multimedia landing page
function fnForcePlay()
{
	 //alert("fnForcePlay"); 
	 $(function()
	  {
		// setup player 
		$f(0).playlist("#playlist").play(0);	
	});
}

//function for changing the playlist dynamically when the user clicks particular sub category 
//when we comes to video section from the different section like webcams,panaromic view 
function  changeProdList(subCatId,index)
{          
            
			//alert("changeProdList"); 
			if(prodArray[index]!= undefined && prodArray[index]!= null  && prodArray[index]!='')
			{
				var par = document.getElementById('playlist');
				removeChildrenFromNode(par);
				var catgrpId = subCatId;
	
 								for(var i=0;i<prodArray[index].length;i++)
 								{							 			
	 								if(prodArray[index][i]!= undefined && prodArray[index][i]!= null  && prodArray[index][i]!='')
			   						{					
											if(prodArray[index][i][0] ==  catgrpId)
		 									{		 									     
		 										var aele = document.createElement('a');
		 										//var aattr = document.createAttribute('href');
		 									    aele.setAttribute("id",'vl_'+index+'_'+i);
		                                        //aele.setAttribute('href', imgPathPrefix+videoPath+prodArray[index][i][11]);
		 									 	var aattr = document.createAttribute('href');
	 											aattr.nodeValue = 'javascript:playVideo(' + index + ',' + i + ');'; 

												aele.setAttributeNode(aattr);
		 									 	var brele = document.createElement('br');
		 										var brele1 = document.createElement('br');

		 									    if(prodArray[index][i]!= undefined && prodArray[index][i]!= null  && prodArray[index][i]!='')
			   									{
		 									   		aele.innerHTML='<strong>'+prodArray[index][i][12]+'<br/>'+prodArray[index][i][2]+'</strong>'+'<br/><br/>'+prodArray[index][i][4];
		 										} 

		 										par.appendChild(aele);
		 										 
		 										 
		 										 
										    	$f("player").playlist("#playlist");
												  		 
												 
																		      
		 									}	 									
		 								}
 								}
 								
 								// uncomment this section to play first video when playlist category is changed 
 								setClipRefreshLoadAndPlay(prodArray[index][0][11]);		
								document.getElementById('video_name').innerHTML=prodArray[index][0][2];
								document.getElementById('video_shortdsc').innerHTML=prodArray[index][0][3];
								document.getElementById('video_longdsc').innerHTML=prodArray[index][0][6];
								document.getElementById('video_length').innerHTML=prodArray[index][0][4]+' | '+prodArray[index][index1][5];
								
 			}
 		
}

//function for playing featured videos
function playFetVideo(i)
{
    //alert("playFetVideo"); 
    document.getElementById('video_name').innerHTML=fetVideoArray[i][2];
    document.getElementById('video_shortdsc').innerHTML=fetVideoArray[i][3];
    document.getElementById('video_longdsc').innerHTML=fetVideoArray[i][6];
    document.getElementById('video_length').innerHTML=fetVideoArray[i][4]+' | '+fetVideoArray[i][5];
    setClipRefreshLoadAndPlay(fetVideoArray[i][11]); 
    //ele.href= imgPathPrefix+videoPath+fetVideoArray[i][11]; 
}

//Function for changing  featured video playlist dynamically
function  changeFetVidList(catentid,index)
{
	//alert("changeFetVidList"); 
	currCatEntId = catentid;
	var par = document.getElementById('playlist1');
	removeChildrenFromNode(par);
 
								for(var i=0;i<fetVideoArray.length;i++)
 								{				
 									if(fetVideoArray[i]!= undefined && fetVideoArray[i]!= null  && fetVideoArray[i]!='')
	   								{
	   								    
	 									if(fetVideoArray[i][1] ==  catentid)
	 									{			 									 																		     
	 										if(index== 'n')
	 										{
	 											if(fetVideoArray.length-1>i && fetVideoArray[parseInt(i)+1]!=undefined)
	 											{
	 												index = parseInt(i)+1;	 													
	 											}
	 											else
	 											{
	 											    index = parseInt(0);
	 											}
	 											
	 										}
	 										else if(index== 'p')
	 										{
	 											if(i>0)
	 											{
	 												index = parseInt(i)-1;
	 											}
	 											else
	 											{
	 												index = parseInt(fetVideoArray.length-1);
	 											}
	 										}
	 										else
	 										{
	 										    index = parseInt(i);
	 										} 
	 										currCatEntId = fetVideoArray[index][1];
	 										var aele = document.createElement('a');
	 										var aattr = document.createAttribute('href');
	 										aattr.nodeValue = 'javascript:playFetVideo(' + index + ');'; 
											aele.setAttributeNode(aattr); 									
	 										//aele.setAttribute('href',imgPathPrefix+videoPath+fetVideoArray[index][11]);
	 										aele.setAttribute('id','vl_f'+'_'+index);
	 										aele.setAttribute('class','arial11bold');
	 										var imgEle = document.createElement('img'); 
	 										imgEle.setAttribute('src',imgPathPrefix+'/images/multimedia/'+fetVideoArray[index][10]);
	 										imgEle.setAttribute('alt',fetVideoArray[index][2]);
	 										aele.appendChild(imgEle);
	 										document.getElementById('fetVideoName').innerHTML=fetVideoArray[index][2];
	 													
	 										par.appendChild(aele);
	 								      
	 									} 
	 								}									
 								}
					
}

//function call for link of next link of  featured videos 
function setUpFetVideoNext()
{

 	currCatEntId = parseInt(currCatEntId);
	changeFetVidList(currCatEntId,'n');
 
}

//function call for link of previous link of  featured videos 
function setUpFetVideoPrev()
{
	 
	currCatEntId = parseInt(currCatEntId);
	changeFetVidList(currCatEntId,'p');
	 
}
 

function changeTabImg(val)
{
	
	if(val == 'video_tab_img')
	{
		var img = document.getElementById(val).src;
		if(img.indexOf('videos_red.jpg')==-1)
		{
		        
			document.getElementById(val).src=imgPathPrefix+'images/multimedia/videos.jpg';
		}
	}
	else if(val == 'pano_tab_img')
	{
		var img = document.getElementById(val).src;
		if(img.indexOf('panoramic_red.jpg')==-1)
		{
			document.getElementById(val).src=imgPathPrefix+'images/multimedia/panoramic.jpg';
		}
	}
	else if(val == 'webcams_tab_img')
	{
		var img = document.getElementById(val).src;
		if(img.indexOf('webcams_red.jpg')==-1)
		{
			document.getElementById(val).src=imgPathPrefix+'images/multimedia/webcams.jpg';
		}
	}
}

function changeTabImg1(val)
{
	
	if(val == 'video_tab_img')
	{
		var img = document.getElementById(val).src;
		if(img.indexOf('videos_red.jpg')==-1)
		{
		    document.getElementById(val).src=imgPathPrefix+'images/multimedia/videos_over.jpg';
		}
	}
	else if(val == 'pano_tab_img')
	{
		var img = document.getElementById(val).src;
		if(img.indexOf('panoramic_red.jpg')==-1)
		{
			document.getElementById(val).src=imgPathPrefix+'images/multimedia/panoramic_over.jpg';
		}
	}
	else if(val == 'webcams_tab_img')
	{
		var img = document.getElementById(val).src;
		if(img.indexOf('webcams_red.jpg')==-1)
		{
			document.getElementById(val).src=imgPathPrefix+'images/multimedia/webcams_over.jpg';
		}
	}
}

function setClipReadyToPlay(urltmp)
{
 
 //alert("setClipReadyToPlay"); 
 if(urltmp.indexOf('http://')==-1)
 {
 urltmp = 'http://'+baseURLVal+imgPathPrefix+urltmp;
 }
 $(function() {
  	$f("player", {src: imgPathPrefix+'swf/flowplayer-3.1.5.swf',wmode:"opaque"},
	{				
	clip: {autoPlay:false,url:urltmp}	
// playlist plugin 
								   
})});
}


function setClipLoadAndPlay(urltmp)
{
 //alert("setClipLoadAndPlay"); 
 //urltmp = removeRelativePrefix(urltmp);
 if(urltmp.indexOf('http://')==-1)
 {
  if(urltmp.indexOf(imgPathPrefix)==-1)
  {
    urltmp = 'http://'+baseURLVal+imgPathPrefix+urltmp;
  }
  else
  {
    urltmp = 'http://'+baseURLVal+urltmp;
  }
 }


$f("player").setClip(urltmp);
$f("player").play(urltmp);
}


function setClipRefreshLoadAndPlay(urltmp)
{
 //alert("setClipRefreshLoadAndPlay"); 
 //urltmp = removeRelativePrefix(urltmp);
 if(urltmp.indexOf('http://')==-1)
 {
   	  if(urltmp.indexOf(imgPathPrefix)==-1)
	  {
	    urltmp = 'http://'+baseURLVal+imgPathPrefix+urltmp;
	  }
	  else
	  {
	    urltmp = 'http://'+baseURLVal+urltmp;
	  }
 }
 
 $(function() {
  	$f("player", {src: imgPathPrefix+'swf/flowplayer-3.1.5.swf',wmode:"opaque"},
	{				
	clip: {autoPlay:true,url:urltmp}	
// playlist plugin 
								   
}).play(urltmp);

});

 
 
}