
function addDecor(link) {
    href = link.attr('href') + '&tx_resopaldecordatabase_pi1%5Bajax%5D=1&type=100';
    $.ajax({
	url	    : href,
	dataType    : 'json',
	success	   : function(decors) {
	    link.find('span.addToSession').hide();
	    link.find('span.isInSession').show();
	    link.find('span.isInSession').css({
		'backgroundColor': '#D23034',
		'color': '#ffffff'
	    });
	    link.css('borderColor', '#D23034');
	    updateDecorCartCount(decors);
	}
    });
}

function removeDecor(link) {
    href = link.attr('href') + '&tx_resopaldecordatabase_pi2%5Bajax%5D=1&type=100';
    link.hide('slow');
    $.ajax({
	url			: href,
	dataType	: 'json',
	success		: function(decors) {
	    updateDecorCartCount(decors);
	    link.next().remove();
	    link.remove();
	},
	error		: function() {
	    link.show();
	}
    });
}

function updateDecorCartCount(decors) {
    decorCount = $('#cart .decorCartMessage .decorCount');
    decorCartMessage = $('#cart .decorCartMessage');
    if(decors.length > 0) {
	decorCount.html(decors.length);
	decorCartMessage.fadeIn();
    } else {
	decorCartMessage.fadeOut('normal', function(){
	    decorCount.html(decors.length);
	});
    }
}

$(document).ready(function() {

    $('a.collection:first-child').addClass('first');
    $('a.collection:last-child').addClass('last');

    $('a.addDecorToSession').click(function(event) {
	event.preventDefault();
	addDecor($(this));
    });

    $('#sampleOrderForm #decorList a').click(function(event) {
	event.preventDefault();
	removeDecor($(this));
    });

    //	$('a.accordion_header').click(function(event) {
    //		event.preventDefault();
    //		window.location.href = 'http://' + window.location.host + $(this).attr('href');
    //	});

    $('.accordion .contact_form .submit').click(function(event) {
	event.preventDefault();
	//window.location.href = 'http://' + window.location.host + $(this).attr('href');
	accordionPath = $(this).closest('.accordion_content').prev().attr('href');
	$(this).closest('form').attr('action', accordionPath);
	$(this).closest('form').submit();
    });

    $('#header .link_list > li').mouseenter(function() {
	$(this).find('ul').stop(true, true).fadeIn();
    });

    $('#header .link_list > li').mouseleave(function() {
	$(this).find('ul').stop(true, true).fadeOut();
    });

    $("select, input:checkbox, input:radio, input:file").uniform();

    $('#slideshow').cycle({
	fx			: 'fade',
	timeout		: 4000,
	speed		: 1000,
	pager		: '#nav',
	slideExpr	: '.slide'
    });
    //$('#slideshow').cycle('pause');
	
    //searchText = 'Suchbegriff eingeben';
    searchBox = $('#search input.searchbox');

    searchBox.attr('value', searchText);

    searchBox.focusin(function() {
	if($(this).attr('value') == searchText) {
	    $(this).attr('value', '');
	}
    });

    searchBox.focusout(function() {
	if($(this).attr('value') == '') {
	    $(this).attr('value', searchText);
	}
    });

    try {
	$(".accordion").accordion({
	    header			: '.accordion_item > a',
	    autoHeight			: false,
	    clearStyle			: true,
	    navigation			: true
	});
    } catch(e) {}

    $(".jqfancybox").fancybox({
	hideOnContentClick:true,
	titlePosition:'over',
	overlayShow:false
    });
	
});

