var chkClickMediaBox = true;

$(document).ready(function() {
	
	for(var i=1;i<=20;i++){
		jQuery('#mycarousel'+i).jcarousel();
	}
	
	$('#activityArea').bxSlider({
    //auto: true,
		displaySlideQty: 6,
		moveSlideQty: 5,
		speed:1000,
		controls: true,
		pause:5000,
		nextText: '',
		//nextImage: 'file/frontend/template/images/landing/landing-banner-right.png',
		prevText: '',
		pager: false
    
  	});
	
	$("#mthaiSlider").bxSlider({
	
		pager: true,
		controls:false
	});
	
  $('#vdo-play-slide').bxSlider({
    	mode: 'vertical',
		pager: false,
		controls:true,
		nextText: '',
		nextImage: 'firstpage/images/vdo-list-next.png',
		prevText: '',
		prevImage: 'firstpage/images/vdo-list-prev.png'
	  });
	
	$('#tvProgramLogoInner').bxSlider({
		mode: 'vertical',
		displaySlideQty: 4,
   	 	moveSlideQty: 3
	});
	
	$("#activityOuter .bx-next").css("visibility","hidden");
	$("#activityOuter .bx-prev").css("visibility","hidden");
 
			
	$('#scrollbar2').tinyscrollbar({ axis: 'x'});	

});

function firstLoad(){	
	
	//$("#activityArea").css("left","-45px");
	//var element = document.getElementById("movieContent").childNodes.length;
	//element /=2;
	//var picNum = Math.floor(element);
	//document.getElementById("movieContent").style.width = (picNum*132)+"px";
	getHotNews();	
}

var hotNews;

function getHotNews(){
		
		$.post('firstpage/keep-cache/hot-news-showing.html',function(data){
				
				hotNews = data.split("&#*#&");
				hotNews.splice(20,1);
				hotNewsRandom();
				
		});
}

function hotNewsRandom(){
	
	var rand = Math.ceil((Math.random() * 20))-1;
	$("#hotNewsContent").html(hotNews[rand]);
	setTimeout("hotNewsRandom()",10000);
	
}

function changeMenuStory(me,status){	
	if(status == 0)
		me.style.backgroundImage = "url('firstpage/images/bg-story-inner.png')";
	else if(status == 1)
		me.style.backgroundImage = "";
	
}

function txtLogin(me,status,txtName){
	
	if(status == 0 && txtName == 0 && me.value == "Username...")
		me.value = "";
	else if(status == 1 && txtName == 0 && me.value == "")
		me.value = "Username...";
	else if(status == 0 && txtName == 1 && me.value == "Password")
		me.value = "";		
	else if(status == 1 && txtName == 1 && me.value == "")
		me.value = "Password";		
	
}

function enter(key,name){
	
	if(key == 13 && name == "loginForm"){
	
		if(!(document.getElementById('txtUsername').value == "Username..."))
			login();	
	}
}

var size = '0';

var keepTmpVdoCss = new Array();
var keepTmpVdoCss2 = new Array();
var tmpNow = "";

