$(document).ready(function(){
	/**
	*	TOP MENU
	**/
	$('#top-logo li').hover(function(){
		$('#top-logo').attr('class','top-logo'+$(this).attr('class'));
	},
	function(){
		$('#top-logo').attr('class','');
	});
	
	/**
	*	MENU UTAMA
	**/
	$('#menu-utama li').hover(function(){
		if ($(this).attr('class')=='backhome') return false;
		try{
			var _img = $('img',$(this)).attr('src');
			var img = _img.split('.png');
			
			$('img',$(this)).attr('src',img[0]+'-hover.png');
			
			
			var k = 0;
			$(this).find('ul li').each(function(){
			    k=k+1;
			});
			var h = k*31;
			$(this).find('ul').stop().animate({height: h+'px', opacity:'0.8'},{queue:false, duration:500, easing: 'easeInSine'});
			
		}catch(e){}
	},
	function(){
		try{
			var _img = $('img',$(this)).attr('src');
			
			var img = _img.replace('-hover','');
			$('img',$(this)).attr('src',img);
			
			$(this).find('ul').stop().animate({height:'0px', opacity:'0'},{queue:false, duration:1000, easing: 'easeInSine'});
			
		}catch(e){}
	});

	/**
	*	HOME HOVER
	**/
	$('#ichome').hover(function(){
		$(this).attr('src','templates/images/top-icon-hover.png');
	},
	function(){
		$(this).attr('src','templates/images/top-icon.png');
	});
	
	/**
	*	MENU FOLLOW US
	**/
	$('#menu-utama-follow li').hover(function(){
		
		var _img = $('img',$(this)).attr('src');
		var img = _img.split('.png');

		$('img',$(this)).attr('src',img[0]+'-hover.png');
	},
	function(){
		var _img = $('img',$(this)).attr('src');
		var img = _img.replace('-hover','');
		$('img',$(this)).attr('src',img);
	});
	
  $('#slideContent').cycle({
      fx:      'scrollLeft',
      timeout:  0,
      prev:    '#prev',
      next:    '#next'
  });

  $('#slideImage').cycle({
      fx:      'turnDown',
      timeout:  0,
      prev:    '#prev',
      next:    '#next'
  });
  
  
  $('#comments input').corner('5px');
  $('#comments textarea').corner('5px');
  
  $('#comments').submit(function(){
  	$('#comments .submit').val('Sending your comments...');
		$.ajax({
				type:'POST',
				url : "index.php",
				data : "page=news&act=comments&"+$(this).serialize(),
				dataType : 'json',
				success : function(resp){
						if (resp.status=='success')
						{
							
						}
						else
						{
							alert(resp.msg);
							$("#prefix").val(resp.prefix);
							$("#imgcaptcha").attr('src',resp.newcaptcha);
						}
						$('#comments .submit').val('Send');
				},
				error : function(resp){alert("Oops, there's been an error.");}
			});
  	return false;
  });
  
  
  /*GALLERY*/
  
  $('#gallery li').hover(function(){
  	$(this).find('.title').css({color:'#FFFFCC'});
  },
  function(){
  	$(this).find('.title').css({color:'#CCC'});
  });
  
 	$(function() {
		$('#gallery .linkthumb').lightBox();
	});

	//WTS
	$('#btsay').click(function(){
		$('#content-wts').stop().animate({height:'0', opacity:'0'},{queue:false, duration:500, easing: 'easeInSine'});
		$('#form-wts').stop().animate({height:'120px', opacity:'10'},{queue:false, duration:500, easing: 'easeOutSine'});
		$('#wts-button').show();
		$('#wts-info').hide();
	});
	
	$('#wts-cancel').click(function(){
		$('#content-wts').stop().animate({height:'120px', opacity:'10'},{queue:false, duration:500, easing: 'easeInSine'});
		$('#form-wts').stop().animate({height:'0', opacity:'0'},{queue:false, duration:500, easing: 'easeOutSine'});
		$('#wts-button').hide();
		$('#wts-info').show();
	});
	
	$('#form-wts').css({height:'0'});
	$('#wts-button').hide();

	$("#form-wts input[name='val_name']").click(function(){
		if ($(this).val()=='Your Name'){$(this).val('')}
	});
	$("#form-wts input[name='val_name']").blur(function(){
		if ($(this).val()==''){$(this).val('Your Name')}
	});
	
	$("#form-wts input[name='val_email']").click(function(){
		if ($(this).val()=='Your Email'){$(this).val('')}
	});
	$("#form-wts input[name='val_email']").blur(function(){
		if ($(this).val()==''){$(this).val('Your Email')}
	});
	
	$("#form-wts textarea[name='val_message']").click(function(){
		if ($(this).text()=='Your Message'){$(this).text('')}
	});
	
	$("#form-wts textarea[name='val_message']").blur(function(){
		if ($(this).text()==''){$(this).text('Your Message')}
	});

  $('#wts-send').click(function(){
  	var sbmit = $(this); 
  	sbmit.text('Sending...');
		$.ajax({
				type:'POST',
				url : "index.php",
				data : "page=what_they_say&act=comments&"+$('#form-wts form').serialize(),
				dataType : 'json',
				success : function(resp){
						if (resp.status=='success')
						{
							$('#wts-cancel').click();
							clearFormWTS();
						}
						else
						{
							alert(resp.msg);
						}
						sbmit.text('Send');
				},
				error : function(resp){alert("Oops, there's been an error.");}
			});
  	return false;
  });
  
  function clearFormWTS()
  {
			$("#form-wts input[name='val_name']").val('Your Name');
			$("#form-wts input[name='val_email']").val('Your Email');
			$("#form-wts textarea[name='val_message']").text('Your Message');
  }
  
  /*
  	QUIZZZZ
  */
  $('#take-quiz').click(function(){
  	$('#dialog-quiz').remove();
  	$('body').append('<div id="dialog-quiz" title="Form Quiz">&nbsp;</div>');
  	
		$.ajax({
				type:'GET',
				url : "index.php",
				data : "page=quiz&cmd=getform",
				success : function(resp){
						
						$("#dialog-quiz").html(resp);
						
						$("#dialog-quiz").dialog({
							resizable: true,
							width:600,
							height:500,
							modal: true,
							overlay: {
								backgroundColor: '#000',
								opacity: 0.8
							},
							buttons: {
								'Send': function() {
									var popup=$(this);
									//posting ajax
									$.ajax({
											type:'POST',
											url : "index.php",
											data : "page=quiz&act=send&"+$('#dialog-quiz form').serialize(),
											dataType : 'json',
											success : function(resp){
													if (resp.status=='success')
													{
														alert(resp.msg);
														popup.dialog('close');
													}
													else
													{
														$("#prefix").val(resp.prefix);
														$("#imgcaptcha").attr('src',resp.newcaptcha);
														alert(resp.msg);
													}
											},
											error : function(resp){alert("Oops, there's been an error.");}
										});
									//end posting ajax
								},
								Cancel: function() {
									$(this).dialog('close');
								}
							}
						});
						//end dialog
				},
				error : function(resp){alert("Oops, there's been an error.");}
			});
			//end ajax get form quiz
  });
  //end click popup form quiz
  
  
  $('#lihat-ketentuan').click(function(){
  	$('#dialog-quiz').remove();
  	$('body').append('<div id="dialog-quiz" title="Ketentuan Quiz">&nbsp;</div>');
  	
		$.ajax({
				type:'GET',
				url : "index.php",
				data : "page=quiz&cmd=ketentuan",
				success : function(resp){
						
						$("#dialog-quiz").html(resp);
						
						$("#dialog-quiz").dialog({
							resizable: true,
							width:500,
							height:'auto',
							modal: true,
							overlay: {
								backgroundColor: '#000',
								opacity: 0.8
							},
							buttons: {
								Close: function() {
									$(this).dialog('close');
								}
							}
						});
						//end dialog
				},
				error : function(resp){alert("Oops, there's been an error.");}
			});
  });
  //end click popup ketentuan quiz
  
  $('#lihat-pemenang').click(function(){
  	$('#dialog-quiz').remove();
  	$('body').append('<div id="dialog-quiz" title="Pemenang Quiz">&nbsp;</div>');
  	
		$.ajax({
				type:'GET',
				url : "index.php",
				data : "page=quiz&cmd=pemenang",
				success : function(resp){
						
						$("#dialog-quiz").html(resp);
						
						$("#dialog-quiz").dialog({
							resizable: true,
							width:500,
							height:'auto',
							modal: true,
							overlay: {
								backgroundColor: '#000',
								opacity: 0.8
							},
							buttons: {
								Close: function() {
									$(this).dialog('close');
								}
							}
						});
						//end dialog
				},
				error : function(resp){alert("Oops, there's been an error.");}
			});
  });
  //end click popup lihat-pemenang
  
  
  /*
  gallery 
  */
  
  $('#gallery .linkmusic').click(function()
  {
		  $('#gallery .player').each(function(){
		  	$(this).html('<div id="player_'+$(this).attr('gid')+'"></div>');
		  });
  	
  		var _obj_player = $('.player',$(this).parent());
  		var obj_player = $('div',_obj_player).attr('id');
  		var obj_title = $(this).attr('judul');
  		
			AudioPlayer.setup(Web_Base_URL+"/javascripts/players/audioplayer/player.swf", {  
			   width: 180
			}); 

			AudioPlayer.embed(obj_player, {  
			   soundFile: $(this).attr('uri'),  
			   titles: obj_title, 
			   transparentpagebg: "yes",   
			   autostart: "yes"  
			}); 
		
  });
  
  $('#gallery .linkvideo').click(function()
  {
  		$('#boxvideoplayer').remove();
  		$('body').append("<div id='boxvideoplayer' title='"+$(this).attr('judul')+"'><div id='videoplayer'></div></div>");
  		
	    flashembed("videoplayer", 
			{
				src: Web_Base_URL+'/javascripts/players/videoplayer/FlowPlayerDark.swf',
				width: 470,
				height: 280,
				wmode: "transparent"
			},
			{
				config: {
				videoFile: $(this).attr('uri'),
				showMenu:false,
				autoPlay: false,
				autoBuffering: false,
				initialScale: "scale"
				}
		  }
		);
		
		$("#boxvideoplayer").dialog({
			resizable: true,
			width:500,
			height:'auto',
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.8
			},
			buttons: {
				Close: function() {
					$(this).dialog('close');
				}
			}
		});
  });
  
  /*
  end gallery
  */
  
  /*
  footer
  */
  $('#menu-utama-follow .invite').click(function(){
		inviteFriend();
  });
  
  
  $('#menu-utama-follow .subscribe').click(function(){
		subscribeEmail();
  });
  
  /**
  CART
  **/
  reloadCountOfCart();
  $('#count_cart').click(function(){
  	location.href = Web_Base_URL +'?page=cart';
  });
  
  $('#gallery .add2cart').each(function(){
	  $(this).click(function(){
	  	addToCart($(this).attr('pid'),1);
	  });
  });
  
  $('.rthumb').corner('4px');
  
  /*checkbox selected*/
  $('table.data thead input:checkbox').click(function(){
        var obj=$(this);
        $('table.data tbody input:checkbox').each(function(){
              $(this).attr('checked',obj.attr('checked'));
              //trChecked($(this));
        });
  });
  
  $('#cartdelete').click(function(){
	  var pid = Array();var k = 0;
	  $('table.data tbody input:checkbox').each(function(){
	  			
	        if ($(this).attr('checked')==true)
	        {
	       		pid[k] = $(this).val();
	       		k=k+1;
	        }
	  });
	  
	  if (pid==''){alert('Please select your cart.'); return false;}
	  deleteCart(pid);
  });
  
  $('#cartsave').click(function(){
	  var pid = Array();
	  var count = Array();
	  var k = 0;
	  $('table.data tbody .count-text').each(function(){
     		pid[k] = $(this).attr('pid');
     		count[k] = $(this).val();
     		k=k+1;
	  });
	  
	  if (pid==''){alert('Please select your cart.'); return false;}
	  addMore(pid, count);
  });
  
  $('#chkout').click(function(){
  		checkOut();
  });
  
  $('#cshopping').click(function(){
  		location.href= Web_Base_URL +'?page=gallery&key=4ad4d364c1889942be9b6e391b7f98f0';
  });
  
  $('#frmcontact').submit(function(){
  	sendContact();
  	return false;
  });
});

