
function change_testi(id){
	var loader_img=base_url+'templates/images/ajax-loader.gif';	
	$(".senderName").hide();
	$(".twitFeed").hide();
	$("#img_loader").fadeIn();	
	
	$.ajax({
		type: "POST",
		url: testi_target_url,
		data: "id="+id,
		success: function(temp){
			data=temp.split('|',4);
			$("#img_loader").fadeOut(function(){
				$('.prevTwitBtn').attr('onclick','change_testi('+data[0]+');return false;');
				$('.nextTwitBtn').attr('onclick','change_testi('+data[3]+');return false;');
				$(".senderName").html(data[1]);	
				$(".twitFeed").html(data[2]);
				
				$(".senderName").fadeIn();	
				$(".twitFeed").fadeIn();			
			});		
		}
	});
}

function change_product(id,img_active,img_inactive){
	var loader_img=base_url+'templates/images/ajax-loader.gif';
	$("#content_prd").html('<img src="'+loader_img+'" />');
	var active_img_link=base_url+'userdata/products/thumbs/'+img_active;
	var inactive_img_link=base_url+'userdata/products/thumbs_inactive/'+img_inactive;
	
	$("#content_prd").load(targetUrl+'/'+id);
	/*$("ul#productsCarousel li").each(function (){
		$(this).find("img").attr("src",$(this).find("img").attr("rel"));
	})*/;
	$("ul#productsCarousel li img").each(function (){
		$(this).attr("src",$(this).attr("rel"));
	})
	$("#img_prd_"+id).attr("src",active_img_link);
}

function change_loc_img(id,name){
	var location_picture_url=base_url+'userdata/locationPhotos/';
	var loader_img=base_url+'templates/images/ajax-loader.gif';
	$("#picture_1 img").attr("src",loader_img);
	$("#picture_2 img").attr("src",loader_img);
	$("#picture_3 img").attr("src",loader_img);
	
	$("#contentScroll .locationItem .locName a").each(function (){
		$(this).removeClass('selected');
	})
	$.ajax({
		type: "POST",
		url: target_location_url,
		data: "id="+id,				  
		success: function(temp){
			$("a#location_id_"+id).addClass('selected');
			var data=temp.split('|',3);
			picture1=location_picture_url+data[0];
			picture2=location_picture_url+data[1];
			picture3=location_picture_url+data[2];
			$("#picture_1 img").attr("src",picture1);
			$("#picture_2 img").attr("src",picture2);
			$("#picture_3 img").attr("src",picture3);
			$("#picture_1 img").attr("alt",'Dapur Cokelat ['+name+']');
			$("#picture_2 img").attr("alt",'Dapur Cokelat ['+name+']');
			$("#picture_3 img").attr("alt",'Dapur Cokelat ['+name+']');
		}
	});		
}


function buy_now_init(){		
	$('#overlay').css('opacity', 0.7);
	$('#overlay').click(function(){
		$('#popupProducts').fadeOut();
		$('#overlay').fadeOut();
	});
	$('a#btnCancel').click(function(){
		$('#popupProducts').fadeOut();
		$('#overlay').fadeOut();
		return false;
	});
	$('a#btnBuyNow').click(function(){
		$('#overlay').fadeIn();
		$('#popupProducts').css('margin-top', -(($('#popupProducts').height())/2));
		$('#popupProducts').fadeIn();
	});	
}
function productsScrollInit(){
	$('#productsScroll').jScrollPane({
		verticalDragMinHeight: 5,
		verticalDragMaxHeight: 5,
	});
}