function openBlock(name,url,vdoName){
	
	if(name == "liveTv"){
		//$("#tv-ad-logo").html("<img src='firstpage/images/wrap-live.png' />");
		$("#vdoFrame").css("background-image","url('firstpage/images/live-tv-frame.png')");
		
		keepTmpVdoCss2[0] = $("#vdoInclude").css("height");
		keepTmpVdoCss2[1] = $("#vdoInclude").css("top");
		keepTmpVdoCss2[2] = $("#vdoInclude").css("left");
		keepTmpVdoCss2[3] = $("#vdoInclude").css("width");
		
		$("#vdoInclude").css("height","360px");
		$("#vdoInclude").css("width","640px");
		$("#vdoInclude").css("top","75px");
		$("#vdoInclude").css("left","67px");
		
		tmpNow = "liveTv";
		
		size = '0';
		
	}else if(name == "streaming"){
		
		document.getElementById("tv-ad-logo").innerHTML = "";
		document.getElementById("vdoFrame").style.backgroundImage = "";
		size = '1';
		
		keepTmpVdoCss[0] = $("#lightingBox").css("width");
		keepTmpVdoCss[1] = $("#lightingBox").css("height");
		keepTmpVdoCss[2] = $("#lightingBox").css("background");
		keepTmpVdoCss[3] = $("#vdoInclude").css("width");
		keepTmpVdoCss[4] = $("#vdoInclude").css("height");
		keepTmpVdoCss[5] = $("#tv-ad-logo").css("bottom");
		keepTmpVdoCss[6] = $("#tv-ad-logo").css("left");
		keepTmpVdoCss[7] = $("#vdoInclude").css("top");
		keepTmpVdoCss[8] = $("#vdoInclude").css("left");
		keepTmpVdoCss[9] =$("#closeVDOLightBoxBtn").css("top");
		keepTmpVdoCss[10] = $("#closeVDOLightBoxBtn").css("right");
		keepTmpVdoCss[11] = $("#vdoPlay").html();
		
		$("#lightingBox").css("width","500px");
		$("#lightingBox").css("height","400px");
		$("#lightingBox").css("background","url('')");
		$("#lightingBox").css("background","#FFFFFF");
		$("#vdoInclude").css("width","397px");
		$("#vdoInclude").css("height","307px");
		//$("#tv-ad-logo").html("<img src='firstpage/images/fream-vdo-concert.png' />");
		$("#tv-ad-logo").html("<img src='firstpage/images/frame-vdo-concert.png' />");
		$("#tv-ad-logo").css("bottom","0px");
		$("#tv-ad-logo").css("left","0px");
		$("#vdoPlay").html("");
		$("#vdoInclude").css("top","43px");
		$("#vdoInclude").css("left","49px");		
		$("#closeVDOLightBoxBtn").css("top","-2px");
		$("#closeVDOLightBoxBtn").css("right","3px");
		
		tmpNow = "streaming";
		
		
	}else{
		$("#vdoInclude").css("width","644px");
		$("#tv-ad-logo").html("");
		$("#vdoFrame").css("background-image","url('')");
		size = '0';
		tmpNow = "vdo";
	}
	
	document.getElementById("blockOpacity").style.display = "block";
	
	openBlock2(10,url,name,vdoName);
	
	
	if(navigator.appName != "Microsoft Internet Explorer")
		vdoLightBox(size);
	else
		vdoLightBoxForIE(size);
}

function vdoLightBoxForIE(){
	document.getElementById("blockOpacity").style.opacity = "0.70";
	document.getElementById("blockOpacity").style.filter = "alpha(opacity = 70)";
	document.getElementById("coverBlockOpacity").style.visibility = "visible";
	setTimeout("$( '#lightingBox' ).css('display','block')",1000);
	setTimeout("includeVdo('"+url+"','"+name+"','"+vdoName+"')",1000);
	
}

function closeVDOLightBox(){
	$("#vdoInclude").attr("src","");
	
	if(size == "1"){
		
		size = "0";	
	}
	
	if(tmpNow == 'liveTv'){
		$("#vdoInclude").css("height",keepTmpVdoCss2[0]);
		$("#vdoInclude").css("top",keepTmpVdoCss2[1] );
		$("#vdoInclude").css("left",keepTmpVdoCss2[2] );
		
		}
		
		
	if(navigator.appName == "Microsoft Internet Explorer"){
		$("#vdoInclude").html("");
		$("#tv-ad-logo").html("");
		document.getElementById('coverBlockOpacity').style.visibility = 'hidden';
		document.getElementById('tv-ad-logo').innerHTML = '';
		
		$("#lightingBox").css("width",keepTmpVdoCss[0] );
		$("#lightingBox").css("height",keepTmpVdoCss[1]);
		$("#lightingBox").css("background","url('firstpage/images/bg-vdo-play.png')");
		$("#vdoInclude").css("width",keepTmpVdoCss[3]);
		$("#vdoInclude").css("height",keepTmpVdoCss[4]);
		$("#tv-ad-logo").css("bottom",keepTmpVdoCss[5]);
		$("#tv-ad-logo").css("left",keepTmpVdoCss[6]);
		$("#vdoInclude").css("top",keepTmpVdoCss[7]);
		$("#vdoInclude").css("left",keepTmpVdoCss[8]);
		$("#vdoPlay").html(keepTmpVdoCss[11]);
		$("#closeVDOLightBoxBtn").css("top","15px");
		$("#closeVDOLightBoxBtn").css("right","15px");
		
		return;
	}else{
		vdoLightBox();
	}
}

function vdoLightBox(size){
	
		var container = $( "#lightingBox" );
		if (container.is( ":visible" )){
				 
			// Hide - slide up.
			document.getElementById("vdoInclude").innerHTML = "";
			document.getElementById("tv-ad-logo").innerHTML = "";
			container.slideUp( 500 );
			setTimeout("afterCloseVdoBox()",750);
		} else {		 
			// Show - slide down.
			container.slideDown( 500 );
			document.getElementById("coverBlockOpacity").style.visibility = "visible";		 
		}
	
}