/***
FUNCTION
****/

function sendContact()
{
	$.ajax({
		beforeSend:function() {
			$("#send_contact").hide();
			$(".alert").show();
			$(".alert").html('Sending mail, please wait...');
		},
		type:'POST',
		url:"index.php",
		data:"page=contact_us&act=contact&"+$("#frmcontact").serialize(),
		dataType:'json',
		success:function(data) 
		{
			$(".alert").html('&nbsp;'); $("#send_contact").css({display:"block"});
			if (data.status=='success'){
				$("#frmcontact").html("Thank you, your message has been sent!.");
			}
			else if (data.status=='txfailed')
			{
				alert(data.msg);
				$("#prefix").val(data.prefix);
				$("#imgcaptcha").attr('src',data.newcaptcha);
			}
			else
			{
				alert(data.msg);
				$("#send_contact").show();
				$(".alert").hide();
			}
		},
		error:function() {
			alert("Error: Failed please try again.");
		}
	});
}

function inviteFriend()
{
	var htm="";
	for (k=0;k<5;k++){
		htm=htm + addNewName();
	}
	
	html='<form id="tell_friend" style="border;1px solid"><table id="tbltell_friend">';
	html=html+'<tr><td>';
	html=html+'<input type="hidden" name="val_url" value="" />';
	html=html+'Your Name <br> <input type="text" name="val_sender_name" /> <br><br>';
	html=html+'Your Email <br> <input type="text" name="val_sender_email" /> <br><br>';
	html=html+'<h3>Your friends : </h3>';
	html=html+'</td></tr>';
	html=html+'<tr><td align="center">Name</td>';
	html=html+'<td align="center">Email</td></tr>';
	html=html+htm;
	html=html+'</table>';
	html=html+'</form>'
	
	
		$('#invite-friend').remove();
		$('body').append("<div id='invite-friend' title='Invite Friend'></div>");
		$("#invite-friend").html(html);
		$("#invite-friend").dialog({
				resizable: true,
				width:370,
				height:'auto',
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.8
				},
				buttons: {
					Ok : function(){
						$.ajax({
							beforeSend:function() {
								$(".alert").html('Sending mail, please wait...');
							},
							type:'POST',
							url:"index.php",
							data:"page=contact_us&act=tellfriend&"+$("#tell_friend").serialize(),
							dataType:'json',
							success:function(data) 
							{
								if (data.status=='success'){
									alert(data.msg);
								}else{
									alert(data.msg);
								}
							},
							error:function() {
								alert("Error: Invite friend problem, please try again.");
							}
						});
					},
					Close: function() {
						$(this).dialog('close');
					}
				}
			});
}

