var _open=0;
var _open = 0;
var _personaListener = new Array();
var currentPersona = 0;
var _suppressHashChange = 0;

/* Facebook */
(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/de_DE/all.js#xfbml=1";
    fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));

jQuery(document).ready(function () {

    if (jQuery(".languageSwitcherWrapper").length > 0) {
        jQuery(document).click(function () {
            jQuery(".languageSwitchOverlay").remove();            
            jQuery(".languageSwitcherWrapper .languageSwitcher").addClass("dn");
        });

        jQuery(".switchLanguageButton").click(function () {
            var overlayDiv = document.createElement("div");
            // show overlay 
            jQuery(overlayDiv).addClass("languageSwitchOverlay");
            jQuery("body").append(overlayDiv);            
            // show layer 
            jQuery(".languageSwitcherWrapper .languageSwitcher").removeClass("dn");
            return false;
        });
    }
});

jQuery(function () {

    if (jQuery(".mainAccordion li h3").length > 0) {
        jQuery(window).hashchange(function () {
            jQuery(document).ready(function () {
                if (_suppressHashChange == 1) {
                    return;
                }                
                _suppressHashChange = 0;
                var numItems = jQuery(".mainAccordion li h3").length;
                if (numItems > 0) {
                    var hash = location.hash;
                    var index = 0;

                    // validate the index
                    if (hash.charAt(1) == 'a') {
                        index = hash.substring(2);
                        if (!index.match(/\d+/) || index >= numItems) {
                            // do nothing                    
                            return;
                        }
                    }

                    //select the accordion item
                    jQuery(".mainAccordion li h3").each(function (i) {
                        var acc = jQuery(this).parent();
                        if (i == index) {
                            acc.find("h3").css("background", "url(/images/boxTitle.jpg) repeat-x");
                            acc.addClass("active");
                            acc.find(".accordionContent").show();
                        }
                        else {
                            acc.removeClass("active");
                            acc.find("accordionContent").css("display", "none");
                        }
                    });
                }
            });
        });

        // trigger hashchange event on page load
        jQuery(window).hashchange();
    }

    // suchbox button
    jQuery('div.headerForm a.arrowCircle1').click(function (event) {
        jQuery('div.headerForm input[type=hidden]').val('true');
        Page_Validators = new Array();
        jQuery('#mainform').submit();
    });

    // prevent forms from being submitted by hitting return
    jQuery('input').keypress(function (event) {
        if (event.keyCode == 13) {
            if (jQuery(this).hasClass('headerSearchBox')) {
                jQuery(this).nextAll('input').val('true');
                Page_Validators = new Array();
                jQuery('#mainform').submit();
            } else {
                event.preventDefault();
                event.stopPropagation();
                return false;
            };
        };
    });

    if (jQuery('a.ext').length > 0) {
        jQuery('a.ext').click(
            function (event) {
                window.open($(this).attr('href'));
                event.preventDefault();
            }
        );
    }

    // google CSBE search autocompletion
    if (jQuery("#SearchBox").attr('id') != undefined) {
        var id = jQuery("#searchResultsItemID").attr("value");
        jQuery("#SearchBox").autocomplete(
            {
                source: "/ajax/autocomplete.aspx?i=" + id,
                minLength: 1
            }
        );
    }

    if (jQuery(".mainAccordion li h3").length > 0) {
        jQuery(".mainAccordion li h3").click(function () {

            var accIndex = jQuery(this).parent().index();

            if (_open == 1) {
                return false;
            }
            _open = 1;

            _suppressHashChange = 1;
            window.location.hash = "a" + accIndex;

            var _this = this;
            if (jQuery(this).parent().is(".active")) {
                accordionClose(jQuery(_this).parent());
            }
            else {
                accordionClose(jQuery(_this).parent().siblings(), function () {
                    accordionOpen(jQuery(_this).parent())
                });

            }
        })
    }

    jQuery("input:text").each(function () {
        var txt = jQuery(this).val();
        jQuery(this).focus(function () {
            if (jQuery(this).val() == txt) {
                jQuery(this).val("");
                //jQuery(this).css("color", "#00579f");
            }
        })
        jQuery(this).blur(function () {
            if (jQuery(this).val() == "") {
                jQuery(this).val(txt);
                //jQuery(this).css("color", "#6699cc");
            }
        })
    })
    /*
    if (jQuery(".sideNav li span").length > 0) {
    jQuery(".sideNav li span").textShadow({
    horizontal: -1,
    vertical: 0,
    color: "rgb(255,255,255)",
    fontsize: "18px"
    });
    }
	
	
    if (jQuery(".nav ul li span a").length > 0) {
    }*/

    // persona-slider
    //    var sliderValue = parseInt(jQuery(".slider .sliderValue").text());
    //    if (sliderValue < 1 || sliderValue > 3 || isNaN(sliderValue)) sliderValue = 2; // fallback if nothing is setted

    //    function setPersonaSliderValue(id, ajax) {

    //        jQuery('div.slider p em:not(.white)').each(function (index, value) {
    //            if (index + 1 == id) {
    //                jQuery(this).addClass('active');
    //                jQuery(this).hide();
    //                jQuery(this).next().show();
    //            }
    //            else {
    //                jQuery(this).removeClass('active');
    //                jQuery(this).next().hide();
    //                jQuery(this).show();
    //            }
    //        });

    //        // set new persona global
    //        currentPersona = id;

    //        // hit all JS-listener for the slider
    //        for (var i = 0; i < _personaListener.length; i++) {
    //            _personaListener[i](); // call listener
    //        }

    //        // send it to server!
    //        if (ajax) {
    //            $.get("/ajax/persona.aspx?persona=" + id);
    //        }

    //    }
    //    if (jQuery('.sliderBox').length > 0 && jQuery('.sliderBox').slider) {
    //        jQuery('.sliderBox').slider({
    //            min: 1,
    //            max: 3,
    //            step: 1,
    //            value: sliderValue,
    //            slide: function (event, ui) {
    //                setPersonaSliderValue(ui.value, true);
    //            }
    //        });
    //        jQuery(window).load(function () {
    //            setPersonaSliderValue(sliderValue, false); //init	
    //        });
    //    }

    //    currentPersona = (jQuery('#personaSetting').length > 0) ? jQuery('#personaSetting').text() : "2";

    //    // persona content
    //    function updatePersonaContent() {
    //        var personaName = "p" + currentPersona;
    //        jQuery('.personaContent').each(function () {
    //            if (jQuery(this).hasClass(personaName)) {
    //                jQuery(this).show();
    //            }
    //            else {
    //                jQuery(this).hide();
    //            }
    //        });
    //    }
    //    updatePersonaContent();
    //    addPersonaListener(updatePersonaContent);
});

