// AJAX INIT
var mail_sent = false;
var cur_choice = -1;

function $$$(id) {
	return document.getElementById(id);
}

function khoitao_ajax()
{
	var x;
	try 
	{
		x	=	new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
    	try 
		{
			x	=	new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(f) { x	=	null; }
  	}

	if	((!x)&&(typeof XMLHttpRequest!="undefined"))

	{

		x=new XMLHttpRequest();

  	}

	return  x;

}

function	Forward(url)
{
	window.location.href = url;
}

function	_postback()
{
	return void(1);
}

/*So sanh height cua 2 the div tgp_body_content & tgp_right*/

function EqualHeight(elements) { 

    //Xác định chiều cao của cột cao nhất 

    tallest = 0; 

    elements.each(function() { 

        elementHeight = jQuery(this).height(); 

        if(elementHeight > tallest) { 

            tallest = elementHeight; 

        } 

    }); 

    //Chỉnh chiều cao cho tất cả các cột 

    //theo chiều cao của cột cao nhất 

    elements.height(tallest); 

} 

function ajax_newsletter(frm_newsletter)
{
	email=frm_newsletter.txt_email.value;
	if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/))
	{
		alert('Invalid email addresss.');
		document.frm_newsletter.txt_email.focus();
		return false;
	}

	var	query	=	"txt_email="+email;
	var http 	=	khoitao_ajax();
	try
	{
		http.open("POST", "/email_reg.php");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					if (x != "OK")
					{
						alert(x);
					}
					else
					{
						alert("Your email has been added to the system successfully.");
					}
				}
				else
				{
					alert('System failure. Please try again later');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}

function send_email_lien_he()
{
	$("#frm_lh_loading").css({"display":"block"});
	if (mail_sent == false)
	{
		email=document.getElementById("txt_email").value;
		name=document.getElementById("txt_name").value;
		subject=document.getElementById("txt_subject").value;
		msg=document.getElementById("txt_msg").value;
		$("#frm_lh_err").css({"display":"none"});
		if (name == "")
		{
			$("#frm_lh_err").css({"display":"block"});
			$("#frm_lh_err").text("You may enter name!");
			$("#frm_lh_loading").css({"display":"none"});
			return false;
		}
		if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/))
		{
			$("#frm_lh_err").css({"display":"block"});
			$("#frm_lh_err").text("Invalid email address!");
			$("#frm_lh_loading").css({"display":"none"});
			return false;
		}
		if (subject == "")
		{
			$("#frm_lh_err").css({"display":"block"});
			$("#frm_lh_err").text("You may enter the title!");
			$("#frm_lh_loading").css({"display":"none"});
			return false;
		}
		if (msg == "")
		{
			$("#frm_lh_err").css({"display":"block"});
			$("#frm_lh_err").text("You may enter text!");
			$("#frm_lh_loading").css({"display":"none"});
			return false;
		}
		
		var	query	=	"name="+name+"&email="+email+"&subject="+subject+"&msg="+msg;
		var http 	=	khoitao_ajax();
		try
		{
			http.open("POST", "/email_lien_he.php");
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			http.setRequestHeader("Cache-control", "no-cache");		
			http.onreadystatechange = function()
			{
				if (http.readyState == 4)
				{
					if (http.status == 200)
					{
						x = http.responseText;
						if (x != "OK")
						{
							$("#frm_lh_err").css({"display":"block"});
							$("#frm_lh_err").text(x);
							$("#frm_lh_loading").css({"display":"none"});
							
						}
						else
						{
							$(".form_lh").css({"text-align":"center"});
							$(".form_lh").html("<div style='color:#FFF; padding-left:50px; padding-right:50px; font-family:Tahoma; font-weight:normal; font-size:12px; padding-top:80px;'><b>Thanks you for your comment to us</b><br/>We look forward to receiving more comments from you to the Fireworks site more better.</font>");
							mail_sent = true;
						}
					}
					else
					{
						alert('System failure. Please try again later!');
						$("#frm_lh_loading").css({"display":"none"});
					}
				}
			}
			http.send(query);
		}
		catch (e)
		{
		}
		return false;
	}
	else
	{
		return false;
	}
}

