var browser=new Browser();
var vid=0;vvid="";vtitle="";vvideo="";vpic= "";
function activateTab(objIndex){			
	
	if(trackingPages!=undefined)trackingPages(location.href.substr(location.href.lastIndexOf("/")),"maintab"+objIndex);
	for(var i=0;i<4;i++){
		$("#t"+i).get(0).className= "noactive";
		if(MM_findObj("con"+i)!=null){
			MM_findObj("con"+i).style.display="none";
		}
	
	}
	
	stopAllMovie();		
	objIndex = parseInt(objIndex,10);		


	if(objIndex==0){
		if(MM_findObj("mainmovief")!=null) if($.browser.msie) MM_findObj("mainmovief").SetVariable("startPlay","true");
		location.hash= tabs[objIndex];
	}else if(objIndex==2){
		if(MM_findObj("tvcf")!=null){
			if($.browser.msie) MM_findObj("tvcf").SetVariable("startPlay","true");
		}else{			
			refreshTVC(0,vvid, vvideo, vpic,vtitle,undefined,vdesc);
		}		
		location.hash = tabs[objIndex];
	}else if(objIndex==1){
		if(MM_findObj("launchmovief")!=null) if($.browser.msie) MM_findObj("launchmovief").SetVariable("startPlay","true");
		location.hash = tabs[objIndex];
	}else if(objIndex==3){
		location.hash= tabs[objIndex];
		//initGallery();
	}

	if(objIndex>=0){	
		$("#t"+objIndex).get(0).className= "active";
		$("#con"+objIndex).get(0).style.display="block";		
	}
}
var intt =-1;
var tabs= ["home","mio-tv-preview","exclusive-video","coverage"];
var cti= 0;
var lcti = cti;
function initTab(){
	var _url = location.href;
	var a_url = _url.split("#");
	//lcti = cti;
	//cti = 3;
	if(a_url.length<=1){
		location.hash= "coverage";
	}else{		
		//location.hash= "home";		
	}
	checkTab();
	intt= setInterval("checkTab()",500);
}
function checkTab(){
	var _url = location.href;
	var a_url = _url.split("#");
	if(a_url.length>1){
		var a_bookmark = a_url[1].toLowerCase();

		lcti = cti;
		cti=($.inArray(a_bookmark,tabs));
	}
	if(cti!=lcti) activateTab(cti);
	//document.write(location.hash);
}
function stopAllMovie(){	
	if($.browser.msie){
		if($("#mainmovief").size()>0) MM_findObj("mainmovief").SetVariable("stopPlay","true");
		if($("#launchmovief").size()>0) MM_findObj("launchmovief").SetVariable("stopPlay","true");
		if($("#tvcf").size()>0) MM_findObj("tvcf").SetVariable("stopPlay","true");
	}

}


function nexttvc(_cid){
	tvcid++;
	if(tvcid>=5)tvcid=0;
	var tgt= document.getElementById("lists2");				
	tgt.getElementsByTagName("a")[tvcid].onclick();

	
}