function addNewName(aft)
{
	var html="";
	
	html=html+'<tr>';
	html=html+'<td><input type="text" name="val_name[]" /></td>';
	html=html+'<td><input type="text" name="val_email[]" /></td>';
	//html=html+'<td><a href="javascript:" onclick="$(this).parent().parent().remove()">del</a></td>';
	html=html+'</tr>';
	
	if (aft=="after"){
		$('#tbltell_friend').append(html);
	}else{
		return html;
	}
}

function subscribeEmail()
{
	$.ajax({
		beforeSend:function() {
			
		},
		type:'GET',
		url:"index.php?page=subscribe&cmd=list",
		dataType:'json',
		success:function(data) 
		{
			html='<form id="frmsubscribe" style="border;1px solid"><table id="tbltell_friend" cellspacing="3">';
			html=html+'<tr><td>Get Catatan Si Boy updates to your email. </td></tr>';
			html=html+'<tr><td>Your Email : <input type="text" name="val_email" value="" /></td></tr>';
			html=html+'<tr><td>Select the following type of Catatan Si Boy updates : </td></tr>';
			html=html+'<tr><td><div id="list-modules">'+data.modules+'</div></td></tr>';
			html=html+'</table>';
			html=html+'</form>';
			
			$('#subscribe').remove();
			$('body').append("<div id='subscribe' title='Email Updates'></div>");
			$('#subscribe').html(html);
			$("#subscribe").dialog({
				resizable: true,
				width:500,
				height:'auto',
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.8
				},
				buttons: {
					Ok : function(){
						$.ajax({
							beforeSend:function() {
								$(".alert").html('Sending mail, please wait...');
							},
							type:'POST',
							url:"index.php",
							data:"page=subscribe&act=subscribe&"+$("#frmsubscribe").serialize(),
							dataType:'json',
							success:function(data) 
							{
								if (data.status=='success'){
									alert(data.msg);
								}else{
									alert(data.msg);
								}
							},
							error:function() {
								alert("Error: subscribe problem, please try again.");
							}
						});
					},
					Close: function() {
						$(this).dialog('close');
					}
				}
			});

		},
		error:function() {
			alert("Error: Failed please try again.");
		}
	});
}