function comment(frm)

{

	id = frm.id.value

	txt_ten = frm.txt_ten.value

	txt_email = frm.txt_email.value

	if(!txt_email)

	{

		alert("Please enter your email!");

		frm.txt_email.focus();

		return false;

	}

	email= frm.txt_email.value

	if (!email.match(/^([-\d\w][-.\d\w]*)?[-\d\w]@([-\w\d]+\.)+[a-zA-Z]{2,6}$/)){

		alert('Invalid email address.');

		frm.txt_email.focus();

		return false;

	}

	txt_noi_dung = frm.txt_noi_dung.value

	if(!txt_email)

	{

		alert("Please enter content!");

		frm.txt_noi_dung.focus();

		return false;

	}

	else

	{

		var	query	=	"act=comment&txt_ten="+txt_ten+"&txt_email="+txt_email+"&txt_noi_dung="+txt_noi_dung+"&id="+id;

		var http 	=	khoitao_ajax();

		try

		{

			$$$('comment').innerHTML = "<center><img src='/images/ajax-loader.gif' /></center>";

			http.open("POST", "/action.php");

			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

			http.setRequestHeader("Cache-control", "no-cache");		

			http.onreadystatechange = function()

			{

				if (http.readyState == 4)

				{

					if (http.status == 200)

					{

						$$$('comment').innerHTML = http.responseText;

					}

					else

					{

						alert('System failure. Please try again later!');

					}

				}

			}

			http.send(query);

		}

		catch (e)

		{

		}

		return false;

	}

}
function hinh_truoc(cat,ten,number,id)
{

	var	query	=	"act=hinh_truoc&i="+id+"&cat="+cat+"&ten="+ten+"&so_hinh="+number;

	var http 	=	khoitao_ajax();

	try

	{

		$$$('hinh_album').innerHTML = "<center style = 'padding-top:10px'><img src='/images/ajax-loader.gif' /></center>";

		http.open("POST", "/action.php");

		

		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		http.setRequestHeader("Cache-control", "no-cache");		

    	http.onreadystatechange = function()

		{

			if (http.readyState == 4)

			{

				if (http.status == 200)

				{

   			
					

					$$$('hinh_album').innerHTML = http.responseText;
					if ($(".hinh_xem img").width() > 710)
						$(".hinh_xem img").css({"width":"710px"});
				}

				else

				{

					alert('System failure. Please try again later!');

				}

			}

		}

		http.send(query);

	}

	catch (e)

	{

	}

	return false;

}
function loading_ctr()
{
	var	query	=	"act=loading_country";
	var http 	=	khoitao_ajax();
	try
	{
		$$$('ctr').innerHTML = "<center style='margin-right:0px;padding-top:40px'><img src='/images/ajax-loader.gif' /> <span style='font-weight:11px'>Data processing... </center>";
		$("#ctr").css({"display":"block"});
		http.open("POST", "/action.php");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					
					
					
					$$$('ctr').innerHTML = http.responseText;
					Cufon.replace('.tgp_country .title', {fontFamily: 'Myriad Pro', color: '#ffd800'});
					$(".lst_country li img").tipsy();
				}
				else
				{
					//alert('Co loi he thong. Vui long thu lai sau.');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}
function vote()
{
	var	query	=	"doi="+cur_choice;
	var http 	=	khoitao_ajax();
	try
	{
		http.open("POST", "/vote.php");
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Cache-control", "no-cache");		
    	http.onreadystatechange = function()
		{
			if (http.readyState == 4)
			{
				if (http.status == 200)
				{
					x = http.responseText;
					$("#vote_area").html(x);
					Cufon.replace('.tgp_body .area_2 .sub_area_1 .news_area_3 .news .news-text .rate_box .right .name', {fontFamily: 'Myriad Pro', color: '-linear-gradient(#fdca32, #efb70c)', textShadow: '-1px 1px 1px rgba(51, 51, 51, 0.6)'});
				}
				else
				{
					alert('System failure. Please try again later!');
				}
			}
		}
		http.send(query);
	}
	catch (e)
	{
	}
	return false;
}

var act_value = "";
var page = 0;
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var count_str = "";
function countdown(yr,m,d){
	theyear=yr;themonth=m;theday=d
	var today=new Date()
	var todayy=today.getYear()
	if (todayy < 1000)
	todayy+=1900
	var todaym=today.getMonth()
	var todayd=today.getDate()
	var todayh=today.getHours()
	var todaymin=today.getMinutes()
	var todaysec=today.getSeconds()
	var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
	futurestring=montharray[m-1]+" "+d+", "+yr
	dd=Date.parse(futurestring)-Date.parse(todaystring)
	dday=Math.floor(dd/(60*60*1000*24)*1)
	dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
	dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
	dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
	if(dday==0&&dhour==0&&dmin==0&&dsec==1){
	count_str=current
	return 
	}
	else
	count_str=dday+"|"+dhour+"|"+dmin+"|"+dsec;
	//document.getElementById("days").innerHTML = dday;
	//document.getElementById("hours").innerHTML = (dhour > 9)?dhour:"0"+dhour;
	//document.getElementById("minutes").innerHTML = (dmin > 9)?dmin:"0"+dmin;
	//document.getElementById("seconds").innerHTML = (dsec > 9)?dsec:"0"+dsec;
	//setTimeout("countdown(theyear,themonth,theday)",1000)
}

jQuery(window).load(function(){ 

	$(".gal_box ul li").rotator({ms : 2000 + Math.floor(Math.random()*101)});
	// Kiểm tra IE

	function isIE()
	{
	  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
	}

	act_value = document.getElementById("js_get_act").value;
 	Cufon.replace('.tgp_banner .div_2 h1', {fontFamily: 'Myriad Pro', color: '#FFF', hover:true,hoverables:{a:true}});
	Cufon.replace('.tgp_site_map .title', {fontFamily: 'Myriad Pro', color: '#dddddd'});
	Cufon.replace('.tgp_body .area_2 .sub_area_1 .news_area_3 .news .news-text .rate_box .right .name,.tgp_body .div_3 .box_1 ul li .name', {fontFamily: 'Myriad Pro', color: '-linear-gradient(#fdca32, #efb70c)', textShadow: '-1px 1px 1px rgba(51, 51, 51, 0.6)'});
	Cufon.replace('.tgp_body .area_1 .sub_area_1 .title,.signup_newsletter .title,.tgp_body .div_3 .box_5 .box .box_text .bt,.tgp_body .area_2 .sub_area_1 .news .news-title,.tgp_body .div_3 .box_3 .title , .tgp_logo_tt .title,.tgp_banner .div_2 h1.active, .tgp_country .title,.thao_luan .title_sub, .author,.tgp_body .div_3 .title_dichthuat', {fontFamily: 'Myriad Pro', color: '#ffd800'});
	Cufon.replace('.tgp_body .area_2 .sub_area_1 .bnews-title,.tgp_body .div_3 .box_3 .gal_title', {fontFamily: 'Myriad Pro', color: '#fff'});


	if(act_value == "home")
	{
		//countdown(2011,4,30);
	}
	$(".logo_slide").jCarouselLite({
        btnNext: ".btn_next",
        btnPrev: ".btn_prev",
		visible: 5
    });
	$(".logo_slide li a").tipsy();

	$("#btn_gopy").click(function(){
		mail_sent = false;
		$('.frm_contact').modal();
	});

	$("#btn_tp_dn").toggle(function(){
		$("#tp_dn").fadeOut(function(){
			$("#tp_dn").html(document.getElementById("da_nang_details").value);
	    });
		$("#tp_dn").fadeIn();
		$("#btn_tp_dn").html("Close");
	},
	function(){
		$("#tp_dn").fadeOut(function(){
			$("#tp_dn").html(document.getElementById("da_nang_home").value);
	    });
		$("#tp_dn").fadeIn();
		$("#btn_tp_dn").html("More");
	});

	$(".tgp_copyright .div_3").tipsy({gravity: 's', html:true, title:'ttin'});
	$(".tgp_banner .div_1 .box_1 a").tipsy();
	$(".lst_country li img").tipsy();
	
	$(".tin_nav a").corner("keep");

	if (!isIE())
	{
		// non-IE
		$(".thao_luan").corner("keep");
		$(".thao_luan .comment_box").corner("keep");
		$(".thao_luan .inputbox").corner("3px");
		$(".box_thao_luan .btn_gui").corner("3px");
		$(".box_thao_luan .btn_dong").corner("3px");
	}
	else
	{
		// IE
		$(".thao_luan .comment_box").corner("keep");
	}

	$("#comment").css({"display":"none"});

	$("#btn_add_cmnt").click(function(){
		if ($("#comment").css("display") == "none")
		{
			$("#comment").slideDown();
		}
		else
		{
			$("#comment").slideUp();
		}
	});

	$(".btn_dong").click(function(){
		$("#comment").slideUp();
	});
	
	$(".thu_vien_box .album_list .album a").hover(function(){
		$(this).find("img").stop().animate({borderTopColor: '#E37100', borderLeftColor: '#E37100', borderRightColor: '#E37100', borderBottomColor: '#E37100'},"fast");
	},
	function(){
		$(this).find("img").stop().animate({borderTopColor: '#3F3F3F', borderLeftColor: '#3F3F3F', borderRightColor: '#3F3F3F', borderBottomColor: '#3F3F3F'},"fast");
	});

	$(" .thu_vien_box .hinh_lst a").hover(function(){
		$(this).stop().animate({borderTopColor: '#ccc', borderLeftColor: '#ccc', borderRightColor: '#ccc', borderBottomColor: '#ccc'},"fast");
	},
	function(){
		$(this).stop().animate({borderTopColor: '#181818', borderLeftColor: '#181818', borderRightColor: '#181818', borderBottomColor: '#181818'},"fast");
	});

	$(".thu_vien_box .hinh_lst a").tipsy();
	
	if (act_value == "home")
	{
		$(".vote_box .vote_item").hover(function(){
			$(this).stop().animate({"backgroundColor": '#E37100'},"fast");
		},
		function(){
			if (cur_choice != $(".vote_box .vote_item").index($(this)))
				$(this).stop().animate({"backgroundColor": '#0E0E0E'},"fast");
		});
		
		$(".vote_box .vote_item").click(function(){
			$(".vote_box .vote_item").removeClass("active");
			$(".vote_box .vote_item").css({"background":"#0E0E0E"});
			$(this).addClass("active");
			$(this).css({"background":"#E37100"});
			cur_choice = $(".vote_box .vote_item").index($(this));
		});
											
	}
	
	if ($(".hinh_xem img").width() > 710)
		$(".hinh_xem img").css({"width":"710px"});
	
	//$(".tgp_copyright .div_3").toggle(function(){
//		$(".tgp_country").fadeIn();
//	},
//	function(){
//		$(".tgp_country").fadeOut();
//	});
	
}); 
$(document).ready(function(){
		// Fix lỗi css trong IE
	//	if (isIE())
//		{
//			$("html").css("overflow","auto");
//			$("html body").css("overflow","hidden");
//		}

		$(".tgp_body .div_3 .box_2 .box .boxt_3 ul").jScrollPane({scrollbarWidth:4});
		$(".f_news_box .f_news_text img").hover(function(){
			$(this).stop().animate({borderTopColor: '#E37100', borderLeftColor: '#E37100', borderRightColor: '#E37100', borderBottomColor: '#E37100'},"fast");
		},
		function(){
			$(this).stop().animate({borderTopColor: '#3F3F3F', borderLeftColor: '#3F3F3F', borderRightColor: '#3F3F3F', borderBottomColor: '#3F3F3F'},"fast");
		});
		
		if ($(".cmt_wrapper").height() >= 250)
		{
			$(".cmt_wrapper").css({"height":"250px"});
			$(".cmt_wrapper").jScrollPane({scrollbarWidth:4});
		}
		
		$(".slider").jCarouselLite({
			//btnNext: ".next",
			//btnPrev: ".prev",
			auto: true,
			speed: 500, // Toc do chay hinh
			auto: 6000,// Thoi gian chay hinh
			visible: 1
		});
	});
