/** 
 * @fileOverview This file is to be included in all full pages that use the OvelayContents.jspf.
 */
dojo.require("dijit.Dialog");
var hiddenContent = null; 

OverlayContentsJS = {
	
	preparePopupStyle :function(style){
    //if present from previous overlay
	dojo.removeClass('overlay_popup' , 'variable');	
	if(style == 'variable'){
	   dojo.addClass('overlay_popup' , 'variable');
	 }
	},
	
	//this function will init loading dynamic content. Dojo limitation that the resource to be loaded (url) cannot be external!!
	showAjaxPopup :function (event, url, style){
	   if(event == null || (event != null && event.type!="keypress") || (event != null && event.type=="keypress" && event.keyCode==13)){
	     OverlayContentsJS.preparePopupStyle(style);
	     OverlayContentsJS.loadHTML(event, url);
	     OverlayContentsJS.displayPopup();
	     }
	},
	
	//this function will init content from a hidden div embeded within the page
	showPopup : function(event, content, style){

	   if(event == null || (event != null && event.type!="keypress") || (event != null && event.type=="keypress" && event.keyCode==13)){
	     OverlayContentsJS.preparePopupStyle(style);
	     OverlayContentsJS.loadContent(event, content);
	     OverlayContentsJS.displayPopup();
	   }
	},
	
	//this function will load the content into the popup
	loadContent: function(event, content){
	    hiddenContent = dojo.clone(dojo.byId(content));
	    hiddenContent.style.display='block';
	    dojo.byId('content').innerHTML = hiddenContent.innerHTML;
		var title = event.getAttribute('popup_title');
		if(title != null && title != ""){
		  dojo.byId('title').innerHTML = '<span>' + title +'</span>';
		 }  
	},
	
	//this function will display the popup
	displayPopup: function (){
		dijit.byId('overlay_popup').closeButtonNode.style.display='none';
        dijit.byId('overlay_popup').show();	       
          
		dojo.query('.dijitDialogUnderlayWrapper', document).forEach(function(tag) {			    	
				tag.style.display='block';
			}).onclick(function(evt){
			    OverlayContentsJS.hidePopup('overlay_popup', evt);
			});	
			
	},
	
	/** 
	 * Reset all contents of the Overlay popup. Also stops any background sound if video was playing (IE)
	 * This dialog will be re-used across all products on the page.
	 */			
	resetPopUp: function() {
	       hiddenContent = null;
           dojo.byId('content').innerHTML = "";
           dojo.byId('title').innerHTML = "";
	},
		
	/** 
	 * Hides the Product Quick Info pop-up.
	 * 
	 * @param {string} id The id of the Product Quick Info pop-up to hide.
	 * @param {object} event The event triggered from user actions.
	 */		
	hidePopup: function(id,event){
	    //dispaly=none on all .dijitDialogUnderlayWrapper present on the DOM
		dojo.query('.dijitDialogUnderlayWrapper', document).forEach(function(tag) {	
		    tag.style.display='none';
			});	
			if(event!=null && event.type=="keypress" && event.keyCode!="27"){
					return;
				}else{		
					var quickInfo = dijit.byId(id);
					if(quickInfo != null){
						quickInfo.hide();
						this.resetPopUp();
					}
				}
	},

	
	/** 
	 * Triggers a call to the hidePopup() function after a delay of a certain amount of time.
	 */			
	delayHidePopup: function() {
		setTimeout(dojo.hitch(this,"hidePopup",'overlay_popup'),200);
	},
	/**
	* Load html. If error occurs message and URL is displayed in the console. Firefox/Firebug.
	* This is to assist with troublshooting.
	*/
	loadHTML: function(event, loadValue){
	        dojo.xhrGet ({
	                // The page that parses the POST request
	                url: loadValue,
	                // Loads this function if everything went ok
	                load: function (data) {
	                        dojo.byId("content").innerHTML = data;
														
							var title = event.getAttribute('popup_title');
								if(title != null && title != "") {
								  dojo.byId('title').innerHTML = '<span>' + title +'</span>';
		                        }

	                },
	                // Call this function if an error happened
	                error: function (error) {
	                       console.log  ('Error: ', error.message);
	                       console.log  ('Possible missing FS content', loadValue);
	                }
	        });
	},
	//link to anchor points within the iframe, script called from the i-frame
	goAnchor: function(id){
        anchorPos = document.getElementById('sizeguideframe').contentWindow.document.getElementById(id).offsetTop;
		document.getElementById('sizeguideframe').contentWindow.document.body.scrollTop = anchorPos;	


		}

}