var $cid=0;
var $pagesize= 15;
var $pageindex = 0;
var $oldpageindex = $pageindex;
	$(document).ready(function(){
		if (jQuery.fn.pngFix) $(document).pngFix();
		
	});
	function initGallery(isrefresh){
		$(".photodesc").each(function(){
			$(this).hide();
		});
		$("#pop_bg").css({opacity:0.5});
		$("#photo_preloader").show();
		$("#photodesc_0").animate({opacity:'show'},'slow');
		
		$.get("dyncon_photogallery.asp",{f:"",c:$pageindex},
			function(data){			
				initGalleryButtons(data,isrefresh);
		  });
		$("#popcon_bg #popcon_btnleft a").unbind("click");
		$("#popcon_bg #popcon_btnleft a").bind("click",function($e){
			$cid--;
			$e.preventDefault();
			//if($cid<0) $cid=$("#album1 a").length;
			$pageindex= Math.ceil(($cid+1)/$pagesize)-1;
			if($pageindex!=$oldpageindex){
				initGallery(true);
			}else{
				$("#b"+$cid).click();
			}
			$oldpageindex = $pageindex;
			
			//$("#b"+$cid).get(0).click();
			//return false;
		});
		$("#popcon_bg #popcon_btnright a").unbind("click");
		$("#popcon_bg #popcon_btnright a").bind("click",function($e){
			$cid++;
			$e.preventDefault();
			//if($cid>=$("#album1 a").length) $cid=0;			
			$pageindex= Math.ceil(($cid+1)/$pagesize)-1;
			
			if($pageindex!=$oldpageindex){				
				initGallery(true);
			}else{
				//$("#b"+$cid).get(0).click();				
				$("#b"+$cid).click();
			}
			$oldpageindex = $pageindex;
			//return false;
		});
		

		$("#popcon_bg #popcon_btnclose a,#pop_bg").click(function($e){
			$e.preventDefault();
			hidePop();
			return false;
		});
		$("#popcon_btnleft .btn_bg, #popcon_btnright .btn_bg").each(function(){
			$(this).hide();
		});
	}
	function hidePop(){
		$("#popcon_bg").hide();
		$("#pop_bg").hide();
		$("select,embed,object").each(function(){
			$(this).css({visibility:"visible"});
		});
	}

	function initGalleryButtons(_data,isrefresh){		
		$("#photo_preloader").hide();
		$("#myphotos").hide();
		$("#myphotos").fadeIn("normal");		
		var _arr= _data.split("<|||>");
		$("#myphotos").get(0).innerHTML=_arr[0];
		
//		alert(_data);
		
		$("#album1 a").each(function(index,elem){
			$(this).unbind("click");
			$(this).bind("click",function($e){					
				$e.preventDefault();
				$cid= $(this).get(0).id.substr(1);				
				//alert($cid);
				var $btn= $("#b"+$cid).get(0);
				var $url= $btn.href;
				var $title= $btn.title;
				
				showGallery($url,$title);				
				//return false;
			});
		});
		if(isrefresh){
			$("#b"+$cid).click();
		}

	}
	function showGallery(_url,_title){	
		var $arr= _url.split("?w=");
		var $arr2= _url.split("&o=");		
		var $url= $arr[0];
		var $page= $arr2[1];
		var $w= parseInt($arr[1],10);
		$("#popcon_img img").remove();
		$("#popcon_img").append("<img src=\"\" />");
		$("#popcon_img img").hide();
		$("#popcon_img img").attr({src:"i/spacer.gif"+'?rand=' + Math.floor(Math.random() * 999999999),width:1,height:1});
		if($w==800){

			$("#popcon_bg .wrapper").css({width:$w+16,height:656});
			$("#popcon_bg").css({width:$w+16,height:656});
			$("#popcon_btnleft a,#popcon_btnright a").each(function(){
				$(this).css({height:616});
			});
			$("#popcon_img img").attr({src:$url,width:800,height:600});

			$("#popcon_title .title").html(_title);
			$("#popcon_page").html($page);
			$("#popcon_page").css({top:"630px"});
			$("#popcon_bg").css({left:($(window).width()-$("#popcon_bg").width())/2,top:(($("body").height()-$("#popcon_bg").height())/2)});
		}else{
			$("#popcon_bg .wrapper").css({width:$w+16,height:856});
			$("#popcon_bg").css({width:$w+16,height:856});
			$("#popcon_btnleft a,#popcon_btnright a").each(function(){
				$(this).css({height:816});
			});
			$("#popcon_img img").attr({src:$url,width:600,height:800});

			$("#popcon_title .title").html(_title);
			$("#popcon_page").html($page);
			$("#popcon_page").css({top:"830px"});
			$("#popcon_bg").css({left:($(window).width()-$("#popcon_bg").width())/2,top:(($("body").height()-$("#popcon_bg").height())/2)});
		}
		
		
		$("#btn_download").attr({href:"download_gallery.asp?fn=" + $url.substr(_url.indexOf("photos/"))});
		$("#popcon_bg #popcon_btnright a").hover(function(){
			$("#popcon_btnright .btn_bg").each(function(){
				$(this).show();
			});
		},function(){
			$("#popcon_btnleft .btn_bg, #popcon_btnright .btn_bg").each(function(){
				$(this).hide()
			});
		});
		$("#popcon_bg #popcon_btnleft a").hover(function(){
			$("#popcon_btnleft .btn_bg").each(function(){
				$(this).show();
			});
		},function(){
			$("#popcon_btnleft .btn_bg, #popcon_btnright .btn_bg").each(function(){
				$(this).hide()
			});
		});
		
		$.cacheImage($url, {
			load:function(e){
				$("#popcon_img img").animate({opacity:'show'},'slow');	
			},
			error:function(e){
				$("#popcon_img img").animate({opacity:'show'},'slow');	
			}
		});
		$("#popcon_img img").hide();
		//$("#popcon_img img").animate({opacity:'show'},'slow');
		$("#pop_bg").show();
		$("#popcon_bg").animate({opacity:'show'},'normal');
		$("select,embed,object").each(function(){
			$(this).css({visibility:"hidden"});
		});
		//$("#pop_bg").css({left:0,height:$("#topheader").height()+$("#container").height()+$("#footer").height()+50});
		$("#pop_bg").css({left:0});

		$("#popcon_bg #popcon_btnleft .btn_bg, #popcon_bg #popcon_btnright .btn_bg").each(function(){
			$(this).css({top:($("#popcon_bg .wrapper").height()-$(this).height())/2});
		});
	}
	function refreshPhotoGallery(obj){			
		$(".photodesc").each(function(){
			$(this).hide();
		});
		$("#photo_preloader").show();

		$("#photodesc_" +obj.selectedIndex).animate({opacity:'show'},'slow');
		var rdate = new Date();
		
		$.get("dyncon_photogallery.asp",{f:obj.value,d:rdate.getTime()},
			function(data){
				initGalleryButtons(data);
				return false;
		  });
	}
	function refreshContentPhotoGallery(cg,cp){		
		var rdate = new Date();
		
		$(".photodesc").each(function(){
			$(this).hide();
		});
		$("#photo_preloader").show();
		$.get("dyncon_photogallery.asp",{f:cg,d:rdate.getTime(),c:cp},
			function(data){
				initGalleryButtons(data);
				return false;
		  });
		 
	}

	function doSomething() {
		//alert("I'm done resizing for the moment");
		//alert($(window).width());
		//$("#pop_bg").css({left:0,height:$("#topheader").height()+$("#container").height()+$("#footer").height()+30});
		//$("#pop_bg").css({left:0});
		//$("#popcon_bg").css({left:($(window).width()-$("#popcon_bg").width())/2});
		reposition($("#pop_bg"));
		reposition($("#popcon_bg"));
	};

	var resizeTimer = null;
	$(window).resize(function() {		
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(doSomething, 100);
		
	}); 

	$(window).keydown(function(event){	
		if (event.keyCode == 27){
			hidePop();
		}	
	});
	$(document).keydown(function(event){		
		if (event.keyCode == 27){
			hidePop();
		}	
	});
	if($.browser.msie && parseInt($.browser.version) < 7) {
		$(window).scroll(function(){			
			if($("#popcon_bg").is(":visible")) {				
				doSomething();
			}
		});
	}
	function reposition(tgt){ //calculate the position
		var left = 0;
		var top = 0;
		
		var winW = $(window).width();
		var winH =  $(window).height();
		var dlgH = tgt.height();
		var dlgW = tgt.width();

		if ($.browser.msie) {
			left = document.body.scrollLeft || document.documentElement.scrollLeft;
			top = document.body.scrollTop || document.documentElement.scrollTop;
		}else {
			left = window.pageXOffset;
			top = window.pageYOffset;
		}

		var topOff = top + winH/2 - dlgH/2; //offset for IE6
		var	leftOff = left + winW/2 - dlgW/2; //offset for IE6
		var topFixed = topOff - top;
		var	leftFixed = leftOff - left;
		
		if ($.browser.msie && parseInt($.browser.version) < 7) { // IE6			
			tgt.css({left: leftOff});
		}else{	// firefox and IE7
			tgt.css({left: leftFixed});
		}
	}

