﻿/*	--[ multiple pop ups ]----------------------------------------------------	*/

//	PUT CUSTOM POSITIONING BACK IN

/*	--[	variables ]-----------------------------------------------------------  */

	var oPopUpDrag			= null;						//	store temporary drag element
	var oPopUpCurrent		= null;						//	current popup being dragged
	var sPopUpTitleDefault	= "More Information...";	//	default text for popup title
	var sPopUpWidthDefault	= 730;						//	default width for popups
	var sPopUpHeightDefault	= 450;						//	default height for popups
	var iZIndex				= 2000000;					//	zindex stack order to insure current popup stays ontop
	var aPopUpCollection	= new Array();				//	collection of popups created
	var allowMultiples		= false;					//	enable multiple popups - INPORTANT this var shouldn't be changed, it should be set in <head> of individual pages
	
	//	normal/mouseout image src
	var oPopUpBtnNormal		= new Image();
	oPopUpBtnNormal.src     = popupNormalCloseButtonURL;
	
	//	active/mouseover image src
	var oPopUpBtnActive		= new Image();
	oPopUpBtnActive.src     = popupActiveCloseButtonURL;
	
/*	--[	increment stacking order (z-index) to display window over others ]----  */

	function updateStackOrder(oPopUp) {
		oPopUp.style.zIndex = iZIndex ++;
	}
	
/*	--[	check the scroll top position of document ]---------------------------  */

	function checkScrollTop() {
		if (document.documentElement && document.documentElement.scrollTop) {
			return document.documentElement.scrollTop;
		} else if (document.body) {
			return document.body.scrollTop;
		} else {
			return window.pageYOffset;
		}
	}

