/*
----- Copyright by WebForceOne ------------------
----- http://www.webforceone.info --------
----- Contact: info@webforceone.info -------
----- Telephone: 010 767 04 81 -------------
*/

var base_url = "http://www.jaarsveld.nl/";
var admin_url = "http://beheer.webforceone.info/jaarsveld/";

// ResizeRight --------------------------
(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","auto");
		});
	}
})(jQuery);


// ShowCart ------------------------------
function ShowCart() {
	
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
  		return this.animate({opacity: 'toggle'}, speed, easing, callback);  
	};
	
	$(".cart_inhoud").fadeToggle(300); 
		
}

// CartItems
function LoadCartItems() {
	
	$.ajax({
	    type: "POST",
	    url: base_url+"include/cart.php",
	    data: "subject=LaatCartZien",
		success: function(html) {
			$(".cart_inhoud").html(html);
		}
	});
}

// CartAantal
function LoadCartAantal() {
	
	$.ajax({
	    type: "POST",
	    url: base_url+"include/cart.php",
	    data: "subject=CartAantal",
		success: function(html) {
			$(".cart .bullet .m").html(html);
		}
	});
	
}

// Delete uit de wagen
function Verwijder(Id) {

	$.ajax({
	    type: "POST",
	    url: base_url+"include/cart.php",
	    data: "subject=DeleteUitCart&pid="+ Id,
		success: function(html) {
			$("#cart_item-"+Id).slideUp(300, function() {
				LoadCartItems();
				LoadCartAantal();
				ShakeBullet();
			});
		}
	});
}

// ShakeBullet
function ShakeBullet() {
	
	$(".cart .bullet").animate({opacity: 0.25},100, function() {
    	$(".cart .bullet").animate({opacity: 1},200);
  	});

}

// Body onload
$(document).ready(function() {
	
	$("#main .container .blok").equalHeights();
	LoadCartItems();
	LoadCartAantal();
	
	// Droppy
	$('.nav').droppy();	
	
	// Cycle
	$(".beeld_balk").cycle({ 
   	 	fx:    "fade", 
   		speed:  1000,
   		timeout: 4500
 	});
 	
 	// Hover ------------------------------------------------------------------------------------------

	// Hover Thumbs
	$(".thumb").click(function() {
		
		var hoofdname = $(this).attr('title');
		
		// Animate In
		$(".hoofd_foto").animate({opacity: 0.0},200, function() {
    		$(".hoofd_foto").css("background-image", "url("+admin_url+"upload/catalogus/"+hoofdname+")");
  		}); 
		
		// Animate Out
		$(".hoofd_foto").animate({opacity: 1},400);
		
	});
	
	// Hover Pijl
	$(".voeg_toe").hover(
		function () {
			$(".pijl").animate({top: "58"},200);
		},
		function () {
			$(".pijl").animate({top: "48"},200);
		}
	);

// Winkelwagen ------------------------------------------------------------------------------------------

	// Voeg toe aan de wagen		
	$(".voeg_toe").click(function(){
		
		var Id = $(".voeg_toe").attr("id");
		var p_aantal = $("#p_aantal").val();
		
		$.ajax({
		    type: "POST",
		    url: base_url+"include/cart.php",
		    data: "subject=VoegToeAanCart&pid="+ Id +"&p_aantal="+ p_aantal,
			success: function(html) {
				$(".cart_inhoud").html(html);
				$('html, body').animate({scrollTop:0}, 500);
				LoadCartAantal();
				ShakeBullet();
				$(".cart_inhoud").fadeIn(300); 
			}
		});
							    	
    });
    
    $(".werkbij").click(function(){
    	$("form:first").submit();
    }); 	
	
	$(".afrekenen").click(function(){
    	$("form:second").submit();
    });
    
    
// Zoek functie ------------------------------------------------------------------------------------------

	// Search Producten on KEYUP
	$("#zoek_functie").keyup(function() {
				
		var Content = $(this).val();
		var charLength = $(this).val().length;
                
        if (charLength > "2") { 
			
			$("#zoek_data").fadeIn(300);
						
			$.ajax({
			    type: "POST",
			    url: base_url+"zoek.php",
			    data: "zoek="+ Content,
				success: function(data) {
					$("#zoek_data").html(data);
				}
			});
					
		} else {
			// Doe niks
		}
		
		if (charLength == 0) {
			$("#zoek_data").slideUp(300);
			$("#zoek_data").html("");
		}
		
	});
	
// Placeholders Switch Text ------------------------------------------------------------------------------------------

	function switchText() {
		
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
			$(this).css("color","black");
		} else if ($.trim($(this).val()) == '') {
			$(this).val($(this).attr('title'));
			$(this).css("color","#8f91ab");
		}
	}
	
	$('input[type=text][title != ""]').each(function() {
		
		if ($.trim($(this).val()) == '') {
			$(this).val($(this).attr('title'));
			$(this).css("color","#8f91ab");
		}
		
	}).focus(switchText).blur(switchText);
	
	$('form').submit(function() {
		$(this).find('input[type=text][title != ""]').each(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
	});
		
	
});