$(document).ready(function(){
	if($('.introMenu').length > 0){
		var obj = null;
		
		$('.introMenu ul > li').hover(function(){
			if(obj){
				obj.find('ul').fadeOut();
				obj = null;
			}
			$(this).find('ul').fadeIn();
		}, function() {
			obj = $(this);
			if (obj) {
				obj.find('ul').fadeOut();
			}
		});
	}
	
	if($('.locationSelectorBox').length > 0){
		$('.locationSelectorBox').hover(function(){
			$('ul.locationOptions').show();
		}, function(){
			$('ul.locationOptions').hide();
		});
	}
	
	if($('#homeSlide').length > 0){
		$(function(){
			$('#homeSlide').slides({
				preload: true,
				preloadImage: '/images/slides/loading.gif',
				play: 7000,
				pause: 4000,
				fadeSpeed: 1500,
				effect: 'fade',
				crossfade: true
			});
		});
	}
	
	if($('ul.newsPromoList').length > 0){
		$('ul.newsPromoList li a.selected').after('<div class="highlightCursor"></div>');
		$('ul.newsPromoList > li a').hover(function(){
			$(this).after('<div class="highlightCursor"></div>');
		}, function(){
			$('div.highlightCursor').remove();
			$('ul.newsPromoList li a.selected').after('<div class="highlightCursor"></div>');
		});
	}
	if($('ul.productsList').length > 0){
		$('ul.productsList li a.selected').after('<div class="highlightCursor"></div>');
		$('ul.productsList > li a').hover(function(){
			$(this).after('<div class="highlightCursor"></div>');
		}, function(){
			$('div.highlightCursor').remove();
			$('ul.productsList li a.selected').after('<div class="highlightCursor"></div>');
		});
	}
	if($('#contentProducts').length > 0){	
		buy_now_init();
	}
	if($('.productsCarousel').length > 0){
    	$('#productsCarousel').jcarousel();
	}
	if($('#productsScroll').length > 0){
		$(document).ready(function(){
			$('#productsScroll').jScrollPane({
				verticalDragMinHeight: 5,
				verticalDragMaxHeight: 5,
			});
		});
	}
	
	if($('#contentTestimonial').length > 0){
		$("#name").focus();
		$('#dob').datepicker({
			numberOfMonths: 1,
			showButtonPanel: true,
			yearRange: "-80:+80",
			changeYear: true,
			dateFormat: "yy-mm-dd",
			minDate: "-80y"
		});
		$("#twitBtn").click(function(){
			var current_page=$("#page").val();
			
			var target_url=base_url+'testimonial/next_testimonial';
			$.ajax({
				type: "POST",
				url: target_url,
				data: "current_page="+current_page,				  
				success: function(rs){	
					var temp=rs.split('|',2);
					$("#page").val(temp[1]);
					$("div.twitFeed p").html(temp[0]);
				}
			});		
			return false;
		});
		$("#testi_send_btn").click(function(){
			$('#form_testimonial').submit();
		});
	}
	
	if($('#contentScroll').length > 0){
		$(document).ready(function(){
			$('#contentScroll').jScrollPane({
				verticalDragMinHeight: 5,
				verticalDragMaxHeight: 5,
			});
		});
	}
	
	if($('#loginForm').length > 0){
		//type enter to submit
		function submitentry(field,e){
			var keycode;
			if (window.event) keycode = window.event.keyCode;
			else if (e) keycode = e.which;
			else return true;
			
			if (keycode == 13)   {
			   document.formLogin.submit();
			   return false;
			}
			else
			   return true;
		}
	}
	
	
	if($('#registerForm').length > 0){
		
		function enterform() {
			$(document).ready(function() {
				$("#formRegister").validate({
					rules : {
						fullname:{
							required:true
						},
						bdate:{
							required:true
						},
						city:{
							required:true
						},
						profession:{
							required:true
						},
						address:{
							required:true
						},
						zipcode:{
							required:true,
							number:true
						},
						phone:{
							required:true,
							number:true
						},
						email:{
							required:true,	
							email:true,			
							remote: {
								url: base_url+"registration/check_email",
								type: "post"
							}	
						},
						password:{
							required:true	
						},
						c_password:{
							required:true	,
							equalTo:"#password"
						}
					},		
					errorPlacement: function(error, element) {
						
					}
				});
				
				if($("#formRegister").validate().form() == true){
					$("#errorMsg").hide();
					$("#formRegister").submit();
					return true;	
				}
				else{
					$("#errorMsg").show();
					return false;
				}
			})
		}
		
		$(document).ready(function(){
			$("#registerBtn").click(function(){
			  return enterform();
			  return false;
			});
			$("#registerBtn2").click(function(){
			  return enterform();
			  return false;
			});
			$('#bdate').datepicker({
				numberOfMonths: 1,
				showButtonPanel: true,
				yearRange: "-80:+80",
				changeYear: true,
				dateFormat: "yy-mm-dd",
				minDate: "-80y"
			});
		});
	}
	
	
	if($('#testi_form').length > 0){		
		function testimonial_check() {
			$(document).ready(function() {
				$("#form_testi").validate({
					rules : {
						name:{
							required:true
						},
						phone:{
							required:true
						},
						email:{
							required:true,
							email:true
						},
						dob:{
							required:true	
						},
						captcha_val:{
							required:true,			
							remote: {
								url: target_captcha_url,
								type: "post"
							}	
						}
					},
					messages:{					
						name:"",
						phone:"",
						email:"",
						dob:"",
						captcha_val:""
					}
				});
				
				if($("#form_testi").validate().form() == true){
					document.getElementById("form_testi").submit();
					return true;	
				}
				else{
					return false;
				}
			})
		}
		
		
		$(document).ready(function(){
			$("#testi_send_btn").click(function(){
			  	testimonial_check();
			});
			$("#testi_send_btn2").click(function(){
			 	testimonial_check();
			});
		});
	}
});
