$(document).ready(function(){

    // ADD CLASS SO WE KNOW THE BROWSER HAS JAVASCRIPT
    $('body').addClass('js');

    // TOGGLE ADVANCED SEARCH
    $('#item-search-wrapper .toggle').click(function(){
	$this = $('#item-search-advanced');
	var ih = $('#item-search-advanced-inner').height();
	if ($this.height() == '0') {
		ih = ih + 10;
		h = ih+'px';
	}
	else {
	    h = '0';
	}
	$this.stop().animate({'height':h}, 500);
    });

    // TABS
    $('.tabs a').each(function(){
	$(this).click(function(){
	    $this = $(this);
	    $this.addClass('active');
	    tabId = $this.attr('href');
	    $tabs = $(tabId).parent().find('.tab-content');
	    $tabs.each(function(){
		if ('#'+$(this).attr('id') == tabId) {
		    $(this).show();
		}
		else {
		    $('a[href="#'+$(this).attr('id')+'"]').removeClass('active');
		    $(this).hide();
		}
	    });
	});
    });

    // SLIDER 
    $('#slider-slides').before('<ul id="slider-nav" class="styled">').cycle({
	timeout:	7000,
	speed:		1000,
	pause:		true,
	pager:		'#slider-nav',
	pauseOnPagerHover: true,
	pagerAnchorBuilder: function(idx, slide) {
	    return '<li class="anchor">' + slide.innerHTML + '</li>';
	}
    });

    // HIDE MESSAGES
    $('.msg').each(function(){
	$msg = $(this);
	$('<a href="#" class="close">x</a>').click(function(){
	    $(this).parent().css('display', 'none');
	}).appendTo($msg);
    });

    // INLINE LABEL
    $(':input[title]').each(function() {
	var $this = $(this);
	$label = $('label[for='+$this.attr('id')+']');
	$label.css('display', 'none');
	$this.addClass('placeholder');
	if($this.val() === '') {
	    $this.val($this.attr('title'));
	}
	$this.focus(function() {
	    if($this.val() === $this.attr('title')) {
		$this.val('');
		$this.removeClass('placeholder');
	    }
	});
	$this.blur(function() {
	    if($this.val() === '') {
		$this.val($this.attr('title'));
		$this.addClass('placeholder');
	    }
	});
    });


});

function extend_search_wrapper(){

	$('#item-search-wrapper .toggle').click(function(){
		$this = $('#item-search-advanced');
		var ih = $('#item-search-advanced-inner').height();
		if ($this.height() == '0') {
			ih = ih + 10;
			h = ih+'px';
		}
		else {
			h = '0';
		}
		$this.stop().animate({'height':h}, 500);
    });

	
}

function update_search_wrapper(){
	
	$.post(
		"/scripts/ajax/search_wrapper.php",
		$('#itemsearch').serialize(), 
		function(data){ $('#item-search-wrapper').html(data); extend_search_wrapper() }
	);

}

function update_ww_amount(data){
	
	$.post(
		"/scripts/ajax/update_winkelmandje_aantal.php",
		data, 
		function(e){ window.location.reload(true) }
	);
	
}

function delete_ww_rule(data){
	
	$.post(
		"/scripts/ajax/delete_winkelmandje_rule.php",
		data, 
		function(e){ window.location.reload(true) }
	);
	
}

function add_to_fav(article_id, reload){
	$.post(
		"/scripts/ajax/add_to_fav.php",
		{article_id : article_id}, 
		function(e){ 
		alert(e) 
		
		if(reload) window.location.reload(true)
		
		}
	);
}