jQuery(window).load(function () {    
	setTimeout(function(){
	jQuery(".mainAccordion li:not('.active')").find(".accordionContent").css("display","none");
	},50);
});

function accordionOpen(acc, callback) {    
    
    acc.find("h3").css("background", "url(/images/boxTitle.jpg) repeat-x");
	acc.addClass("active");
	acc.find(".accordionContent").slideDown("normal","linear",function(){
		
		callback;
		_open=0;
	})    
}

//function addPersonaListener(func) {
//    _personaListener.push(func);
//}


function accordionClose(acc, callback){
    callback = callback ||
    function(){
    };
    acc.find("h3").css("background", "#FFF");
	var accContents	= acc.find(".accordionContent");
	if (accContents.length == 0)
	{
		callback();
		return false;
	}
    accContents.slideUp("normal","linear", function(){
		jQuery(this).parent().removeClass("active");
		_open=0;
		callback();		
    })
	
}


(function($){
    $.fn.extend({
        Rselect: function(options){
            var defaults = {
                skin: 'blue'
            };		
            var opts = $.extend(defaults, options);
            jQuery(this).each(function(){				
                var name = jQuery(this).attr("id");
                var bselectBox, bselect, boptions, options, boption, option;
                var _this = jQuery(this);
                var x = 0;
                jQuery(this).before("<div class='Rselect_" + opts.skin + "' id='select_" + name + "'></div>");
                bselectBox = jQuery("#select_" + name);
                bselectBox.append("<div class='select_" + opts.skin + "'></div>");
                bselect = bselectBox.find(".select_" + opts.skin);
                boptions = jQuery(document.createElement("ul"));
                boptions.addClass("options_" + opts.skin);
                jQuery("body").append(boptions);								
                var selectX = bselectBox.offset().left, selectY = bselectBox.offset().top;				
                boptions.css("left", selectX).css("top", selectY + bselectBox.height());
                options = jQuery(this).children();
                option = jQuery(this).find("option");
                jQuery(this).hide();
                boptions.hide();
                for (var i = 0; i < options.length; i++) {
                    if (options.eq(i).is("option")) {
                        boptions.append("<li class='option_" + opts.skin + "'>" + options.eq(i).html() + "</li>");
                        if (options.eq(i).is(":selected")) {
                            boptions.children().last().addClass("selected");
                            bselect.html(options.eq(i).html());
                        }
                    }
                    else 
                        if (options.eq(i).is("optgroup")) {
                            x++;
                            var indentOptions = options.eq(i).children();
                            boptions.append("<li class='optgroup_" + opts.skin + "'>" + options.eq(i).attr('label') + "</li>");
                            for (var n = 0; n < indentOptions.length; n++) {
                                boptions.append("<li class='indent option_" + opts.skin + "'>" + indentOptions.eq(n).html() + "</li>");
                                if (indentOptions.eq(n).is(":selected")) {
                                    boptions.children().last().addClass("selected");
                                    bselect.html(indentOptions.eq(n).html());
                                }
                            }
                        }
                }
                boption = boptions.find(".option_" + opts.skin);
                jQuery(window).resize(function(){
					selectX = bselectBox.offset().left, selectY = bselectBox.offset().top;
					boptions.css("left", selectX).css("top", selectY+ bselectBox.height() );
                });
                boption.click(function(){
                    boption.removeClass("selected");
                    bselect.html(jQuery(this).html());
                    jQuery(this).addClass("selected");
                    boptions.hide();
                    _this.attr("value", option.eq(boption.index(jQuery(this))).val());
					PostbackProductFinder();
                    return false;
                });
                boption.hover(function(){
                    boption.removeClass("hover");
                    jQuery(this).addClass("hover");
                });
                bselect.click(function(){
                    boptions.toggle();
                    return false;
                });
                jQuery("body").click(function(){
                    boptions.hide();
                });
                
            })
            return false;
        }
    })
})(jQuery);