function afterCloseVdoBox(){
	
		$("#lightingBox").css("width",keepTmpVdoCss[0] );
		$("#lightingBox").css("height",keepTmpVdoCss[1]);
		$("#lightingBox").css("background",keepTmpVdoCss[2] );
		$("#vdoInclude").css("width",keepTmpVdoCss[3]);
		$("#vdoInclude").css("height",keepTmpVdoCss[4]);
		$("#tv-ad-logo").css("bottom",keepTmpVdoCss[5]);
		$("#tv-ad-logo").css("left",keepTmpVdoCss[6]);
		$("#vdoInclude").css("top",keepTmpVdoCss[7]);
		$("#vdoInclude").css("left",keepTmpVdoCss[8]);
		$("#vdoPlay").html(keepTmpVdoCss[11]);
		$("#closeVDOLightBoxBtn").css("top","15px");
		$("#closeVDOLightBoxBtn").css("right","15px");
		
		document.getElementById('coverBlockOpacity').style.visibility = 'hidden'; document.getElementById('tv-ad-logo').innerHTML = '';
}

function openBlock2(op,url,name,vdoName){

	document.getElementById("blockOpacity").style.opacity = "0.70";
	document.getElementById("blockOpacity").style.filter = "alpha(opacity = 70)";
	setTimeout("includeVdo('"+url+"','"+name+"','"+vdoName+"')",400);

	
}

function includeVdo(url,name,vdoName){
	var value = "ieCache="+Math.random()+"&vdoName="+vdoName;
	url += value;
	//ajaxSendNGet(url,"vdoInclude");
	
	$("#vdoInclude").attr("src",url);
}

function login(){
	
	// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
	var username = encodeURIComponent(document.getElementById('txtUsername').value);
	var password = encodeURIComponent(document.getElementById('txtPassword').value);
	if(username == "Username...")
		return;
	
	// Set te random number to add to URL request
	nocache = Math.random();
	// Pass the login variables like URL variable
			// Optional: Show a waiting message in the layer with ID ajax_response
	document.getElementById('loginFormInner').style.visibility = 'hidden';
	document.getElementById('tmpLogin').innerHTML = "<img src=\"firstpage/images/loadera32.gif\" border=\"0\" style=\"position:relative; \" />";
	
	var xmlHttp = createXMLHttp();
	
	if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4){
		xmlHttp.open('get', 'firstpage/login/login.php?email='+username+'&psw='+password+'&nocache = '+nocache);
		xmlHttp.send(null);
		xmlHttp.onreadystatechange = function(){
			
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
				var response = xmlHttp.responseText;
				if(response == "0"){
					// if login fails
					var div = document.getElementById('tmpLogin');
					div.innerHTML = "<p id=\"login_error_msg\" align=\"center\" style=\"color:red;padding-top:8px;\">Login failed! Verify user and <br / >password.</p>";
					setTimeout("document.getElementById('loginFormInner').style.visibility = 'visible'; document.getElementById('tmpLogin').innerHTML = ''",3000);
					
					// else if login is ok show a message: "Welcome + the user name".
				} else {
					document.getElementById('loginForm').innerHTML = response;
					document.getElementById('login_form').innerHTML = '';
				}
			}
			
		};
	}
	
}
	
function getMajorMovies(status){
	
	var rsIeCache = Math.random();
	var urlNData="http://www.my3space.com/firstpage/movie/showing.php?req=showing&rsIeCache="+rsIeCache;
	document.getElementById("btnMajorMovie").style.right = "80px";
	
	if(status == 1){
		urlNData = "http://www.my3space.com/firstpage/movie/showing.php?req=coming&rsIeCache"+rsIeCache;
		document.getElementById("btnMajorMovie").style.right = "-10px";	
	}
	
	
	var xmlHttp = createXMLHttp();
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
		xmlHttp.open("get",urlNData,true);
		xmlHttp.send(null);	
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
				var res = xmlHttp.responseText;		
				document.getElementById("movieContent").innerHTML = res;
				
				var element = document.getElementById("movieContent").childNodes.length;
				element /=2;
				var picNum = Math.floor(element);
				document.getElementById("movieContent").style.width = (picNum*132)+"px";
	
				$(document).ready(function(){
		
					$('#scrollbar2').tinyscrollbar({ axis: 'x'});	
		
				});	
			}
		};
	}
	
}


