
 $(document).ready(function() {

/**********************
 * category slide down*
 **********************/

function init_slide_down(head, body, ident) {
    if ($.cookie(ident) != 'true') {
        body.hide();
        head.addClass('closed');
    }

    head.find('a').click(function(){
        $.cookie(ident, head.hasClass('closed'), {expires: 1, path: '/'});

        head.toggleClass('closed');
        if ($.browser.msie)
            body.toggle();
        else
            body.slideToggle('fast');
        return false;
    });
}

init_slide_down($('#category-link').parent(), $('#menu_container'), 'categories_open');
init_slide_down($('#brand-link').parent(), $('#brand_menu_container'), 'brand_open');



/*******************************
 * galleria in product details *
 *******************************/

    $('#product-thumbnails').galleria({
                insert: '#product-image-large',
        	//history   : true, // activates the history object for bookmarking, back-button etc.
		history: false,
		clickNext : true, // helper for making the image clickable
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

		// fade in the image & caption
		if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
		image.css('display','none').fadeIn(1000);
		}
		caption.css('display','none').fadeIn(1000);

		// fetch the thumbnail container
		var _li = thumb.parents('li');

		// fade out inactive thumbnail
		_li.siblings().children('img.selected').fadeTo(500,0.3);

		// fade in active thumbnail
		thumb.fadeTo('fast',1).addClass('selected');

		// add a title for the clickable image
		//image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here

		// fetch the thumbnail container
		var _li = thumb.parents('li');

		// if thumbnail is active, fade all the way.
		var _fadeTo = _li.is('.active') ? '1' : '0.3';

		// fade in the thumbnail when finnished loading
		thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

		// hover effects
		thumb.hover(
			function() { thumb.fadeTo('fast',1); },
			function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
	}
    });





/********************
 * slide demo       *
 ********************/
    $("#slide-demo")
        .mouseover(function() {
            $("#header .contentinner").animate({height:"80px"}, {duration:1500})
        });
 });


/*******************
 * navigation      *
 *******************/
/* TODO simplify traversing
   not working in IE (debuging on Windows PC)
   just a POC so far
   but its working in FF
*/

function init_sale_navigation() {
    var $sale = $('#menu>h2>a[href=/category/sale/]').parent();
    var $elem = $('<div style="margin-left:5px;font-size:80%;"></div>');
    $sale.after($elem);
    $elem.append('<h2 class="sub"><a href="#" id="category-link2">Kategorien</a></h2>');
    $('#menu_container').clone(true).attr('id', 'menu_container2').show().appendTo($elem);
    $elem.append('<h2 class="sub"><a href="#" id="brand-link2">Labels</a></h2>');
    $('#brand_menu_container').clone(true).attr('id', 'brand_menu_container2').show().appendTo($elem);
    $elem.find('a').each(function(){
        this.href += '?sale=1';
    });
}

$(document).ready(function(){
    /*
    $("#menu ul li a").each(function(){
        //finding subnav
        if($(this).next().is("ul")) {
            subnav = $(this).next("ul")
            if(!subnav.children("li").children("a").hasClass("current")) {
                subnav.css("display","none");
            }
            $(this).click(function() {
                if(subnav.css("display")=="none") {
                    subnav.slideDown("slow");
                }
                else {
                    subnav.slideUp("slow");
                }
                return false;
            })
        }
    });
    */


    var in_sale = window.location.href.indexOf('sale')!=-1;

    if (in_sale) {
        init_sale_navigation();
    }

    $("#menu ul li ul li a").mouseover(function() {
        $(this).parent().parent().parent().children("a").stop().animate({backgroundPosition:"(0 -180px)"}, {duration:300})
    })

    $("#menu ul li ul li a").mouseout(function() {
        if(!$(this).parent("li").parent("ul").children("li").children("a").hasClass("current"))
            $(this).parent().parent().parent().children("a").stop().animate({backgroundPosition:"(0 0)"}, {duration:1000})
    })

    $("#menu ul li ul li .current").each(function(){
        $(this).css({background: "none"})
        $(this).parent("li").css( {background: "url(/media/img/design/nav-arrow.gif) left center no-repeat"})
        $(this).parent("li").parent("ul").parent("li").children("a").css("background-position", "0 -180px")
    })

/********************
 * the menu fade in *
 ********************/
     $("#menu>div>ul>li>a").each(function(){
	// only if not marked and first level nav
        if(((!$(this).hasClass("mark")) && (!$(this).hasClass("current"))) && !$(this).next("ul").children("li").children("a").hasClass("current")) {
            $(this)
                .css( {backgroundPosition: "0 0"} )
                .mouseover(function(){
                    $(this).stop().animate({backgroundPosition:"(0 -180px)"}, {duration:300})
                })
                .mouseout(function(){
                    if(!($(this).next("ul").children("li").children("a").hasClass("current") || $(this).hasClass("mark")))
                       $(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:1000})
                    })
                }
        })
    })

