var toggleFlag;
var boolFlag=0;
var BoolToggle=0;
var hostname = "http://"+document.location.hostname;
var nowPlayingId = '';
var clickFlag = 0;
var tabValue = "";
var ShareIdValue = "";
var BrokerTheme = "";
var scrollHeight = "";

function show(tabType,idno,theme,scrollHght)
{
	var url = "get_media.php";
	var queryString = "";
	queryString = "tab="+tabType;
	queryString += "&id="+idno;
	tabValue = tabType;
	ShareIdValue = idno;
	BrokerTheme = theme;
	
	ajaxRequest(url,queryString,resVideo = function(originalRequest) 
	{
			var response =  trim(originalRequest.responseText);
			//alert(response);
			if(response=="")
			{
				InitialResponse();
			}
			else
			{
				$("Menutxt").innerHTML = trim(response);
				var CatCount = response.split('&==&');   // used to get the Category count from response 
				//alert(CatCount[1]);
				var CatHght = (CatCount[1]*24.3)+24.3;
				if(CatHght < 485)
				{
					CatHght = 485;
					$('maincontent').style.height = CatHght+"px";
					$('thumbsScroll').style.height = CatHght+"px";
					$('Scrollbar-Down').style.top = "453px";
					$('Scrollbar-Track').style.height = "440px";
					scrollHeight = CatHght;
				}
				else
				{
					$('maincontent').style.height = CatHght+"px";
					$('thumbsScroll').style.height = CatHght+"px";
					$('Scrollbar-Down').style.top = (CatHght-49)+"px";
					$('Scrollbar-Track').style.height = (CatHght-62)+"px";
					scrollHeight = CatHght;
				}
				//alert(CatHght);
				//alert(idno);
				var elem = $('a');
				elem.onclick.apply(elem);
				Menu.init("menu", {"orientation": Menu.VERTICAL});
			}
	});
}

function InitialResponse()
{
	CustomScroll();
	$('Scrollbar-Container-thumbs').style.display = "none";
	$('maincontent').style.height = "485px";
	$("Cat_Title").innerHTML="&nbsp;";
	$("thumbs_Title").innerHTML="&nbsp;";
	$('bg').removeClassName('wrapnone');
	$('bg').addClassName('wraptocenter');
	$("image").style.display = "inline";
	$("image").src = "/wt/"+BrokerTheme+"/images/video/categorynotupload.jpg";
	$("lightbox_image").style.display = "none";  // hide for other type except img
	$("download_icon").style.display = "none";   // hide for youtube file
	$("BGid").style.display = "block";
	$("imgGO").style.display = "block";
	$("imagehref").href = "javascript:void(0);";
	$('linkWrapperId').style.display = "none";
	$('Sharebutton').style.display = "none";
	if(tabValue=="interoTube")
		$("imgGO").src = "/wt/"+BrokerTheme+"/images/video/teraSearch.png";
	else
		$("imgGO").src = "/wt/"+BrokerTheme+"/images/video/btn-go.png";
}

function CustomScroll()
{
	scroller  = null;
	scrollbar = null;
	scroller  = new jsScroller($("thumbsScroll"), 140, scrollHeight);			/*News*/
	scrollbar = new jsScrollbar ($("Scrollbar-Container-thumbs"), scroller, true);
}