/*	--[	create new popup ]----------------------------------------------------  */

	
/*	--[	popup dragging ]------------------------------------------------------  */

	function setPopUpDrag(e) {
	    //	create temporary drag layer 
		if (oPopUpDrag == null) {
			oPopUpDrag = document.createElement("div");
			oPopUpDrag.setAttribute("id", "PopUpPhoneView_Drag");
			document.body.appendChild(oPopUpDrag);
		}
	
	    //	set temporary drag element position and dimensions.  The - 2 is to compensate
		//	for the border size on the temporary drag element
		oPopUpDrag.style.left			= getPageOffsetLeft(oPopUpCurrent) + "px";
		oPopUpDrag.style.top			= getPageOffsetTop(oPopUpCurrent) + "px";
		oPopUpDrag.style.height			= (oPopUpCurrent.offsetHeight - 2) + "px";
		oPopUpDrag.style.width			= (oPopUpCurrent.offsetWidth - 2) + "px";
		
		//	set starting position of click event
		oPopUpDrag.iClientX				= e.clientX;
		oPopUpDrag.iClientY				= e.clientY;
		
		//	hide window and update temporary drag elements zindex and show
		updateStackOrder(oPopUpDrag);
		
		//  oPopUpCurrent.style.visibility	= "hidden";
		    
		oPopUpDrag.style.visibility		= "visible";
	}

	function dragPopUp(e) {
		//	store the event element
		var oTitle = getEventTarget(e);	//.srcElement;
		
		//	if the title doesn't have a class name of "PopUp_Title" loop back and 
		//	find.  If it doesn't exist then exit.
		while (oTitle.className != "PopUpPhoneView_Title" && oTitle.tagName != "HTML") {
			oTitle = oTitle.parentNode
		}
		if (oTitle.className != "PopUpPhoneView_Title") {
			return;
		}
		
		//	store the current popup window
		oPopUpCurrent = oTitle.parentNode;
		
		while (oPopUpCurrent.className != "PopUpPhoneView" && oPopUpCurrent.tagName != "HTML") {
			oPopUpCurrent = oPopUpCurrent.parentNode;
		}
		
		//	setup the temporary drag element
		setPopUpDrag(e);

		//	get starting position of popup
		oPopUpDrag.iOffsetLeft  = getPageOffsetLeft(oPopUpDrag);
		oPopUpDrag.iOffsetTop   = getPageOffsetTop(oPopUpDrag);
		oPopUpDrag.style.cursor = "move";
		
		//	start event capture
		objectAttachEvent(document, "mousemove", movePopUp);
		objectAttachEvent(document, "mouseup", dropPopUp);
	}
	
	function movePopUp(e) {
		//	get current cursor position
		var iClientX	= e.clientX;
		var iClientY	= e.clientY;
		
		//	get top and left positions
		var iLeft		= parseInt((oPopUpDrag.iOffsetLeft + iClientX) - oPopUpDrag.iClientX);
		var iTop		= parseInt((oPopUpDrag.iOffsetTop + iClientY) - oPopUpDrag.iClientY);

		//	check that popup isn't dragged out side of innerwindow
		var iScrollTop = checkScrollTop();
		//	left of screen
		if (iLeft < 0) { iLeft = 0 };
		//	top of screen
		if (iTop < 0) { iTop = 0 };
		//	right of screen
		if ((iLeft + oPopUpCurrent.iPopUpWidth) > iClientInnerWidth) { iLeft = iClientInnerWidth - oPopUpCurrent.iPopUpWidth; }
		//	bottom of screen
		if ((iTop + oPopUpCurrent.iPopUpHeight) > iClientInnerHeight + iScrollTop) { iTop = (iClientInnerHeight + iScrollTop) - oPopUpCurrent.iPopUpHeight; }
		
		//	set temporary drag element position
		oPopUpDrag.style.left = iLeft + "px";
		oPopUpDrag.style.top  = iTop + "px";
	
		//	stop events bubbling
		if (window.event) {
			window.event.cancelBubble	= true;
			window.event.returnValue	= false;
		} else if (e && e.preventDefault && e.stopPropagation) {
			e.preventDefault();
			e.stopPropagation();
		}
	}
	
	function dropPopUp() {
		//	stop event capture
		objectDetachEvent(document, "mousemove", movePopUp);
		objectDetachEvent(document, "mouseup", dropPopUp);
		
		//	if oPopUpCurrent is null exit
		if (!oPopUpCurrent) {
			return;
		}
		
		//	updated stored popup positions to temporary drag element positions
		oPopUpCurrent.iPopUpLeft		= getPageOffsetLeft(oPopUpDrag);
		oPopUpCurrent.style.left		= oPopUpCurrent.iPopUpLeft + "px";
		oPopUpCurrent.iPopUpTop			= getPageOffsetTop(oPopUpDrag);
		oPopUpCurrent.style.top			= oPopUpCurrent.iPopUpTop + "px";

		//	remove the scroll top value from the stored position value
		var iScrollTop = checkScrollTop();
		oPopUpCurrent.iPopUpTop			= oPopUpCurrent.iPopUpTop - iScrollTop;

		//	hide temporary drag element and show popup and update it's zindex
		oPopUpDrag.style.visibility		= "hidden";
		//  oPopUpCurrent.style.visibility	= "visible";
		updateStackOrder(oPopUpCurrent);
		
		//	clear current popup
		oPopUpCurrent					= null;
	}
	
/*	--[	update the popup position ]-------------------------------------------  */

	function updatePopUpPosition(oPopUp) {
		getClientInner();

		//	store page scroll capture
		var iScrollTop = checkScrollTop();
		
		//	set popup left position
		if (!oPopUp.iPopUpLeft) oPopUp.iPopUpLeft		= (iClientInnerWidth - oPopUp.offsetWidth) / 2;
		if (oPopUp.iPopUpLeft < 0) oPopUp.iPopUpLeft	= 10;
		oPopUp.style.left								= parseInt(oPopUp.iPopUpLeft) + "px";
		
		//	set popup top position
		if (!oPopUp.iPopUpTop) oPopUp.iPopUpTop			= (iClientInnerHeight - oPopUp.offsetHeight) / 4;
		if (oPopUp.iPopUpTop < 0) oPopUp.iPopUpTop		= 10;
		oPopUp.style.top								= parseInt(oPopUp.iPopUpTop + iScrollTop) + "px";
	}
	
