/**
 * @author Richard.Muehlehner
 */
/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
$.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = $.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() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    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;
};

function svital_menu(){

    var timer;
    
    /* Menu Box hovers */
    $('.category_menu tr td div').each(function(){
        $(this).parent('td').hover(function(){
            //add hoverclass
            $(this).children('div').addClass('active_menu').removeClass('inactive_menu');
        }, function(){
            $(this).children('div').removeClass('active_menu inactive_menu');
            if (!$(this).children('div').hasClass('this_active_menu')) 
                $(this).children('div').addClass('inactive_menu');
        });
    });
    
    /* Top Menu sliders */
    $('#slidermenu_list li.cat_level_0').each(function(){
    
        var li_pos = $(this).position();
        var ul_level_1 = $(this).children('ul.ul_level_1');
        var this_height = $(this).outerHeight();
        var this_li = $(this);
        var ls_backgroundcolor = this_li.children('a.link_level_0').css('color');
        
        /*highlight active category */
        
        if (this_li.hasClass('active_category')) {
            this_li.children('a.link_level_0').css('backgroundColor', ls_backgroundcolor);
            this_li.children('a.link_level_0').css('color', 'white');
            //$('#content_wrapper a, #column_left a, TD.infoBoxHeading, TD.infoBoxHeading_contact, .ueberschrift').css('color', ls_backgroundcolor);
            
            //Colormatching menu items
            $('.categorymenu, a.categoryLink, .product_listing_headline a, .p_list_headline').css('color', ls_backgroundcolor);
            
            //Tooltip bg
            $('#this_tooltip').css('backgroundColor',ls_backgroundcolor);
        }
        
        //Bei Hover mache Subcategorien sichtbar und positioniere wenn vorhanden
        $(this).hover(function(){
            if (timer) {
                clearTimeout(timer);
                timer = null
            }
            timer = setTimeout(function(){
                //$('.clear_the_evil_float').html($('.clear_the_evil_float').html() +'<br />'+ parseInt(li_pos.top+$(this).height()));
                //console.log(parseInt(li_pos.top+this_height));
                
                if (!this_li.hasClass('active_category')) {
                    this_li.children('a.link_level_0').css('backgroundColor', ls_backgroundcolor);
                    this_li.children('a.link_level_0').css('color', 'white');
                }
                
                //ul_level_1.fadeIn(100);
                ul_level_1.slideDown("slow");
                ul_level_1.css('top', parseInt(li_pos.top + this_height) + 'px');
            }, 200);
        }, function(){
            ul_level_1.fadeOut(50);
            
            if (!this_li.hasClass('active_category')) {
                this_li.children('a.link_level_0').css('color', ls_backgroundcolor);
                this_li.children('a.link_level_0').css('backgroundColor', 'white');
            }
            clearTimeout(timer);
            timer = null
        });
    });
}

function show_strukturbild(source, title){
   // console.log(source + ' --> ' + title);
    
    var img = new Image();
    img.onload = function(){
       // console.log(img.height + ' -> ' + img.width);
        
        //$('#strukturbild_wrapper').append(img);
        
        
        $('#strukturbild_wrapper').each(function(){
        	$(this).css({
                'background-image': 'url(' + source + ')',
                'height': img.height,
                'display': 'none'
            });
    		
        	if($.cookie('temp_category') == null || $.cookie('temp_category') != source){
        		$(this).slideDown(500);
        	}
        	else {
        		$(this).show();
        	}
        	
        	$.cookie('temp_category', source);
        });
		
		/*
		.animate({
	            opacity: 'toggle',
	            height: 'toggle'
	        	}, 500, 'linear');
	     */
   
	};
    img.src = source;

}

function setup_amount_buttons(){
	var input_value = $('input[name=amount]');
	$('.add_amount').click(function(){
		input_value.val(function(i,val){return parseInt(val)+1;});
	});
	$('.sub_amount').click(function(){
		if(parseInt(input_value.val())>1)
			input_value.val(function(i,val){return parseInt(val)-1;});
		else
			input_value.val(1);
	});
}

$(document).ready(function(){
	var cart_timer;
    svital_menu();
    setup_amount_buttons();
    // Set Search Suggest Background Color Wrapper
    $('.search_suggest_wrapper').css('background-color', $('li.active_category a', '#slidermenu_list').css('background-color'));
    
    //wenn JS activ dann kann tooltip gefadet werden / einmal pro session
    if($.cookie('tooltip_faded') == null){
    	$('#this_tooltip').fadeIn('slow').delay(5000).fadeOut('slow');
    	$.cookie('tooltip_faded','faded');
    }
    
    //Kleiner Einkaufswagen Tooltip Warenwert + Rest bist Versandkostenfrei
    $('#shopping_cart')
	.hover(
		function(){
			//console.log('Hoverin');
            if (cart_timer) {
                clearTimeout(cart_timer);
                cart_timer = null
            }
            cart_timer = setTimeout(function(){
				//$('#this_tooltip').animate({top: '-6px'}, 500);
				$('#this_tooltip').fadeIn('slow');
            }, 200);
		}
	,
		function(){
			//$('#this_tooltip').animate({top:'-500px'}, 500);
			$('#this_tooltip').fadeOut('slow');
			
			clearTimeout(cart_timer);
			cart_timer = null
		}
	);
    
    //Checkout Zusätzliche Informationen zu bezahlsystem nur sichtbar machen wenn ausgewählt
    $('#payment_type_list .default_hidden').hide();
    $('#payment_type_list .moduleRowSelected, #payment_type_list .moduleRowOver, #payment_type_list .moduleRow').click(function(){$('.default_hidden').hide(); $(this).siblings().show()});
    
    /* Hover State Buttons auf anderen Elementen als Links */
    $('#content_wrapper .buy_button, #content_wrapper .checkout_button').not('#TheDisabledButton, #TheSubmitButton, #button_add_to_cart')
    	.hover(function(){
    		$(this).css({'border':'1px solid #E8F0F5','background-color':'#4F87BA','color':'#FFFFFF'});
    	},
    	function(){
    		$(this).css({'border':'1px solid #4F87BA','background-color':'#E8F0F5','color':'#4F87BA'});
    	});
});
