$(document).ready(function(){
	//preload image
	pre_load_image('images/hehe_05.gif');
	pre_load_image('images/hehe_06.gif');
	pre_load_image('images/hehe_07.gif');
	pre_load_image('images/hehe_08.gif');
	pre_load_image('images/hehe_09.gif');
	pre_load_image('images/hehe_10.gif');
	pre_load_image('images/hehe_11.gif');
	pre_load_image('images/hehe_12.gif');
	
	
	// hili menu
	var counter = 5;
	
  $('#menu_div').find("a").each(function()
  {
	  var mySelected = $(this).attr("selected");
	  
	  if (mySelected == 'true')
	  {
	  	 if (counter < 10)
	  	 {
	  	 	 counter = '0' + counter;
	  	 }
	  	
       imgSrc = 'url(images/hehe_' + counter + '.gif)';
       
       $(this).css({"background-image" : imgSrc});
	  }
	
	  counter += 1;
	
	});

  // menu onClick
  $('#menu_div a').click(function(){
  	                                      var foo = $(this).attr('link');
  	                                      window.location = foo;
  	                               });
  	                               
  // centering the 'fixed_div'
  // 1. get bodyWidth:
  
  myBodyWidth = document.documentElement.scrollWidth;
  
  myMarginLeft = ((myBodyWidth / 2) - 370) + 'px'
  
  $('#fixed_div').css({'margin-left' : myMarginLeft});
});




function redirect(myURL)
{
	window.location = myURL;
}

function phanHoiOk()
{
	var myConfirm = confirm('Gửi phản hồi ?');
	if (myConfirm)
	{
		yourName = $('#yourName').attr('value');
		yourComment = $('#yourComment').attr('value');
		
		$.ajax({
			type: "POST",
			url: "index.php?m=ajax&a=sendphanhoi",
			data: {"yourName": yourName,
			       "yourComment":yourComment
			      },
			success: function(msg)
			         {
									$('#phanhoi').slideUp(function(){
										alert('Đã gửi phản hồi của bạn');
									});
					      }
			});
	}
}

function phanHoi()
{
	$('#phanhoi').slideDown();
	$('#chiase').slideUp();
}
function chiase()
{
	$('#chiase').slideDown();
	$('#phanhoi').slideUp();
  $('#chiase input').attr({'value' : ''});
}

function chiaseOk()
{
	$('#chiase').slideUp(function(){
		alert('Email đã được gửi. Cám ơn bạn');
	});
}

function pre_load_image(image_url)
{
  $('<img />').attr('src', image_url);
}

function open_image(imageURL , imageTYPE)
{
	if (imageTYPE == '1')
	{
	  myURL = 'uploads/' + imageURL;
  }
  else
  {
  	myURL = imageURL;
  }
	
	// alert(myURL);
	
	$('<img />')
	    .attr('src', randomUrl(myURL))
	    .load(function(){
		    $('#fixed_div').fadeIn('slow');
		    
		    $('#main_img').attr({"src" : myURL});
	    
	    });
}

function myClose()
{
	 $('#fixed_div').fadeOut('fast');
	 $('#fixed_div').css({"display" : "none"});
	 $('#myYouTube').html('');
}

function randomUrl(myURL)
{
						var fetch_unix_timestamp = function()
						{
						return parseInt(new Date().getTime().toString().substring(0, 10))
						};
						
						var randomnumber = fetch_unix_timestamp();
						var aurl= myURL + '?p=';
						var ranurl= aurl + randomnumber;
						return ranurl;	
}
 
function open_video(videoCODE)
{
	$('#fixed_div').fadeIn('slow');
  var myHTML = '<param name="movie" value="http://www.youtube.com/v/' + videoCODE + '&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/' + videoCODE + '&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="304"></embed>';
  $('#myYouTube').html(myHTML);
}

function submit_contact_form()
{
	myname = $('#name').attr('value');
	dob = $('#dob').attr('value');
	sex = $('#sex').attr('value');
	address = $('#address').attr('value');
	email = $('#email').attr('value');
	phone = $('#phone').attr('value');
	fax = $('#fax').attr('value');
	noidung = $('#noidung').attr('value');	
	
	if ((myname == '') || (address == '') || (email == '') || (noidung == ''))
	{
		alert('Bạn cần nhập đầy đủ nội dung bắt buộc');
	}
  else
  {
		$.ajax({
			type: "POST",
			url: "index.php?m=ajax&a=submitform",
			data: {
				      "myname" : myname,
				      "dob" : dob,
				      "sex" : sex,
				      "address" : address,
				      "email" : email,
				      "phone" : phone,
				      "fax" : fax,
				      "noidung" : noidung
			      },
			success: function(msg)
			          {
						      alert( "Ðã gửi yêu cầu của bạn");
						      window.location = 'index.php?m=tuvan';
					      }
			});
	 }
}