function Display(value,heading,k,total,flag,DefFlag)
{
	//alert("flag="+flag+" DefFlag="+DefFlag);
	clickFlag=1;
	if(ShareIdValue!=0)   // for sharing purpose search criteria is hidden
	{
		$("searchComboId").style.display = "none";	
	}
	
	if(boolFlag==1)
	{
		flag=0;
		DefFlag=0; // for tab=interoTube default value
	}
	$("thumbs_Title").innerHTML="";
	$("thumbs_Title").innerHTML=heading;
	$("displayItems").innerHTML="";
	$("displayItems").innerHTML=value;
	$('linkWrapperId').style.display = "block";
	$('Sharebutton').style.display = "block";
	$("linkTextId").style.display = "block";
	$("imgGO").style.display = "block";
	if(tabValue=="interoTube")
		$("imgGO").src = "/wt/"+BrokerTheme+"/images/video/teraSearch.png";
	else
		$("imgGO").src = "/wt/"+BrokerTheme+"/images/video/btn-go.png";
	
	//alert("hello= "+tabValue);
	setTimeout("CustomScroll()",500); /* to call the function*/
	
	var elem = $$('#menu li > a.nimo');
	for(var i=0; i<elem.length ; i++){
		if(tabValue=="interoTube")
			elem[i].style.backgroundImage = "url(/wt/"+BrokerTheme+"/images/video/cat2.png)";
		else
		{
			elem[i].style.backgroundImage = "url(/wt/"+BrokerTheme+"/images/video/cat.png)";
			elem[i].removeClassName('cat_sel');
		}
		//elem[i].style.color = "white";
		elem[i].addClassName('cat_desel');
	}

	
	if(tabValue=="interoTube")
	{	
		$("main"+k).style.backgroundImage  = "url(/wt/"+BrokerTheme+"/images/video/catNew.png)";
		//$("main"+k).style.color = "white";
		$("main"+k).addClassName('cat_desel');
	}
	else
	{
		$("main"+k).addClassName('cat_sel');
	}
	$("main"+k).style.lineheight = "20px";
	$("main"+k).style.width = "115px";
	
	if(flag==1)
	{
		var ele = $$('#c a > img.largeImage');
	}
	else
	{
		if(DefFlag==1)
		{
			var ele = $$('#d a > img.largeImage');
		}
		else
			var ele = $$('#b a > img.largeImage');
	}
	
	if(ele.length == 1){
		
		var nameValue = ele[0].name.split('|==|'); 
		
		if($("image"))         /* for loading*/
		{
			$("image").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		else if($("lighty"))
		{
			$("lighty").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		else if ($("youTubeId"))
		{
			$("youTubeId").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		else if($("flash"))
		{
			$("flash").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		/*nameValue[4] contains Media URL to display the large image on the*/
		nameValue[2] = nameValue[2].replace(/\'/g,"&apos;");
		setTimeout("showLargeView('"+nameValue[4]+"','"+ele[0].id+"','"+nameValue[0]+"','"+nameValue[1]+"','"+nameValue[2]+"','"+nameValue[3]+"')",500);
		
	}
	
	if(toggleFlag==1)    /* used for animation of share button*/
	{
		toggleFlag=0;
		BoolToggle=0;
		$('Text').style.display="none";
		//share('0');
	}
	boolFlag=1;
	
}

function SearchDisplay(id)
{
	CustomScroll(); /* to call the function*/
	$('linkWrapperId').style.display = "block";
	$('Sharebutton').style.display = "block";
	$("linkTextId").style.display = "block";
	var elemSearch = $$('#menu li > a.nimo');
	for(var j=0; j<elemSearch.length ; j++){
		if(tabValue=="interoTube")
			elemSearch[j].style.backgroundImage  = "url(/wt/"+BrokerTheme+"/images/video/cat2.png)";
		else
		{
			elemSearch[j].style.backgroundImage  = "url(/wt/"+BrokerTheme+"/images/video/cat.png)";
			elemSearch[j].removeClassName('cat_sel');
		}
		//elemSearch[j].style.color = "white";
		elemSearch[j].addClassName('cat_desel');
	}
	if(tabValue=="interoTube")
	{
		$("main"+id).style.backgroundImage  = "url(/wt/"+BrokerTheme+"/images/video/catNew.png)";
		//$("main"+id).style.color = "white";
		$("main"+id).addClassName('cat_desel');
	}
	else
	{
		$("main"+id).addClassName('cat_sel');
	}
	$("main"+id).style.lineheight = "20px";
	$("main"+id).style.width = "115px";
	
	var eleSearch = $$('#search a > img.largeImage');
	if(eleSearch.length == 1){
		var nameValueSearch = eleSearch[0].name.split('|==|');

		if($("image"))         /* for loading*/
		{
			$("image").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		else if($("lighty"))
		{
			$("lighty").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		else if ($("youTubeId"))
		{
			$("youTubeId").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		else if($("flash"))
		{
			$("flash").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";
		}
		// nameValueSearch[4] is used for media url for large image 
		nameValueSearch[2] = nameValueSearch[2].replace(/\'/g,"&apos;");
		setTimeout("showLargeView('"+nameValueSearch[4]+"','"+eleSearch[0].id+"','"+nameValueSearch[0]+"','"+nameValueSearch[1]+"','"+nameValueSearch[2]+"','"+nameValueSearch[3]+"')",500);
	}
	if(toggleFlag==1)   /* used for animation of share button*/
	{
		toggleFlag=0;
		BoolToggle=0;
		$('Text').style.display="none";
		//share('0');
	}
}

function showLargeView(imgSrc,idValue,type,Url,Title,IsUpload) 
{
	$("lightbox_image").style.display = "none";  // hide for other type except img
	$("download_icon").style.display = "none";   // hide for youtube file
	Title = Title.replace(/&apos;/g,"'");
	iSrc = idValue;
	var exten = new Array("doc","docx","xls","xlsx","ppt","pptx","pps","ppsx","pdf",'jpg','jpeg','png','bmp','gif','flv','swf','key');
	var extenFlag = false;
	if(Url=="")
	{
		if(Title=="")
		{
			Title = "No additional information is available";
		}
		Url = "javascript:void(0)";
		$("linkTextId").innerHTML = '<a class="href" href="'+Url+'">'+Title+'</a>';
		$("imagehref").href = Url;
		$("imagehref").target = "";
	}
	else
	{
		var fileExten = Url.substring((Url.lastIndexOf(".")+1),Url.length);
		
		for(var i=0; i<exten.length ;i++)
		{
			if(fileExten == exten[i].toString())
			{
				extenFlag=true;
				break;
			}
		}
		if(extenFlag==true)
		{
			$("linkTextId").innerHTML = '<a class="href" href="'+Url+'">'+Title+'</a>';
			$("imagehref").href = Url;
			$("imagehref").target = "_self";
		}
		else
		{
			$("linkTextId").innerHTML = '<a class="href" href="'+Url+'" target="_blank">'+Title+'</a>';
			$("imagehref").href = Url;
			$("imagehref").target = "_blank";
		}
	}
	
	if(IsUpload=="1")
	{
		$("download_icon").style.display = "block";
		$("download_icon").href = "/brokerAdmin/videoNavigation/download.php?file="+imgSrc;
	}
	
	$("large").style.display = "block";
	$("BGid").style.display = "block";
	
	if(type=="img" || type=="link")
	{
		var ImageExtenVal = imgSrc.substring((imgSrc.lastIndexOf(".")+1),imgSrc.length);  // done for bmp files

		$('bg').removeClassName('wrapnone');
		$('bg').addClassName('wraptocenter');
		
		$("image").style.display = "inline";
		$("lighty").style.display = "none";
		$("flash").style.display = "none";
		$("youTubeId").style.display = "none";
		$("lighty").innerHTML = "";   // to stop the video
		$("flash").innerHTML = "";    // to stop the video
		$("youTubeId").innerHTML = "";  // to stop the video
		if(ImageExtenVal == "bmp")
		{
			$("image").src = imgSrc;
		}
		else
		{
			$("image").src = "/vendor/thumb/thumb.php?src="+imgSrc+"&x=590&y=360&default=videoCover";
		}
		if(type=="img")
		{
			$("lightbox_image").style.display = "block";
			if(ImageExtenVal == "bmp")
			{
				$("lightbox_image").href = imgSrc;
			}
			else
				$("lightbox_image").href = "/vendor/thumb/thumb.php?src="+imgSrc+"&x=750&y=520&default=videoCover";
		}
	}
	else if(type=="doc")
	{
		$('bg').removeClassName('wrapnone');
		$('bg').addClassName('wraptocenter');
		$("image").style.display = "inline";
		$("lighty").style.display = "none";
		$("flash").style.display = "none";
		$("youTubeId").style.display = "none";
		$("lighty").innerHTML = "";   // to stop the video
		$("flash").innerHTML = "";    // to stop the video
		$("youTubeId").innerHTML = "";  // to stop the video
		$("image").src ="/vendor/thumb/thumb.php?src="+hostname+"/wt/"+BrokerTheme+"/images/video/word.jpg&x=590&y=360&default=videoCover";	
	}
	else if(type=="pdf")
	{
		$('bg').removeClassName('wrapnone');
		$('bg').addClassName('wraptocenter');
		$("image").style.display = "inline";
		$("lighty").style.display = "none";
		$("flash").style.display = "none";
		$("youTubeId").style.display = "none";
		$("lighty").innerHTML = "";   // to stop the video
		$("flash").innerHTML = "";    // to stop the video
		$("youTubeId").innerHTML = "";  // to stop the video
		$("image").src ="/vendor/thumb/thumb.php?src="+hostname+"/wt/"+BrokerTheme+"/images/video/pdf.jpg&x=590&y=360&default=videoCover";	
	}
	else if(type=="xls")
	{
		$('bg').removeClassName('wrapnone');
		$('bg').addClassName('wraptocenter');
		$("image").style.display = "inline";
		$("lighty").style.display = "none";
		$("flash").style.display = "none";
		$("youTubeId").style.display = "none";
		$("lighty").innerHTML = "";   // to stop the video
		$("flash").innerHTML = "";    // to stop the video
		$("youTubeId").innerHTML = "";  // to stop the video
		$("image").src ="/vendor/thumb/thumb.php?src="+hostname+"/wt/"+BrokerTheme+"/images/video/excel.jpg&x=590&y=360&default=videoCover";	
	}
	else if(type=="ppt" || type=="pps" || type=="key")
	{
		$('bg').removeClassName('wrapnone');
		$('bg').addClassName('wraptocenter');
		$("image").style.display = "inline";
		$("lighty").style.display = "none";
		$("flash").style.display = "none";
		$("youTubeId").style.display = "none";
		$("lighty").innerHTML = "";   // to stop the video
		$("flash").innerHTML = "";    // to stop the video
		$("youTubeId").innerHTML = "";  // to stop the video
		$("image").src ="/vendor/thumb/thumb.php?src="+hostname+"/wt/"+BrokerTheme+"/images/video/powerpoint.jpg&x=590&y=360&default=videoCover";	
	}

	else if(type=="swf")
	{
		$("flash").style.display = "block";
		$("image").style.display = "none";
		$('bg').removeClassName('wraptocenter');
		$('bg').addClassName('wrapnone');
		$("lighty").style.display = "none";
		$("youTubeId").style.display = "none";
		$("lighty").innerHTML = "";   // to stop the video
		$("flash").innerHTML = "";    // to stop the video
		$("youTubeId").innerHTML = "";  // to stop the video
		$('flash').innerHTML = '<object width="590" height="360" onclick=click("'+Url+'")><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed bgcolor="#000000" src="'+imgSrc+'" width="590" height="360" allowscriptaccess="always" allowfullscreen="true" wmode="transparent"></embed></object>';
		//$('flash').innerHTML = '<object width="590" height="360"><param name="movie" value="http://www.youtube.com/v/Ahg6qcgoay4&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/Ahg6qcgoay4&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="590" height="360" bgcolor="#000000" wmode="transparent"></embed></object>';
		//$("flash").innerHTML = '<a href="'+Url+'" target="_self"></a>';
		/*var c1 = $("flash");
		c1.onclick = {$("imagehref").href = Url};*/
	}
	else if(type="flv")
	{
		$('bg').removeClassName('wraptocenter');
		$('bg').addClassName('wrapnone');
		$("image").style.display = "none";
		$("flash").style.display = "none";
		$("lighty").innerHTML = "";   // to stop the video
		$("flash").innerHTML = "";    // to stop the video
		$("youTubeId").innerHTML = "";  // to stop the video
		if(IsUpload==1)
		{
			$("lighty").style.display = "block";
			$("youTubeId").style.display = "none";
			$f("lighty", "flowplayer-3.1.5.swf", { 
		 
			// configure clip to use "lighthttpd" plugin for providing video data 
			clip: { 
				url: imgSrc, 
				provider: 'lighttpd'
			}, 
				 
			// streaming plugins are configured normally under the plugins node 
			plugins: { 
				lighttpd: { 
					url: 'flowplayer.pseudostreaming-3.1.3.swf'
				} 
			} 
			});
		}
		else if(IsUpload==0) /* for youtube videos*/
		{
			$("youTubeId").style.display = "block";
			$("lighty").style.display = "none";
			$("youTubeId").innerHTML = '<object width="590" height="360"><param name="movie" value="http://www.youtube.com/v/'+imgSrc+'&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+imgSrc+'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="590" height="360" bgcolor="#000000" wmode="transparent"></embed></object>';
		}
		else if(IsUpload==2)  /* for youtube videos (Search criteria) */
		{
			$("youTubeId").style.display = "block";
			$("lighty").style.display = "none";
			$("youTubeId").innerHTML = '<object width="590" height="360"><param name="movie" value="'+imgSrc+'&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+imgSrc+'&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="590" height="360" bgcolor="#000000" wmode="transparent"></embed></object>';
		}
	}
	//var el = $('shareId');
	//el.onclick = share;

	//alert("nowPlayingId=="+nowPlayingId+" clickflg=="+clickFlag);
	if(nowPlayingId != "") 
	{
		if(clickFlag==0)
		{
			$("playingID"+nowPlayingId).style.display ="none";
		}	
	}
	$("playingID"+idValue).src ="/wt/"+BrokerTheme+"/images/video/nowPlayingNewTrans.png";
	$("playingID"+idValue).style.display ="block";
	nowPlayingId = idValue;
	clickFlag=0;
}

function click(val)
{
	$("flash_href").href = val;
}

function share(tno)
{
	if(tno==1 && BoolToggle==0)
	{
		toggleFlag=1;
		BoolToggle=1;
		if(tabValue=="interoTube")
			$("ShareText").innerHTML= hostname+"/video/index.php?tab=interoTube&id="+iSrc;
		else
			$("ShareText").innerHTML= hostname+"/video/index.php?id="+iSrc;
		Effect1.toggle('Text', 'slide', { delay: -0.01 }); 
		return false;
		
	}

	/*else if(tno==0 && BoolToggle==1)  // used for toogle close 
	{
		BoolToggle=0;
		Effect.toggle('Text', 'slide', { delay: -0.3 }); 
		return false;
	}*/
}

function cancelShare()
{
	BoolToggle=0;
	Effect1.toggle('Text', 'slide', { delay: -0.1 }); 
	return false;
}

Event.observe(document, 'click', function(event) 
{
	var tag = Event.element(event).className;
	if(tag=='largeImage')
	{
		
		$("image").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";	 /* for loading*/
		$("lighty").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif"; /* for loading*/
		$("youTubeId").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";	 /* for loading*/
		$("flash").src = "/wt/"+BrokerTheme+"/images/video/help-loading1.gif";	 /* for loading*/

		if(toggleFlag==1)    /* used for animation of share button*/
		{  
			toggleFlag=0;
			BoolToggle=0;
			$('Text').style.display="none";
			//share('0');
		}
		
		var ElementValue = Event.element(event).name.split('|==|');
		if(ElementValue[5])          // for spliting name and using main id from search
		{
			var elem = $$('#menu li > a.nimo');
			for(var i=0; i<elem.length ; i++)
			{
				if(tabValue=="interoTube")
					elem[i].style.backgroundImage = "url(/wt/"+BrokerTheme+"/images/video/cat2.png)";
				else
				{
					elem[i].style.backgroundImage = "url(/wt/"+BrokerTheme+"/images/video/cat.png)";
					elem[i].removeClassName('cat_sel');
				}
				//elem[i].style.color = "white";
				elem[i].addClassName('cat_desel');
			}
			if(tabValue=="interoTube")
			{
				$("main"+ElementValue[5]).style.backgroundImage  = "url(/wt/"+BrokerTheme+"/images/video/catNew.png)";
				//$("main"+ElementValue[5]).style.color  = "white";
				$("main"+ElementValue[5]).addClassName('cat_desel');
			}
			else
			{
				$("main"+ElementValue[5]).addClassName('cat_sel');
			}
			$("main"+ElementValue[5]).style.lineheight = "20px";
			$("main"+ElementValue[5]).style.width = "115px";
		}
		
		//showLargeView(titleVal[0],Event.element(event).id,ElementValue[0],ElementValue[1],ElementValue[2]);
		/*ElementValue[4] is used for media url large image*/
		ElementValue[2] = ElementValue[2].replace(/\'/g,"&apos;");
		setTimeout("showLargeView('"+ElementValue[4]+"','"+Event.element(event).id+"','"+ElementValue[0]+"','"+ElementValue[1]+"','"+ElementValue[2]+"','"+ElementValue[3]+"')",500); 
	}
	else if(tag=='buttonWrapper')
	{
		share('2');
	}
});

function viewRecord(id)
{
	if(trim($("videoInput").value) == '')
	{
		alert("Please enter file name to search.");
		return;
	}
	nowPlayingId = '';  /* for keeping the image now playing while search*/

	$("displayItems").innerHTML = "";
	
	var url = "viewVideo.php";
	var queryString = "";
	queryString = queryString + "&search=record";
	queryString = queryString + "&title="+escape($('videoInput').value);
	queryString += "&tab="+tabValue;
	
	ajaxRequest(url,queryString,resVideo = function(originalRequest) 
				{
						var response =  trim(originalRequest.responseText);
						var responseArr = response.split("===");
						
						if(responseArr[0] > 0)
						{
							
							$("displayItems").innerHTML = trim(responseArr[1]);
							$("thumbs_Title").innerHTML = "Search Result";
							var element = $('search');
							element.onclick.apply(element);
						}
						else
						{
							CustomScroll();
							//$("large").style.display = "block";
							$("thumbs_Title").innerHTML = "&nbsp;";
							$("BGid").style.display = "block";
							$("imagehref").href = "javascript:void(0);";
							$('linkWrapperId').style.display = "none";
							$("linkTextId").style.display = "none";
							$('Sharebutton').style.display = "none";
							
							$('bg').removeClassName('wrapnone');
							$('bg').addClassName('wraptocenter');
							
							$("image").style.display = "inline";
							$("lighty").style.display = "none";
							$("flash").style.display = "none";
							$("youTubeId").style.display = "none";
							$("lightbox_image").style.display = "none";  // hide for other type except img
							$("download_icon").style.display = "none";   // hide for youtube file
							$("image").src = "/wt/"+BrokerTheme+"/images/video/noresult-1.jpg";
							//$("displayItems").innerHTML = '<br/><table align="center"><tr style="height:30px;"><td colspan="2" class="txtOther moduleLeft" style="border:1px solid #000000;background-color:#DADADA;padding-left: 10px;padding-right: 10px;">No result found</td></tr></table>';
						}
				});
}

function videoEnter(event)
{
	if(event && event.keyCode == 13)
	{
		viewRecord('');
	}
}

function InteroWindowOpen()
{
	var url = hostname+"/video/media.php?tab=interoTube";
	var sLoc=",location=1";
	win=window.open(url,"","width=1000,height=620,resizable=no,dialog=yes,scrollbars=no,toolbar=no,menubar=no,titlebar=no,directories=no,location=yes");
}

