function slideSwitch(switchSpeed){
    var $active = $('.slider img.active');
    
    if ($active.length === 0) 
        $active = $('.slider img:last');
    
    var $next = $active.next().length ? $active.next() : $('.slider img:first');
    
    $active.addClass('last-active');
    
    $next.css({
        opacity: 0.0
    }).addClass('active').animate({
        opacity: 1.0
    }, 1000, function(){
        $active.removeClass('active last-active');
    });
}

$(function(){
    setInterval("slideSwitch(1000)", 5000);
});


$(document).ready(function(){
    var url = "http://www.vp-verhuur.nl/webshop_2010/webshop/";
    var std_string = "Zoek voor producten";
    
    $.post(url + "ajax/twitter.php");
    
    $("a[rel=productimage], div.vpposttext a img > a").colorbox({
        next: "xx",
        maxWidth: '90%',
        maxHeight: '90%'
    });
    
    var options = {
        //zoomType: 'standard',  
        // lens:true,  
        // preloadImages: true,  
        // alwaysOn:false,  
        zoomWidth: 440,
        zoomHeight: 300,
        xOffset: 10
        // yOffset:30,  
        //  position:'left'  
        //...MORE OPTIONS  
    };
    
    $('.jqzoom').jqzoom(options);
    
    /*
     function load(num){
     $('#content').load(num + ".html");
     }
     */
    $.history.init(function(search_val){
        if (search_val !== "") 
            go_search(search_val);
    });
    
    /* 
     $('#ajax-links a').live('click', function(e){
     var url = $(this).attr('href');
     url = url.replace(/^.*#/, '');
     $.history.load(url);
     return false;
     });
     */
    $("#search_producten").keyup(function(e){
    
        var string = $(this).val();
        if (e.which === 13) {
            go_search(string);
        }
        $.post(url + "includes/aantal_producten.php", {
            ajax: "yes",
            search_product: string
        }, function(data){
            $('#aantal_producten').html(data);
        });
        
        
    });
    
    $("#search_producten").click(function(e){
        var string = $(this).val();
        if (string === std_string) {
            $(this).val('');
            $('#zoekbutton').fadeIn('fast');
        }
    });
    
    $("#search_producten").blur(function(e){
        var string = $(this).val();
        if (string === "") {
            $(this).val(std_string);
            $('#zoekbutton').fadeOut('fast');
            $('#aantal_producten').html('');
        }
    });
    
    $('#zoekbutton').click(function(e){
        e.preventDefault();
        var search_val = $("#search_producten").val();
        go_search(search_val);
        
    });
    
    function go_search(search_val){
        $('#producten').html('');
        $('#producten').addClass('loading');
        $('#aantal_producten').html('');
        
        // var url = $(this).attr('href');
        search_val = search_val.replace(/^.*#/, '');
        $.history.load(search_val);
        
        
        $.post(url + "includes/producten.php", {
            ajax: "yes",
            search_product: search_val
        }, function(data){
            $('#producten').removeClass('loading');
            $('#producten').html(data);
            //alert(data);
            $('#shop_nav').html('<b>NAVIGATIE</b> | <a href="' + url + '">Categorieën</a> > Zoeken naar: <b>' + search_val + '</b><div class=\"hr\"></div>');
        });
    }
    
    $('.table_form input[type = text], .table_form input[type = password], .table_form textarea').focus(function(e){
        $(this).addClass('focus_input');
    });
    
    $('.table_form input[type = text], .table_form input[type = password], .table_form textarea').blur(function(e){
        $(this).removeClass('focus_input');
    });
    
    $('#postcode').keyup(function(e){
        var val = $(this).val();
        val = val.replace(' ', '');
        $(this).val(val);
        //  alert(val.lenght);
        if (val.length === 6) {
            $(this).blur();
            $('#woonplaats').focus();
        }
    });
    
    $('#aflever_postcode').keyup(function(e){
        var val = $(this).val();
        val = val.replace(' ', '');
        $(this).val(val);
        //  alert(val.lenght);
        if (val.length === 6) {
            $(this).blur();
            $('#aflever_woonplaats').focus();
        }
    });
    
    
    $('.cart_product a.delete').live('click', function(e){
		e.preventDefault();
        var item = $(this).attr('alt');
        var hoeveel = 1;
        var aantal = 0;
        var div = '#winkelwagen';
        
        $(div).html('');
        $(div).addClass('loading');
        $.post(url + "includes/winkelwagen.php", {
            ajax: "yes",
            prod_aantal: aantal,
            prod_item: item,
            prod_hoeveel: hoeveel
        }, function(data){
            $(div).removeClass('loading');
            
            $(div).html(data);
            
        });
    });
    
    $('a[rel=verander_aantal]').live('click', function(e){
        e.preventDefault();
        var proceed = "no";
        var div;
      	var include;
      	var add = "no";
        var change_aantal;
        var max;
        var item = $(this).attr('href');
        var plusmin = $(this).attr('class');
        var nr;
        
        item = item.replace("?addproduct=", "");
        
        
        var soort = $(this).attr('alt');
        
        var hoeveel = $('#hoeveelheid_' + item).val() * 1;
        var aantal = $('#prod_' + item).val() * 1;
        
        if (soort === "big_aantal" || soort === "big_hoeveelheid") {
            div = '#totaalprijs';
             include = "totaalprijs.php";
        }
        else {
            div = '#winkelwagen';
             include = "winkelwagen.php";
        }
        
        if (soort === "big_hoeveelheid") {
            var nr = '#hoeveelheid_';
            max = 1;
            switch (plusmin) {
                case "min":
                    hoeveel = hoeveel - 1;
                    break;
                case "plus":
                    hoeveel = hoeveel + 1;
                    break;
            }
            if (hoeveel >= max) 
                proceed = "yes";
            change_aantal = hoeveel;
            
        }
        else {
            max = 0;
            nr = '#prod_';
            switch (plusmin) {
                case "min":
                    aantal = aantal - 1;
                    break;
                case "plus":
                    aantal = aantal + 1;
                    break;
                case "toevoegen":
                    hoeveel = hoeveel;
                    add = "yes";
                    break;
            }
            if (aantal >= max) 
                proceed = "yes";
            change_aantal = aantal;
        }
        
        if (proceed === "yes") {
            $(nr + item).val(change_aantal);
            $(div).html('');
            $(div).addClass('loading');
            $.post(url + "includes/" + include, {
                ajax: "yes",
                prod_aantal: aantal,
                prod_item: item,
                prod_hoeveel: hoeveel,
                toevoegen: add
            }, function(data){
                $(div).removeClass('loading');
                if (soort === "big_aantal" || soort === "big_hoeveelheid") {
                    if (aantal === 0) {
                        $('#tr_' + item).remove();
                    }
                    
                }
                $(div).html(data);
                
            });
            
        }
        
    });
    
    
    
    $("#afleveradres").click(function(e){
        if ($('#afleveradres').attr('checked')) {
            $('#afleveradres_div').slideUp('fast');
        }
        else {
            $('#afleveradres_div').slideDown();
        }
    });

	
	$('select[name=loc_ondergrond]').change(function(e){
		var val = $(this).val();
		if(val == "anders"){
			$(this).next('div').fadeIn('fast');
		}else{
			$(this).next('div').fadeOut('fast');
		}
	});
	
	$('input[alt=toggle], input[name=podium]').change(function(e){
		var val = $(this).val();
		if($(this).is(':checked')){
			$(this).next('div').fadeIn('fast');
		}else{
			$(this).next('div').fadeOut('fast');	
		}
	});
	
	$('.dateimg').click(function(){
		$(this).prev('input').focus();
	});
	
	$("input.example, textarea.example").example(function() {
		if($(this).attr('title') !== "" && $(this).attr('title') !== undefined) {
			return $(this).attr('title');
		}
		
	});

	$("input.example, textarea.example").each(function(index) {
    if($(this).attr('title') != $(this).val()){
    	//alert('')
			$(this).removeClass('example');
			}
	});

    $('form[rel=checker] #submit').click(function(e) {
		var form = $(this).closest('form');
		var color = "#ffadad";
		var given = 0;

		//e.preventDefault();
		$.each(form.find('input[rel=verplicht], textarea[rel=verplicht]'), function() {
			var title = $(this).attr('title');
			var val = $(this).val();
			var type = $(this).attr('type');

			if(val == "" || title == val) {

				if(type == "textarea") {
					$(this).css({
						"background-color" : color
					});
				} else {
					$(this).css({
						"background-color" : color
					});
				}

				if(given == 0) 
					given = 1;
				
				//return false;
			} else {
				//alert('U heeft het verplichte veld \"' + title + '\" niet volledig ingevuld');
				if(type == "textarea") {
					$(this).css({
						"background-color" : ""
					});
				} else {
					$(this).parent().css({
						"background-color" : ""
					});
				}
			}

		});
		if(given == 1) 
			e.preventDefault();
		
	});
    
});




