﻿/* This is added via the CMS to the parent page to override the global navigation; it is here for reference only. */
var yh =  yh  || {};

/* pawel maziarz: work around for ie logging */
if (!window.console) {
    var console = {
        log: function(msg) {
        }
    }
}

(function() {
	var backgroundImageUrlDefault = overrideImagePath;
    var iframeID = jQuery('iframe#YHIframe');
	var overlayID = jQuery('#hdp-content-overlay');
	
    yh.navigationOverride = {
		
		wrapImagePath: function (backgroundImageUrl) {			 
			var imageReplacementForFlash = 'url(' + backgroundImageUrl + ')';
			return imageReplacementForFlash;
        },
		imageForContext: function() {		
		    return yh.navigationOverride.wrapImagePath(backgroundImageUrlDefault);	
		},
		noMenusCurrentlyOpen: function(){
			if(typeof (overlayID.data('mainMenuOpen')) == 'undefined' &&
			   typeof (overlayID.data('countryListOpen')) == 'undefined' &&
			   typeof (overlayID.data('faqOpen')) == 'undefined'){
			   return true;
			}else{
				return false;
			}
		},
		hideIframe: function hideIframe(currentImage) {
			console.log(overlayID.data('mainMenuOpen'));
			iframeID.css('visibility', 'hidden');
			if(currentImage !== ""){
				jQuery('#hdp-content').css('background-image', currentImage);
			}
		},	
		showIframe: function() {			
			if (yh.navigationOverride.noMenusCurrentlyOpen()) {
				iframeID.css('visibility', 'visible');
				jQuery('#hdp-content').css('background-image', 'none');
			}
		},
		showCheekyOverlay: function() {
			if (typeof (overlayID.data('doNotShowOverlay')) == 'undefined') {
				overlayID.filter(':hidden')
					.css({'height' : '517px','opacity': 0,'display':'block','z-index': '-1'})
					.end()
					.stop(false, true)
					.animate({ 'opacity': 0.7 }, 1000);
			}
		},
		hideCheekyOverlay : function() {
			if (yh.navigationOverride.noMenusCurrentlyOpen() || overlayID.data('doNotShowOverlay') === true) {
				overlayID
					.stop()
					.animate({
						opacity: 0
					},{
						duration:500,
						easing:'easeInSine',
						complete:function(){
							jQuery(this).css('display', 'none');}
					});
			}
		},

        init: function() {           
            var imageDefault = new Image();
            imageDefault.src = backgroundImageUrlDefault;
               
            var oldShowOverlayFunction = hdp.GlobalNavigation.showOverlay;
            hdp.GlobalNavigation.showOverlay = function() {
                oldShowOverlayFunction();
				
				overlayID.data('mainMenuOpen', true);				
				var currentImage = yh.navigationOverride.imageForContext();
				yh.navigationOverride.hideIframe(currentImage);									
            };

            var oldHideOverlayFunction = hdp.GlobalNavigation.hideOverlay;
            hdp.GlobalNavigation.hideOverlay = function() {
                oldHideOverlayFunction();
				
				overlayID.removeData('mainMenuOpen');
                yh.navigationOverride.showIframe();	
				yh.navigationOverride.hideCheekyOverlay();				
            };
			
			var oldCountryListClose = hdp.selectList.close;
			hdp.selectList.close = function() {
                oldCountryListClose('hdp-country-list');
				
				overlayID.removeData('countryListOpen');				
                yh.navigationOverride.showIframe();	
				yh.navigationOverride.hideCheekyOverlay();
            };
			
            var oldCountryListToggle = hdp.selectList.openClose;
            hdp.selectList.openClose = function(e) {
                oldCountryListToggle(e, 'hdp-country-list');
				
                if (jQuery('div.hdp-country-list-div.hdp-list-open').length) {
					overlayID.data('countryListOpen', true);					
					var currentImage = yh.navigationOverride.imageForContext();					
					yh.navigationOverride.hideIframe(currentImage);
					yh.navigationOverride.showCheekyOverlay();	
					jQuery('#hdp-footer').css('z-index','6000');
                } else {
					overlayID.removeData('countryListOpen');
                    yh.navigationOverride.showIframe();	
					yh.navigationOverride.hideCheekyOverlay();															
                }
            };
            
            var oldFAQloading = hdp.FAQ.loading;
            hdp.FAQ.loading = function() {
				overlayID.data('faqOpen', true);
				overlayID.data('doNotShowOverlay', true);
				yh.navigationOverride.hideCheekyOverlay();
                yh.navigationOverride.hideIframe();				
				
				return oldFAQloading();
            };

			var oldFAQHide = hdp.FAQ.hide;
            hdp.FAQ.hide = function() {
                oldFAQHide();
				
				overlayID.removeData('faqOpen');	
                overlayID.removeData('doNotShowOverlay');
				yh.navigationOverride.showIframe();							
            };
        }
    };
})();

// As IE doesn't when setting the document.domain of the iframe (security) 
// Override global navigation behaviour so that iframe (with flash content) does not conflict with navigation.
$(document).ready(function() { yh.navigationOverride.init(); });
//document.getElementById("YHIframe").onload = function(){
//    yh.navigationOverride.init();	
//};
