jQuery.noConflict();

jQuery(document).ready(function() {

    jQuery('input[title!=""]').hint();
	
	jQuery('.jquery-button-pakket-html').click(function(){
	       

    	   	jQuery(this).parent().parent().find('.pakket-bottom').each(function(){
    	   	
    	   	   if(jQuery(this).css("display") == "none"){
               jQuery(this).slideDown("slow");
               } else {
               jQuery(this).slideUp("slow");
               }

            });	

	});
    
    refreshWinkelmandje();
    
    // init knoppen
    
    jQuery('.ajax-button-add-product').click(function(){
        jQuery.ajax({
           type: "GET",
           url: "/shop2010/jquery.ajax.php",
           data: "action=addproduct&product=" + jQuery(this).attr('productid') + "&aantal=" + jQuery(this).siblings('.input-aantal-bestellen').attr('value'),
           success: function(msg){
             refreshWinkelmandje();
           }
         });
    });
    
    refreshScroll();
    jQuery(window).scroll(function() {
        refreshScroll();
    });
    
    jQuery('.ajax-button-add-input').click(function(){
    
        var total = jQuery(this).parent().parent().children('.input-aantal-bestellen').attr('value');
        jQuery(this).parent().parent().children('.input-aantal-bestellen').attr('value',parseInt(total) + 1);

    });
    
    jQuery('.ajax-button-subtract-input').click(function(){
        var total = jQuery(this).parent().parent().children('.input-aantal-bestellen').attr('value');
        if(total > 1) jQuery(this).parent().parent().children('.input-aantal-bestellen').attr('value',parseInt(total) - 1);
       
    });
    
	jQuery('a.lightbox').lightBox({
        containerResizeSpeed: 300,
        fixedNavigation:true,
        txtImage: 'Product',
    	txtOf: 'van',
    	imageBtnClose: '/shop2010/gfx/lightbox-sluiten.png',
    	imageBtnPrev: '/shop2010/gfx/lightbox-vorige.png',
    	imageBtnNext: '/shop2010/gfx/lightbox-volgende.png'	
    }); 

           
});

function refreshWinkelmandje(){
    
    jQuery.ajax({
       contentType: "application/json; charset=iso-8859-1",
       type: "GET",
       url: "/shop2010/jquery.ajax.php",
       data: "action=showwinkelmandje",
       success: function(msg){
         jQuery('.winkelmandje-content').html(msg);
         refreshRemoveButtons();
         refreshInputField();
       }
     });    
}


function refreshRemoveButtons(){

     jQuery('.ajax-button-remove-product').click(function(){
        
        jQuery.ajax({
           type: "GET",
           url: "/shop2010/jquery.ajax.php",
           data: "action=removeproduct&product=" + jQuery(this).attr('productid'),
           success: function(msg){
             refreshWinkelmandje();
           }
         });  
     });
}

function refreshInputField(){

    jQuery('.winkelmandje-td-aantal').click(function(){
    
        var aantal = jQuery(this).find('.aantal-input-replacement').html();
        
        if(!jQuery(this).hasClass('used')){
        
            jQuery(this).addClass("used");        
            jQuery(this).html("<input class='jquery-winkelmandje-input' type='text' value='"+aantal+"'>");   
            jQuery(this).find(".jquery-winkelmandje-input").select();

            
            jQuery(this).find(".jquery-winkelmandje-input").keydown(function(event) {
            
                if(event.which == "13"){
                    var aantal = jQuery(this).val();
                    var product = jQuery(this).parent().attr('product');
                    ajaxAddProduct(product,aantal); 
                    jQuery(this).parent().removeClass('used');
                    jQuery(this).parent().html(aantal);
                    
                }
            });
            
            jQuery(this).find(".jquery-winkelmandje-input").blur(function(){
                var aantal = jQuery(this).val();
                var product = jQuery(this).parent().attr('product');
                ajaxAddProduct(product,aantal); 
                jQuery(this).parent().removeClass('used');
                jQuery(this).parent().html(aantal);
                
            });
        }
    });
}

function refreshScroll(){
        if(jQuery(this).scrollTop() > 350) {
         jQuery('.scroller').css('top', (jQuery(this).scrollTop() - 350 ) + "px");
         } else {
         jQuery('.scroller').css('top', "0px");
         }
}

function ajaxAddProduct(product, aantal){

    jQuery.ajax({
       type: "GET",
       url: "/shop2010/jquery.ajax.php",
       data: "action=addproduct&product=" + product + "&aantal=" + aantal,
       success: function(msg){
       
         refreshWinkelmandje();
       }
     });
}