/*	--[	update visible popups positions when the window scrolls ]-------------  */

	function scrollPopUps() {
		for (var i = 0; i < aPopUpCollection.length; i++) {
			var oPopUp = aPopUpCollection[i];
			if (oPopUp.style.visibility == "visible") {
				updatePopUpPosition(oPopUp);
			}
		}
	}
	
/*	--[	reset popup positions and hide when the window resizes ]--------------  */

	function resetPopUpPosition() {
		var iScrollTop = checkScrollTop();
		
		//	capture client inner window dimensions
		var iClientInnerWidthTemp = (document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
		if (iClientInnerWidthTemp != iClientInnerWidth || !$.browser.msie) {
			for (var i = 0; i < aPopUpCollection.length; i++) {
				var oPopUp = aPopUpCollection[i];
				oPopUp.style.visibility = "hidden";
				oPopUp.iPopUpLeft		= null;
				oPopUp.iPopUpTop		= null;
				oPopUp.style.top		= "0px";
				oPopUp.style.left		= "0px";
				
			}
		}
	}

/*	--[	initilise on window load ]--------------------------------------------  */
function initPopUp() {
$("#mm_bkr img.phone_info").click(function(){        
	    displayPopupPhoneView("popupid_phoneview","More Information...",'<div id="max_mega_popup"></div>',"false","false","730","550","#E6EFF6");
		$("#max_mega_popup").append($(".SearchPhoneView:first"));
		$("#max_mega_popup").append($(".SearchGiftView:first"));
		$("#max_mega_popup").append($(".SearchNetworkView:first"));
		$("#max_mega_popup").append($(".SearchDiscountView:first"));
		$("#ajaxLoaded").css({'display':'block'});		
	})
}

//ITWR-2179
function displayManufacturerInfo(id, title, body, isUrl, disableDrag, width, height, bgColor)
 {
      
     
	    displayPopupPhoneView(id,title,body,isUrl,disableDrag,width,height,bgColor);

		
		$("#max_mega_popup").append($(".SearchPhoneView:first"));
		$("#max_mega_popup").append($(".SearchGiftView:first"));
		$("#max_mega_popup").append($(".SearchNetworkView:first"));
		$("#max_mega_popup").append($(".SearchDiscountView:first"));
		$("#ajaxLoaded").css({'display':'block'});				
	
}
function displayMoreInfoPopUp() {
      
	    displayPopupPhoneView("popupid_phoneview","More Information...",'<div id="max_mega_popup"></div>',"false","false","730","550","#E6EFF6");
		$("#max_mega_popup").append($(".SearchPhoneView:first"));
		$("#max_mega_popup").append($(".SearchGiftView:first"));
		$("#max_mega_popup").append($(".SearchNetworkView:first"));
		$("#max_mega_popup").append($(".SearchDiscountView:first"));
		$("#ajaxLoaded").css({'display':'block'});		
	
}


//	function initPopUp() {
//	    
//	    $("#mm_bkr img.phone_info").click(function(){
//	    displayPopup("popupid_phoneview","More Information...",'<div id="max_mega_popup"></div>',"false","false","730","300","#E6EFF6");
//		$("#max_mega_popup").append($(".SearchPhoneView:first"));
//		//$("#ajaxLoaded").css({'display':'block'});
//	})
//		//	potentiall need to check for multiple tag types
//		var	aANCHORs = document.getElementsByTagName("A");

//		for (var i = 0; i < aANCHORs.length; i++) {
//			if (aANCHORs[i].getAttribute("popupPhoneViewid")) {
//			    objectAttachEvent(aANCHORs[i], "click", function() {
//				
//				    var oANCHOR = null;
//					
//					if (window.event) {
//						var oANCHOR = window.event.srcElement;
//						while (oANCHOR.tagName != "A" && oANCHOR.tagName != "HTML") {
//							oANCHOR = oANCHOR.parentNode;
//						}
//					} else {
//						var oANCHOR = this;
//					}
//			
//					if (!allowMultiples && aPopUpCollection.length > 0) {
//						for (var j = 0; j < aPopUpCollection.length; j++) {
//							aPopUpCollection[j].style.visibility = "hidden";
//						}
//					}				
//					
//					var oPopUp;
//					
//					//	store id
//					var id = oANCHOR.getAttribute("popupPhoneViewid");

//					initPopUpBuild(oANCHOR, id);
//				});
//			}
//		}
//	}

    

	function closePopUp(e, key) {
	    if (allowMultiples == true) {
			return;
		}
		
		var iKeyCode = null;
		
		if (key) {
		    iKeyCode = key;
		} else {
		    iKeyCode = (window.event) ? event.keyCode : e.keyCode; 
		}
		
		var iEscKey  = (window.event) ? 27 : e.DOM_VK_ESCAPE;
		
		if (iKeyCode == iEscKey) {
			for (var i = 0; i < aPopUpCollection.length; i++) {
				if (aPopUpCollection[i].style.visibility == "visible") {
				    //  clear iframe content
                    if (aPopUpCollection[i].oPopUpContentInner) {
                        aPopUpCollection[i].oPopUpContentInner.src = "/_blank.htm";
                        //   aPopUpCollection[i].oPopUpContentInner.src = "javascript:false; document.write('');";
                    }
				    aPopUpCollection[i].style.visibility = "hidden";
				    if ($.browser.msie) {
						toggleSelects(document, false);
					}
				}
			}
		}
	}


    function displayAlert(message)
	{
	    var display = false;
	    if(display==true)
	        alert(message);
	}
	
	//  ADD "bgColor"
	function displayPopupPhoneView(id, title, body, isUrl, disableDrag, width, height, bgColor)
	{
	    
	    //Set default values
	    if(width==0)
	        width=sPopUpWidthDefault;
	        
	    if(height==0)
	        height=sPopUpHeightDefault;
 
	    //  alert('This will be a popup: ' + title + ' -> ' + body);
	    
	    //  ------------------------------------------------------------------
	    //  BUILD POPUP
	    //  ------------------------------------------------------------------
	    
	    var oPopUp = document.getElementById(id);
	    
	    //  check to see it popup already exists
	    if (!oPopUp) {
	        //	container
		    oPopUp = document.createElement("div");
		    oPopUp.className = "PopUpPhoneView";
		    oPopUp.setAttribute("id", id);
		    oPopUp.onclick = function () {
		        updateStackOrder(this);
		    }
		    document.body.appendChild(oPopUp);
    		
		    //	header
		    oPopUp.oPopUpHeader = document.createElement("div");
		    oPopUp.oPopUpHeader.className = "PopUpPhoneView_Header"
		    oPopUp.oPopUpHeader.oPopUp = oPopUp;
		    oPopUp.appendChild(oPopUp.oPopUpHeader);
    		
		    //	title
		    oPopUp.oPopUpTitle = document.createElement("div");
		    oPopUp.oPopUpTitle.className = "PopUpPhoneView_Title";
		    oPopUp.oPopUpTitle.innerHTML = "<h4>" + title + "</h4>";
		    oPopUp.oPopUpHeader.appendChild(oPopUp.oPopUpTitle);
    		
		    //	disabled drag - if true don't attach drag event and change cursor
		    if (disableDrag == "true") {
			    oPopUp.oPopUpTitle.style.cursor = "default";
		    } else {
			    objectAttachEvent(oPopUp.oPopUpHeader, "mousedown", dragPopUp);
		    }
    		
		    //	close button container
		    oPopUp.oPopUpBtnClose = document.createElement("a");
		    oPopUp.oPopUpBtnClose.href = "JavaScript:void(0);";
		    oPopUp.oPopUpBtnClose.className = "PopUpPhoneView_BtnCloseNormal";
		    oPopUp.oPopUpBtnClose.oPopUp = oPopUp;
		    oPopUp.oPopUpHeader.appendChild(oPopUp.oPopUpBtnClose);	
    		
		    //  close button image
		    oPopUp.oPopUpBtnCloseImg = document.createElement("img");
		    oPopUp.oPopUpBtnCloseImg.src = oPopUpBtnNormal.src;
		    oPopUp.oPopUpBtnClose.appendChild(oPopUp.oPopUpBtnCloseImg);
    		
		    //	close button events
		    oPopUp.oPopUpBtnCloseImg.onmouseover = function() { this.src = oPopUpBtnActive.src; }
		    oPopUp.oPopUpBtnCloseImg.onmouseout = function() { this.src = oPopUpBtnNormal.src; }
		    
		    oPopUp.oPopUpBtnClose.onclick = function() {
		        closePopUp(this);
		        return false;
		    }
    		
    		//  store isUrl value
    		oPopUp.isUrl = isUrl;
    		
		    //	content (outer)
		    oPopUp.oPopUpContent = document.createElement("div");
		    oPopUp.oPopUpContent.className = "PopUpPhoneView_ContentArea";
		    if (bgColor != null && oPopUp.isUrl == "true") {
		        oPopUp.oPopUpContent.style.backgroundColor = bgColor;
		    }
		    oPopUp.appendChild(oPopUp.oPopUpContent);
    		
		    //	content (inner)
		    //	iframe
		    if (oPopUp.isUrl == "true") {
			    oPopUp.oPopUpContentInner = document.createElement("IFRAME");
			    oPopUp.oPopUpContentInner.className = "PopUpPhoneView_ContentAreaInnerIframe";
			    oPopUp.oPopUpContentInner.frameBorder = 0;

		    //	inline content
		    } else {
			    oPopUp.oPopUpContentInner = document.createElement("div");
			    oPopUp.oPopUpContentInner.className = "PopUpPhoneView_ContentAreaInner";
			    oPopUp.oPopUpContentInner.innerHTML = body;
		    }
    	    oPopUp.oPopUpContent.appendChild(oPopUp.oPopUpContentInner);
            
            //	set popup and content inner dimensions
		    oPopUp.style.width = width + "px";
		    oPopUp.iHeightExtra = parseInt(height - oPopUp.oPopUpHeader.offsetHeight - 10);
		    oPopUp.style.height = height - 4 + "px";
		    oPopUp.oPopUpContentInner.style.height = (height - oPopUp.oPopUpHeader.offsetHeight - 14) + "px";	//	14 = 5 for margin top, 5 margin bottom, 2 border top and 2 border bottom
		    oPopUp.oPopUpContentInner.style.width = (width - 14) + "px";										//	14 = 5 for margin top, 5 margin bottom, 2 border top and 2 border bottom
            
            //	set content and make visible
		    if (oPopUp.isUrl == "false") {
			    for (var j = 0; j < oPopUp.oPopUpContentInner.childNodes.length; j++) {
			        if (oPopUp.oPopUpContentInner.childNodes[j].nodeType == 1 && oPopUp.oPopUpContentInner.childNodes[j].className.search("PopUpPhoneView_Content") != -1) {
			            oPopUp.oContent = oPopUp.oPopUpContentInner.childNodes[j];
					    oPopUp.oContent.style.display = "block";
					    oPopUp.oContent.style.height = oPopUp.iHeightExtra + "px";
					    oPopUp.oContent.style.width = (oPopUp.offsetWidth - 18) + "px";
					    break;
				    }
			    }
		    }
		    /*
	        //	resize popup size if it exceeds the window height
	        if (oPopUp.iPopUpHeight > iClientInnerHeight) {
	            //  container
	            var iHeight = parseInt(iClientInnerHeight - 20);
	            obj.oPopUp.style.height = iHeight + "px";
	            //  content (outer and inner)
		        if (oPopUp.oContent) { oPopUp.oContent.style.height = parseInt(iHeight - oPopUp.oPopUpHeader.offsetHeight - 10) + "px"; }
		        if (oPopUp.oPopUpContentInner) { oPopUp.oPopUpContentInner.style.height = parseInt(iHeight - oPopUp.oPopUpHeader.offsetHeight - 10) + "px"; }
		        //	flag as being resized
		        oPopUp.isResized = true;

	        } else if (oPopUp.iPopUpHeight < iClientInnerHeight && oPopUp.isResized == true) {
	            //  container
		        oPopUp.style.height = oPopUp.iPopUpHeight + "px";
		        //  content (outer and inner)
		        if (oPopUp.oContent) { oPopUp.oContent.style.height = oPopUp.iPopUpHeightExtra + "px"; }
		        if (oPopUp.oPopUpContentInner) { oPopUp.oPopUpContentInner.style.height = oPopUp.iPopUpHeightExtra + "px"; }
		        //	remove resize flag flag as being resized
		        oPopUp.isResized = false;
	        }
	        */
	    }
	    
	    //	content (inner)
	    //	iframe
	    if (oPopUp.isUrl == "true") {
		    oPopUp.oPopUpContentInner.src = body;
	    }
	    
	    //	update pop up position and zindex
	    if (!allowMultiples) {
		    oPopUp.iPopUpLeft	= (iClientInnerWidth - oPopUp.offsetWidth) / 2;
		    oPopUp.iPopUpTop	= (iClientInnerHeight - oPopUp.offsetHeight) / 4;
	    }
	    updatePopUpPosition(oPopUp);
	    updateStackOrder(oPopUp);
		
	    //	store popup
	    aPopUpCollection[aPopUpCollection.length] = oPopUp;
        
        //	hide selects in document if ie, but still shwo the ones within the popup
	    if ($.browser.msie) {
		    toggleSelects(document, true);
		    toggleSelects(oPopUp, false);
	    }
        
	    //  END - show
	    oPopUp.style.visibility = "visible";
	}
	
	
	function closePopUp(obj) {
	
        if (document.getElementById("Reviews"))
            {
                $("#Reviews").click();
            }
        else
            { 
                $("#Description").click();
            }
	
	    //  store popup
	    if (obj.id == "popupReportError") {
	        var oPopUp = obj;
	    } else {
	        var oPopUp = obj.oPopUp;
	    }
	
	    if (oPopUp != null) {
		    oPopUp.style.visibility = "hidden"; 
			
		    //  clear iframe content
		    if (oPopUp.isUrl == "true") {
                oPopUp.oPopUpContentInner.src = "/_blank.htm";
                //  oPopUp.oPopUpContentInner.src = "javascript:false; document.write('');";
            }
			
		    //	hide selects in document if ie, but still shwo the ones within the popup
		    if ($.browser.msie) {
			    toggleSelects(document, false);
				
			    //	hide selects for all popups
			    for (var i = 0; i < aPopUpCollection.length; i++) {
				    toggleSelects(aPopUpCollection[i], true);
			    }
		    }
        }
	}

	

/*	--[ attach events ]-------------------------------------------------------	*/	

	objectAttachEvent(window, "load", initPopUp);
	objectAttachEvent(window, "scroll", scrollPopUps);
	objectAttachEvent(window, "resize", resetPopUpPosition);
	/*objectAttachEvent(document, "keypress", closePopUp);*/