function mediaboxChange(me){

	setTimeout("getDivMediaBox('"+me+"')",500);
	if(navigator.appName == "Microsoft Internet Explorer")
		return;	
		
	var container = $( "#divMediaBox" );
	container.slideUp( 500 );
	
}
	
function getDivMediaBox(me){
	var container = $( "#divMediaBox" );
	
	if(me == "i"){
		$("#inewsMain").css("display","block");
		$("#highlightMain").css("display","none");
		$("#categoryMain").css("display","none");
		
	}else if(me=="h"){
		
		$("#highlightMain").css("display","block");
		$("#categoryMain").css("display","none");
		$("#inewsMain").css("display","none");
	}else{
		
		$("#categoryMain").css("display","block");
		$("#highlightMain").css("display","none");
		$("#inewsMain").css("display","none");
		
	}
	
	setTimeout("getDivMediaBox2()",300);
	
}

function getDivMediaBox2(){
	
	var container = $( "#divMediaBox" );
	container.slideDown( 500 )
	
}
	
function categoryMenu(status,me,userId){
	
	$("#categoryContent").html("");
	
	document.getElementById("categoryMenu1").src = "firstpage/images/inews-menu/btn-economy1.png";
	document.getElementById("categoryMenu2").src = "firstpage/images/inews-menu/btn-politics1.png";
	document.getElementById("categoryMenu3").src = "firstpage/images/inews-menu/btn-foreign1.png";
	document.getElementById("categoryMenu4").src = "firstpage/images/inews-menu/btn-crame1.png";
	document.getElementById("categoryMenu5").src = "firstpage/images/inews-menu/btn-sport1.png";
	document.getElementById("categoryMenu6").src = "firstpage/images/inews-menu/btn-region1.png";
	document.getElementById("categoryMenu7").src = "firstpage/images/inews-menu/btn-socity1.png";
	document.getElementById("categoryMenu8").src = "firstpage/images/inews-menu/btn-entertain1.png";
	
	if(status == 1){
		document.getElementById("categoryMenu1").src = "firstpage/images/inews-menu/btn-economy2.png";
	}else if(status == 2){
		document.getElementById("categoryMenu2").src = "firstpage/images/inews-menu/btn-politics2.png";
	}else if(status == 3){
		document.getElementById("categoryMenu3").src = "firstpage/images/inews-menu/btn-foreign2.png";
	}else if(status == 4){
		document.getElementById("categoryMenu4").src = "firstpage/images/inews-menu/btn-crame2.png";
	}else if(status == 5){
		document.getElementById("categoryMenu5").src = "firstpage/images/inews-menu/btn-sport2.png";
	}else if(status == 6){
		document.getElementById("categoryMenu6").src = "firstpage/images/inews-menu/btn-region2.png";
	}else if(status == 7){
		document.getElementById("categoryMenu7").src = "firstpage/images/inews-menu/btn-socity2.png";
	}else if(status == 8){
		document.getElementById("categoryMenu8").src = "firstpage/images/inews-menu/btn-entertain2.png";
	}
	
	$.post("firstpage/mediabox/get-category.php?userId="+userId+"&statusSending=1",function(data){
		$("#categoryContent").html(data);	
	
	});
	
}

function getCategory(userId,pageNow){
	
	var IeCatch = Math.random();
	
	$.post("firstpage/mediabox/get-category.php?userId="+userId+"&statusSending=1&pageNow="+pageNow+"&IeCatch="+IeCatch,function(data){
		$("#categoryContent").html(data);		
	});
}

function activityBanner(state){
	
	if(state){
		$("#activityOuter .bx-next").css("visibility","hidden");
		$("#activityOuter .bx-prev").css("visibility","hidden");
	}else{
		$("#activityOuter .bx-next").css("visibility","visible");
		$("#activityOuter .bx-prev").css("visibility","visible");
	}
	
}

function btnVdoPlayChange(me,status){
	
	if(status == 1)
		document.getElementById(me).src = "http://www.my3space.com/firstpage/images/btn-vdo-play-over.png";
	else
		document.getElementById(me).src = "http://www.my3space.com/firstpage/images/btn-vdo-play.png";
}

function btnVdoPlayChange2(me,status){
	
	if(status == 1)
		me.src = "http://www.my3space.com/firstpage/images/btn-vdo-play-over.png";
	else
		me.src = "http://www.my3space.com/firstpage/images/btn-vdo-play.png";
}
