// JavaScript Document
$(document).ready(function() {
	 
	$(document).pngFix();
	
    $('#SearchCat, #SearchCatCountry').selectbox();
    
    $('.col-3 .wine-advisor-selectbox').each(function(){
     var sThisClass = (($(this).attr("class")).slice((($(this).attr("class")).indexOf(" ")+1)));
        $(this).selectbox(  
               {  
                 inputClass: 'wine-advisor-selectbox', //css class for the input which will replace the select tag, display the background image  
                 containerClass: 'wine-advisor-selectbox-wrapper ' + sThisClass, // The list container class (a div element)  
                 hoverClass: 'current', // css class for the current element  
                 currentClass: 'selected', // css class for the selected element  
                 debug: false // debug mode on/off  
             }  
             );  
        });
    function myFunction(args)
    {
        //if (args.selectedVal != '')
            var sHref;
            sHref = args.selectedVal.replace(/dotabezeichnung3/,'dot_abezeichnung3');
            sHref = sHref.replace(/dotjahrgang/,'dot_jahrgang');
  
            window.location.href = sHref;
            
    }

        $('.col-2 .product-overview-sorter').selectbox({
                     onChangeCallback : myFunction,
                     inputClass: 'product-overview-sorter', //css class for the input which will replace the select tag, display the background image  
                     containerClass: 'product-overview-sorter-wrapper', // The list container class (a div element)  
                     hoverClass: 'current', // css class for the current element  
                     currentClass: 'selected', // css class for the selected element  
                     debug: false // debug mode on/off  
        }); 
    ///
    ///JCarousel functions
    ///
    
    ////
    ////remember: all carousels that should show from the beginning need the "active" class in the surrounding div with class "carousel"
    ////
    
    ////-------------------------------------start of functions for carousels
    function in_array(needle, haystack) {
        for ( var i = 0; i < haystack.length; i++ )    {
            if (haystack[i] == needle) return true;
        }
        return false;
    }
    
    function hideActiveJCarousel() {
        jQuery('div.carousel').each(function(){
            if ($(this).hasClass("active") && $(this).hasClass("tab-carousel")) {
                $(this).hide();
                $(this).removeClass("active");
            }
        });
    }
    
    function showSelectedCarousel(sNameOfSelectedCarousel) {
        oObj = jQuery('#direct-shopping-carousel-' + sNameOfSelectedCarousel).parent().parent().parent().parent();
        $(oObj).show();
        $(oObj).addClass("active");
    }
    
    ////-----------------------------end of functions for carousels
    
    jQuery('#direct-shopping-carousel-standard').jcarousel();
    jQuery('#direct-shopping-carousel-new-wines').jcarousel();
    jQuery('#direct-shopping-carousel-awarded-wines').jcarousel();
    jQuery('#direct-shopping-carousel-our-tip').jcarousel();
    jQuery('div.carousel').each(function(){
        if (!$(this).hasClass("active")) {$(this).hide();}
    });
    jQuery('#vintner-carousel').jcarousel({scroll:8});
    jQuery('#recommendations-carousel').jcarousel();
    
    ///
    ///JCarousel tab functions
    ///
    jQuery('.home-tabs>li>a').click(function(){
        var sWineTabCategory = 'standard';
        var sWineTabCategory = ($(this).hasClass("new-wines")) ? "new-wines" : sWineTabCategory;
        var sWineTabCategory = ($(this).hasClass("awarded-wines")) ? "awarded-wines" : sWineTabCategory;
        var sWineTabCategory = ($(this).hasClass("our-tip")) ? "our-tip" : sWineTabCategory;
        
        hideActiveJCarousel();
        
        showSelectedCarousel(sWineTabCategory);
        
        return false; 
    });

    ////
    ////Footer functions 
    ////
    var bFooterActive = false;
    var iSnapBackTiming = 1200;
    var myTimeout = window.setTimeout(function(){},1);
	
	///
	///set active element
	///
	$('#QuickInfoList li a').each(function(){
										   
		$(this).removeClass("active");
//		var domain = 'bwk.dot-deploy.de';
		var domain = 'bremer-weinkolleg.de';
		if ( location.href.indexOf(domain + $(this).attr("href")) > -1) $(this).addClass("active");
	});
	
	var oCheckActiveElement = $('#QuickInfoList li a.active');
	if (oCheckActiveElement.length < 1) $('#QuickInfoList li a:first').addClass("active");
    var oActiveAElementOnPageCall = jQuery('#QuickInfoList li a.active');
	

    //function to show Footer <p> that is active on the current page (toggled by the "active" class of the <a> element)
    function restoreActiveP (oInitActObj, bIsRestore){
        
        if (bFooterActive == false){
            
            var sActiveRefString = $("#QuickInfoList li a.active").attr("title");

            if (bIsRestore == true) {
                jQuery("#QuickInfoActiveText p" + sActiveRefString).fadeOut(100, function(){
        
                    jQuery("#QuickInfoList a").removeClass("active");
                    jQuery(oInitActObj).addClass("active");
                                                                                      
                    var sRefString = jQuery(oInitActObj).attr("title");
                    jQuery("#QuickInfoActiveText p#" + sRefString).fadeIn(100);
                });
            }
            else {
                jQuery("#QuickInfoActiveText p#" + sActiveRefString).show();
            }
                
        }
    }
    
    //show corresponding <p> when hovering over a elements
    jQuery("#QuickInfoList li a").hover(function(){

        if (!($(this).hasClass("active"))){
        
                window.clearTimeout(myTimeout);
                bFooterActive = true;

                jQuery("#QuickInfoList a").removeClass("active");
                jQuery(this).addClass("active");
                
                $('*:animated').stop(true,true);

                var sRefString = jQuery(this).attr("title");
                var self = $(this);
            
                jQuery("#QuickInfoActiveText p:visible").fadeOut(100, function(){
                                                                                          
                    var sRefString = jQuery(self).attr("title");
                    jQuery("#QuickInfoActiveText p#" + sRefString).fadeIn(100);
                    
                });
                
            }
            
    }, function(){
        
        bFooterActive = false;
        
        var sActiveRefString = jQuery("#QuickInfoList li a.active").attr("title");
        var sOnLoadRefString = jQuery(oActiveAElementOnPageCall).attr("title");
        
        if (sActiveRefString != sOnLoadRefString) {myTimeout = window.setTimeout(function(){restoreActiveP(oActiveAElementOnPageCall, true)},iSnapBackTiming);}
        
    });
    
    //on page load show the corresponding <p> element // this way, only the <a> element of the footer needs to be set to active in the html code.
    restoreActiveP(oActiveAElementOnPageCall, false);


    ////
    //////bestseller list functions
    ////
    var sActiveListElement = 'none';
    var iActiveElement = 0;
    var bActive = false;
    
    function checkClose () {
        if (bActive == false) {
            var sSelectString = '.bestseller-list li';
            var sSelectStringSubMenu = '.bestseller-list li ul';
            jQuery(sSelectString).css("zIndex","1");
            jQuery(sSelectString + ' a').attr("class","inactive");
            jQuery(sSelectStringSubMenu).hide().css("zIndex","1");
        }
    }
    
    function setActFalseAndInitTimeout() {
        bActive = false;
        setTimeout(function (){checkClose();},150);
    }
    
    ///
    ///this function is used for different menus - the list name is handed over in sListString
    ///
    function toggleBottomShadowColor (sListString) {
        var aBottomElementDark = $("." + sListString + "-bottom-dark");
        if (aBottomElementDark.length == 0) $('.' + sListString +'-bottom').attr("class", sListString + "-bottom-dark");
        else $('.' + sListString + '-bottom-dark').attr("class", sListString + "-bottom");
    }
    
    
    ///this is the function to toggle the menus on click
    jQuery('.bestseller-list>li>a').click(function(event){

        var iSlideTiming = 500;
        var bToggleBottomDiv = false;
        
        //change look of bottom div according to li elements
        if ($(this).next("ul").hasClass("last-dropdown")) {toggleBottomShadowColor('bestseller-list');}

        //close open elements (if any) and toggle menu
        var aOpenElements = $(".bestseller-list ul.open");
        var self = this;
        if ( (aOpenElements.length >0) && !($(this).next("ul").hasClass("open")) ) {
            if ( (!($(self).next("ul").hasClass("last-dropdown"))) && ($(".bestseller-list ul.last-dropdown").hasClass("open")) ) {bToggleBottomDiv = true;}
            $(".bestseller-list ul.open").slideToggle(iSlideTiming).toggleClass("open");
        }
        $(this).next("ul").slideToggle(iSlideTiming,function(){
            if (!($(self).hasClass("active"))) {
                $(".bestseller-list>li>a").removeClass("active"); 
                $(".bestseller-list>li").removeClass("active");
                if (bToggleBottomDiv) {
                    bToggleBottomDiv = false; 
                    toggleBottomShadowColor('bestseller-list');
                }
            }
            $(self).toggleClass("active");
            $(self).parent().toggleClass("active");
            

        }).toggleClass("open");

        return false;
    });
    

    ///
    ///this is in order to show menus that are set to active on load
    ///
    /// all you need to do is to give the corresponding <a> tag the class active. It will spread the necessary classes. 
    $('.bestseller-list>li>a').each(function(){
        if ($(this).hasClass("active")){ 
            $(this).parent().removeClass("active").addClass("active"); //give parent li element class active even if not yet set
            $(this).next("ul").removeClass("open").addClass("open");  //give ul class open even if not yet set
            $(this).next("ul").show();    // show ul
        }
    });
    
    ////*******************END Bestseller List Functions **************//////////////////////////////////////


    //////************ START Main Navi functions fo rollable Navi ***************////////////////////////////
    
    ////
    //// This is for the control of the Main Navi if there are more than 1 Main Navi UL elements
    ////
    
    function toggleMainNaviRollableShadowColor(oThisUl) {
        var iThisUlIndex = $("#MainNaviRollable ul.main-navi-rollable").index(oThisUl)+1; //find no of the list toggled
        $('#MainNaviRollable>.is-main-navi-bottom').each(function(){ 
            if ($(this).hasClass("is-no-" + iThisUlIndex)){ 
                if ($(this).hasClass("main-navi-list-bottom-dark")) {$(this).removeClass("main-navi-list-bottom-dark").addClass("main-navi-list-bottom");}
                else {$(this).removeClass("main-navi-list-bottom").addClass("main-navi-list-bottom-dark");}
            }
        });
    }
    
    ////
    ////functions to slidetoggle main navi
    ////
    jQuery('#MainNaviRollable>ul>li>a').click(function(){    
        
        var oThisParentUl = $(this).parent().parent();
        var self = $(this);

        var iIndexOfThisUl = $("#MainNaviRollable>ul").index(oThisParentUl)+1;
        var iIndexOfActiveUl = $("#MainNaviRollable>ul").index($('#MainNaviRollable>ul.active'))+1;
        (iIndexOfThisUl == iIndexOfActiveUl) ? bTogglingActiveUl = true : bTogglingActiveUl = false;

        //close any open uls
        if (!bTogglingActiveUl && ($('#MainNaviRollable>ul.active').length >0)) { //this is only the case if any UL is open at the moment of clicking and a non-active ul>li>a is clicked

            $('#MainNaviRollable>ul.active>li>ul').slideToggle();
            $('#MainNaviRollable>ul.active>li>a').removeClass("active");

            $('#MainNaviRollable>.is-main-navi-bottom').each(function(){
                if ($(this).hasClass("is-no-" + iIndexOfActiveUl)){ $(this).toggle(); }
            });
            $('#MainNaviRollable>ul.active').removeClass("active");

        }
            
        $(oThisParentUl).toggleClass("active");
        $(this).next("ul").slideToggle(
            function(){$(self).toggleClass("active");});
    
        $('#MainNaviRollable>.is-main-navi-bottom').each(function(){
            if ($(this).hasClass("is-no-" + iIndexOfThisUl)){ $(this).toggle(); }
        });
        
        return false;
    });
    
    ////
    ////functions to toggle main navi sub menu level 1
    ////
/*    jQuery('#MainNaviRollable>ul>li>ul>li>a').click(function(){    
        var oNextUl = $(this).next("ul");
        if (oNextUl.length>0) {$(this).addClass("toggling"); }
        var self = this;
        $(this).toggleClass("active");
        if ($(this).hasClass("last")) {
            toggleMainNaviRollableShadowColor($(this).parent().parent().parent().parent());
            if ($(this).hasClass("last") && oNextUl.length == 0) {
                $(this).toggleClass("last-active-no-submenus"); 
            }
        }
        if (oNextUl.length>0) {
            $(this).next('ul').slideToggle(function(){ 
                $(self).removeClass("toggling");
             }).toggleClass("active");
        }

        return false;
    });
    
    ////
    ////function to slidetoggle main navi sub menu level 2
    ////
    jQuery('#MainNaviRollable>ul>li>ul>li>ul>li>a').click(function(){
        var self = this;
        var oNextUl = $(this).next('ul');
        
        if (oNextUl.length) {
            oNextUl.slideToggle(function(){ $(self).toggleClass("active"); });
        }
        else { $(self).toggleClass("active"); }
        return false;
    });

    ///
    ///last level can only be active or not, no toggling, no border crap
    ///
    jQuery('#MainNaviRollable>ul>li>ul>li>ul>li>ul>li>a').click(function(){
        $(this).toggleClass("active"); 
        return false;
    });*/
    
    ////
    ////functions to control tabs on product detail view
    ////
    
    ////
    ////get the element that is active on page load in order to restore later
    ////
    var oActiveTabWineInfo = $('.wine-info-tabs>ul>li>a.active');
    var oActiveTabVintnerInfo = $('.vintner-info-tabs>ul>li>a.active');
    var bTabMovementActive = false; //var that keeps from early restorement of the initially Active Tab
    var tTabTimeout = window.setTimeout(function(){},1);
    
    function restoreActiveTab (sTabCategory, oActiveTabToRestore) {
        return true;
        if (!(bTabMovementActive)) {
            $('.' + sTabCategory + '-info-tabs>ul>li>a').removeClass("active");
            $(oActiveTabToRestore).addClass("active");
        }
    }
    
    $('.wine-info-tabs>ul>li').click(function(){
//        window.clearTimeout(tTabTimeout);
        $('.wine-info-tabs>ul>li>a').removeClass("active"); 
        $(this).find("a").addClass("active");
        $('.wine-profile').removeClass('wine-profile-active');
        var oShow = $( $(this).find("a").attr('href') ).addClass('wine-profile-active');
        if ($($(this).find("a").attr('href')).hasClass("has-light-bottom-shadow")) {$('.wine-info-tabs .wine-info-bottom').removeClass("wine-info-bottom").addClass("wine-info-bottom-light")}
        else {$('.wine-info-tabs .wine-info-bottom-light').removeClass("wine-info-bottom-light").addClass("wine-info-bottom")}
//    },function(){
//        tTabTimeout = window.setTimeout(function(){restoreActiveTab('wine', oActiveTabWineInfo);},850);
    });
    
    $('.wine-info-tabs>ul>li').click(function(){
        return false; 
    });

    
    ////
    ////Tab functions for vintner info
    ////
    $('.vintner-info-tabs>ul>li').hover(function(){
        //window.clearTimeout(tTabTimeout);
        //$('.vintner-info-tabs>ul>li>a').removeClass("active"); 
        //$(this).find("a").addClass("active");
    },function(){
        //tTabTimeout = window.setTimeout(function(){restoreActiveTab('vintner', oActiveTabVintnerInfo);},850);
    });
    
    ///
    ///Tab function for vintner info - change bottom shadow
    ///
    $('.vintner-info-tabs>ul>li>a').click(function(){
        if ($(this).attr("rel") == 'wines') {
            var oBottomThing = $('.vintner-info-tabs .vintner-info-bottom');
            $(oBottomThing).attr("class", "vintner-info-bottom-light");
        }
        else {
            var oBottomThing = $('.vintner-info-tabs .vintner-info-bottom-light');
            $(oBottomThing).attr("class", "vintner-info-bottom");
        }
    });
    

    ////
    ////lightbox
    ////
    $('.lightbox-link').lightBox({fixedNavigation:true});


    ////
    ////functions for newsletter dropdown
    ////
    $('.col-3>.newsletter-bar>a, .col-1>.newsletter-bar>a').click(function(){
        $(this).toggleClass("active");
        $(this).next("div.newsletter-register").slideToggle();
        return false;
    });
    
    
    /// Winzer Tabs
    $('.vintner-info-tabs ul.tabs li a').click(function() {
        
        ident = $(this).attr('rel');
        
        if(ident == 'profile') {
            $('#vintner-profile').show();
            $('#vintner-wines, #vintner-wines-top').hide();
        }
        else {
            $('#vintner-profile').hide();
            $('#vintner-wines, #vintner-wines-top').show();
        }
        
        $('.vintner-info-tabs>ul>li>a').removeClass("active"); 
        $(this).addClass("active");
        
        return false;
    })
    
    ////
    ////fix WYSIWYG markup
    ////
    $('font[size]').each(function(){
        var sSize = $(this).attr("size");
        $(this).parent().addClass("font-size-" + sSize);
    });
    
});


////
////functions for newsletter form
////
function checkHideValue(obj, val) {
    
    if ($(obj).attr("value") == val) {
        
        var string = $(obj).attr("value");
        $(obj).attr("value","");
        
    }
    
}

function checkShowValue(obj, val) {
    
    if ($(obj).attr("value") == '') $(obj).attr("value",val);
}

function SubmitForm(sFormName) {
	if (sFormName) {
		document.forms[sFormName].submit();
	}
}