function addToCart(id,count)
{
	$.ajax({
		type:'POST',
		url : "index.php",
		data : "page=cart&act=addtocart&pid="+id+"&count="+count,
		dataType : 'json',
		success : function(resp){
				if (resp.status=='success')
				{
					alert("Thanks you, this product has been added to your cart.");
					reloadCountOfCart();
				}
				else
				{
					if (resp.statmod=='nologin')
					{
						location.href = "?page=member&show=register";
					}
				}

		},
		error : function(resp){alert("Oops, there's been an error.");}
	});
}

function reloadCountOfCart()
{
	//$("#cart-action-info").html('Refresh your cart, please wait...');
	$.ajax({
		type:'get',
		url : "index.php",
		data : "page=cart&view=count",
		dataType : 'json',
		success : function(resp){
				$('#count_cart span').html(resp.msg);
		},
		error : function(resp){alert("Oops, there's been an error.");}
	});
}

function addMore(id,count)
{
	$("#cart-action-info").html('Saving your cart, please wait...');
	$.ajax({
		type : 'post',
		url : "index.php",
		data : "page=cart&act=addMore&pid="+id+'&count='+count,
		dataType : 'json',
		success : function(resp){
				$("#cart-action-info").html(resp.msg);
				location.href = Web_Base_URL +'?page=cart';
			},
		error : function(resp){alert("Oops, there's been an error.");}
	});
}

function deleteCart(id)
{
	$("#cart-action-info").html('Deleting your cart, please wait...');
	$.ajax({
		type : 'post',
		url : "index.php",
		dataType : 'json',
		data : "page=cart&act=delcart&pid="+id,
		success : function(resp){
				$("#cart-action-info").html(resp.msg);
				location.href = Web_Base_URL +'?page=cart';
			},
		error : function(resp){alert("Oops, there's been an error.");}
	});
}

function checkOut()
{
	$("#cart-action-info").html('Checkout your cart, please wait...');

	$.ajax({
		type : 'post',
		url : "index.php",
		data : "page=cart&act=checkout&"+$('#formcheckout').serialize(),
		dataType : 'json',
		success : function(resp){
			if (resp.status=='txfailed')
			{
					$("#prefix").val(resp.prefix);
					$("#imgcaptcha").attr('src',resp.newcaptcha);
					alert(resp.msg);
			}
			else if (resp.status=='failed')
			{
				alert(resp.msg);
			}
			else
			{
				alert(resp.msg);
				location.href = Web_Base_URL;
			}
			},
		error : function(resp){alert("Oops, there's been an error.");}
	});
}
