var timer = 0;
var klantreacties_vis = false;

jQuery.cookie = function (key, value, options) {
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        
        value = String(value);
        
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '',
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


$(document).ready(function() {
	/* steden menu */
	if($.cookie('steden_all') != null)
		$('#steden_pop').hide();
	else
		$('#steden_all').hide();
		
	/* steden menu listener */
	$('#steden_pop_link').click(function() {
		$('#steden_pop').hide();
		$('#steden_all').fadeIn('slow');
		$.cookie('steden_all', '1');
		return false;
	});
	$('#steden_all_link').click(function() {
		$('#steden_all').hide();
		$('#steden_pop').fadeIn('slow');
		$.cookie('steden_all', null);
		return false;
	});
		
	/* search */
	$('#module_search input').keydown(function(e) {
		if (e.keyCode == 13) {
			moduleSearch();
		}
	});
	
	/* bestsellers */
	if ($('#bestsellers').length)
		$("#bestsellers").scrollable({vertical:true, circular: true}).autoscroll({autoplay: true, interval: 6000});
		
	/* voordelen/klantreacties */
	if ($('#voordelen_holder').length) {
		var timer = setInterval(toggleVoordelenKlantreacties, 7000);

	}
	

	/* cart */
	/*
	$('#add_to_cart').replaceWith('<a onclick="" id="add_to_cart" class="button">' + $('#add_to_cart').html() + '</a>');
	$('#add_to_cart').click(function () {
		$.ajax({
			type: 'post',
			url: '/index.php?route=checkout/cart',
			data: $('#product :input')
		});	

		$('#module_cart .middle').load('/index.php?route=module/cart/callback');
	
		var image = $('#image').offset();
		var cart  = $('#module_cart').offset();
	
		$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
	
		params = {
			top : cart.top + 'px',
			left : cart.left + 'px',
			opacity : 0.0,
			width : $('#module_cart').width(),  
			heigth : $('#module_cart').height()
		};		
	
		$('#temp').animate(params, 'slow', false, function () {
			$('#temp').remove();
		});			
	});	
	*/
});

function toggleVoordelenKlantreacties() {
	clearTimeout ( timer );

	if (klantreacties_vis) {
		$('#klantreacties_holder').fadeOut(2000, function(){
			$('#voordelen_holder').fadeIn(2000);
		
		});
	} else {
		$('#voordelen_holder').fadeOut(2000, function(){
			$('#klantreacties_holder').fadeIn(2000);
		
		});
	}
	
	klantreacties_vis = !klantreacties_vis;	
	timer = setInterval(toggleVoordelenKlantreacties, 9000);
}

function moduleSearch() {
	location = 'index.php?route=product/search&keyword=' + encodeURIComponent($('#filter_keyword').attr('value')) + '&description=1';
}