jQuery(function () {
    jQuery('.headerForm span span a').each(function () {
        jQuery(this).clone().addClass('selector-hover').insertAfter($(this));
    });
    jQuery('.nav ul li span a').each(function () {
        jQuery(this).clone().addClass('selector-hover').insertAfter($(this));
    });
    jQuery('div.slider p em').each(function () {
        jQuery(this).clone().removeClass('active').addClass('white').insertAfter($(this));
    });

    // activate teaser
    if (jQuery().dynamicTeaser) {
        jQuery("div.welcomeModule ul").dynamicTeaser();
        jQuery("ul.teaserList").dynamicTeaser({ "direction": "v", "distance": 200, "height": 150 });
    }

    // lightbox
    if (jQuery().prettyPhoto) {
        jQuery("a[rel^='prettyPhoto']").prettyPhoto({
            social_tools: "",
            default_width: 810,
            overlay_gallery: false,
            slideshow: false
        });

    }

    // multimedia
    var multimediaAccordionCount = 0;
    var multimediaJq = jQuery(".videoArea").find('.carouselBox');
	var globalVideoIndex = 0;
    multimediaJq.each(function () {
        multimediaAccordionCount++;
        var setVideo = function (element, id, auto) {
            var html = "<iframe id=\"player\" type=\"text/html\" width=\"620\" height=\"378\" src=\"http://www.youtube.com/embed/" + id + "?enablejsapi=1&autoplay=" + auto + "\" frameborder=\"0\" />";
            element.html(html);
        }

        var multimediaItemsJs = jQuery(this).find("li");
        var id = jQuery(this).attr('id');
        var carouselJq = jQuery(this);
        var currentVideoJq = null;
        var carouselControl = null;
		var lightCarousel = (multimediaItemsJs.length == 1);
		
		if(!lightCarousel)
		{
			jQuery(this).jcarousel({
				wrap: 'circular',
				scroll: 1,
				visible: multimediaItemsJs.length >= 3 ? 3 : length,
				itemFallbackDimension: 183,
				start: multimediaItemsJs.length,
				itemFirstInCallback: {
					onBeforeAnimation: function (carousel, item, index, state) {				
						var activeJq = carousel.get(index + 1);
						currentVideoJq = activeJq;
						var mediaId = activeJq.attr("title");
					
						activeJq.fadeTo("fast", 1);
						carouselJq.find(".jcarousel-item").each(function () {
							if (activeJq.index() != jQuery(this).index()) {
								jQuery(this).fadeTo("fast", 0.3);
							}
						});
						
						// title
						carouselJq.parents(".carousel").find(".carouselTitle").each(function () {
							if (jQuery(this).hasClass(mediaId)) {
								jQuery(this).removeClass("dn");
							}
							else {
								jQuery(this).addClass("dn");
							}
						});
					}
				},

				initCallback: function (carousel) {
					carouselControl = carousel;
				}
			});
		}

        var videoItemsJq = jQuery(this).find("li");
        if (lightCarousel) {
			var i = 0;
			videoItemsJq.each(function(){
				if(i == 0)
				{
					jQuery(this).show();
					jQuery(this).parent().css('width', '');
					jQuery(this).css('float', '');
					jQuery(this).css('width', '');
					jQuery(this).css('margin', 'auto');
				}
				else
				{
					jQuery(this).remove();
				}
				i++;
			});
			
			
		
            var container = videoItemsJq.parents(".jcarousel-container");
            container.find(".jcarousel-prev").hide();
            container.find(".jcarousel-next").hide();
        }

        videoItemsJq.each(function () {
            var videoId = jQuery(this).attr("rel");

            if (multimediaAccordionCount == 1) {
                jQuery(this).parents(".carousel").find(".carouselDesc").each(function () {
                    if (jQuery(this).hasClass("media1")) {
                        jQuery(this).parents(".videoArea").find(".videoInfo").html(jQuery(this).html());
                    }
                });

                // title
                jQuery(this).parents(".carousel").find(".carouselTitle").each(function () {
                    if (jQuery(this).hasClass("media1")) {
                        jQuery(this).parents(".videoArea").find(".videoTitle").html(jQuery(this).html());
                    }
                });
            }

            jQuery(this).click(function () {
				if(lightCarousel)
				{
					setVideo(jQuery("div.videoBox"), videoId, "1");
					return;
				}
			
                var clickedIndex = jQuery(this).index();
                var currentIndex = currentVideoJq.index();

                if (clickedIndex < currentIndex) {
                    carouselControl.prev();
                }
                if (clickedIndex > currentIndex) {
                    carouselControl.next();
                }

                var mediaId = jQuery(this).attr("title");

                // description
                jQuery(this).parents(".carousel").find(".carouselDesc").each(function () {
                    if (jQuery(this).hasClass(mediaId)) {
                        jQuery(this).parents(".videoArea").find(".videoInfo").html(jQuery(this).html());
                    }
                });

                // title
                jQuery(this).parents(".carousel").find(".carouselTitle").each(function () {
                    if (jQuery(this).hasClass(mediaId)) {
                        jQuery(this).parents(".videoArea").find(".videoTitle").html(jQuery(this).html());
                    }
                });

                // set video
                setVideo(jQuery("div.videoBox"), videoId, "1");
            });

            if (globalVideoIndex == 0) {
                setVideo(jQuery("div.videoBox"), videoId, "0");
            }
			
            if (!lightCarousel && globalVideoIndex != videoItemsJq.length - 2) {
                jQuery(this).fadeTo(0, 0.3);
            }
            globalVideoIndex++;
        });
    });


    /* slider tooltip */
    jQuery(".sliderTooltip img").each(function () {
        var textJq = jQuery(".sliderTooltip .tooltip p");
        var borderJq = jQuery(".sliderTooltip .tooltip .border");
        var tooltipJq = jQuery(".sliderTooltip .tooltip");
        borderJq.fadeTo(0, 0.5);
        borderJq.width(textJq.width() + 20).height(textJq.height() + 20);

        jQuery(this).mouseenter(function () {
            tooltipJq.css("visibility", "visible");
            tooltipJq.fadeIn("fast");
        });

        jQuery(this).mouseleave(function () {
            tooltipJq.hide();
        });

        jQuery(".sliderTooltip .tooltip").hide();
    });

});
