// JScript File
var popupStatus = 0;
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 124;
//Flash exists details - 0,1,2 to find  notexists, oldversion, exists
var flashExists;

//apnum reurned from flash
var returnedAPnum;
//apnum coming from Product Page
var passedAPnum;

var dletrackingpath;
var dleProductImageId;
var varDLECountryCode;
var varDFLEDomainURL;
var DLEWebserviceLocation;

function DetectFlashVersion(){
// Version check for the Flash Player that has the ability to start Player
var hasProductInstall = DetectFlashVer(6, 0, 65);

// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
    if ( hasProductInstall && !hasRequestedVersion ) {
         flashExists =1;
    }  else if (hasRequestedVersion) {
        flashExists = 2;
     } else {  
        flashExists =0;
     } 
     
}



function dleSelectedAPNUM(actionName)
{

    returnedAPnum=actionName;
}


function notifyTealeaf(actionName)
{
        
        $.get("/tealeaf/notify.asp", { elementID: "dle", theAction: actionName } );
}

function DLEErrors(ErrorMessage,ErrorObject)
{
        $.post(dletrackingpath, { msg: ErrorMessage, errObject: ErrorObject } );
}

function APNUMChangedFlag(ChangedAPNUM,ChangeFlag)
{
        $.post(dletrackingpath, { elementID: "dle" ,CurrentDLEAPNUM: ChangedAPNUM, IsChanged: ChangeFlag, DLEItemImageId: dleProductImageId } );
}

function DLERoomVisits(CurrentAPNUM,RoomImageName)
{
        $.post(dletrackingpath, { elementID: "dle" ,CurrentRoomViewAPNUM: CurrentAPNUM, RoomViewed: RoomImageName, DLEItemImageId: dleProductImageId } );
}

function DLEOpenFlag()
{
        $.post(dletrackingpath, { elementID: "dle" ,DLEModuleOpen: "True", DLEItemImageId: dleProductImageId  } );
}

function DLEFlashNotExists()
{
        $.post(dletrackingpath, { elementID: "dle" ,FlashNotExists: "True" } );
}

//loading popup with jQuery magic!
function loadPopup(dleUrl,SessionId,ImageDir){
	//loads popup only if it is disabled
	
	var DLEswfPath;
	DLEswfPath = varDFLEDomainURL + "/DFLEPopUp.swf";
	
	
	
	var DLERoomDetailsXMLPath;
	DLERoomDetailsXMLPath = varDFLEDomainURL + "/DLERoomViews.xml";
	
    
    DLEWebserviceLocation = DLEWebserviceLocation + "?wsdl";
	
	if(popupStatus==0){
		$("#backgroundPopup").css({
		    "background-color": "#000000",
		    "filter":"alpha (opacity=70)",
			"filter":"progid:DXImageTransform.Microsoft.Alpha(style=0, opacity=70)",
			"-moz-opacity": "0.7",
			"opacity": "0.7",
			"-khtml-opacity":".7",
            "zoom": "1"
		}); 

		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		
		popupStatus = 1;		
		
		
	
		var sObjectStr = '';
		if (flashExists == 2){		
			$("#popupContact").css({
				"position": "absolute",
				"background":"#FFFFFF",
				"height":546,
				"width":597
			}); 
	
	    $("#myswf").css({
	    "position": "absolute",	    
	    "background": "#FFFFFF",	    
		"height":546
	    });  
	  
	    sObjectStr = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
	                                'id="DFLEPopUp" ' +		                        
			                        'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> ' +
			                        '<param name="movie" value="'+DLEswfPath+'" /> ' +
			                        '<param name="quality" value="high" /> ' +
			                         '<param name="FlashVars" value="SessionId=' + SessionId + '&ImageData= ' + dleUrl + '&DLEWebserviceLocation='+DLEWebserviceLocation+ '&DLERoomDetailsXMLPath='+DLERoomDetailsXMLPath+'&DLECountryCode='+varDLECountryCode+'" /> ' +
			                        '<param name="bgcolor" value="#2e2c2d" /> ' +
			                        '<param name="allowScriptAccess" value="always" /> ' +
			                    '<embed src="'+DLEswfPath+'" ' +
			                       'FlashVars="SessionId=' + SessionId + '&ImageData= ' + dleUrl +'&DLEWebserviceLocation='+DLEWebserviceLocation+'&DLERoomDetailsXMLPath='+DLERoomDetailsXMLPath+'&DLECountryCode='+varDLECountryCode+'"' +			                      
                                    ' quality="high" bgcolor="#2e2c2d" ' +
				                    ' name="DFLEPopUp" align="middle" ' +
	                                'id="DFLEPopUp1" ' +		                        
				                    'play="true" ' +
				                    'loop="false" ' +
				                    'quality="high" ' +
				                    'allowScriptAccess="always" ' +
				                    'type="application/x-shockwave-flash" ' +
				                    'pluginspage="http://www.adobe.com/go/getflashplayer"> ' +
			                    '</embed> ' +
		                        '</object>';            
		    addContent('myswf', sObjectStr);
		}
		
		else {
		$("#popupContact").css({
			"position": "absolute",
			"background":"#2E2D2D",
			"height":203,
			"width": 597      
	    }); 
	    $("#myswf").css({
			"position": "absolute",
			"background":"#2E2D2D",
			"height": 160,

			"border-bottom": "2px solid #FFFFFF",

			"width": 597 
	    });  

			if (document.all) {
			    $("#myswf").css({
			    "width": 601 
			    });
			}
			
		    sObjectStr = '<div style="position:absolute; left:50px; top:49px;"> <img src="' + ImageDir + '/Images/dfle/1/graphics/flash_icn.gif" id="bigImage" width="60" height="60"/> </div> ' +
		                 '<div style="position:absolute; left:127px; top:43px; width:416px;"> ' +
		                 '<FONT style="FONT-SIZE:14pt" color="#ffffff" face="verdana">Adobe Flash Player 9 or higher is required</FONT> ' +
		                 '<FONT style="FONT-SIZE:14pt" color="#ffffff" face="verdana">to view the Room View ShowCase.<br></FONT> ' +
		                 '<a href="http://get.adobe.com/flashplayer/" style="font-family:"verdana regular";font-size:11pt; color:#ffffff; filter:Glow(color=#ff0000,strength=12);">Download Flash Player (free)</a> ' +
		                 '</div>';
		    addContent('myswf', sObjectStr);
		   
		}
	}
}

function addContent(divName, content) {

	document.getElementById(divName).innerHTML = content;
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;

		if (returnedAPnum != passedAPnum)
		{
			$('.sizeOptionBlock').each(function()
			{

            			var x = $(this).find('.apnum').text();

            			if (returnedAPnum == x)
                        		$(this).trigger('click');
			});
		}

	}
}



//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.body.clientWidth;
	var windowHeight = document.body.clientHeight;
	
	var popupHeight = $("#popupContact").height() + 40;
	var popupWidth = $("#popupContact").width();  
	   if(!window.screen){
		    windowWidth = 800;
		    windowHeight = 600;
		   }
	   if(popupWidth >  windowWidth){
		    popupWidth = (windowWidth)*(0.9);
		   }
	   if(popupHeight > windowHeight){
		   	popupHeight = (windowHeight)*(0.9);
		   }	   
	//centering


    var objControl = document.getElementById("popupContact");
    if (objControl != null)
    {
        var left = (objControl.offsetParent.clientWidth / 2) - (objControl.clientWidth / 2) + objControl.offsetParent.scrollLeft;
        var top = (objControl.offsetParent.clientHeight / 2) - (objControl.clientHeight / 2) + objControl.offsetParent.scrollTop;

        $("#popupContact").css({
            "position": "absolute",
            "top": top,
            "left": left
        });
    }	
		   

	var docHeight = Math.max(
        $(document).height(),
        $(window).height(),
        /* For opera: */
        document.documentElement.clientHeight
    );

	var docWidth = Math.max(
        $(document).width(),
        $(window).width(),
        /* For opera: */
        document.documentElement.clientWidth
    );
	$("#backgroundPopup").css({
		"height":docHeight,
        "width":docWidth
	});	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){	
	// Handle window resize
	//If the User resizes the window, adjust the #backgroundPopup height and width
	$(window).bind("resize", resizeWindow);
	
	function resizeWindow( e ) {	
		var docHeight = Math.max(
			$(document).height(),
			$(window).height(),
			/* For opera: */
			document.documentElement.clientHeight
		);

		var docWidth = Math.max(
			$(document).width(),
			$(window).width(),
			/* For opera: */
			document.documentElement.clientWidth
		);
		$("#backgroundPopup").css({
			"height":docHeight,
			"width":docWidth
		});
		


		if ( $('#dleIcon').length == 0)
		{
			$('.enlarge').css('width',(iw/2)+28);
		}
		else {		
			var newEnlargePosition = $('#A1').position().left;
			$('.enlarge').css('position','absolute');
			$('.enlarge').css('left',newEnlargePosition);
			$('.enlarge').css('width','100px');

		centerPopup();

		}
	}	
	
	//LOADING POPUP
	//Click the DLE LINK button event!
	$("#DLELink").click(function(){
		
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	
	
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});

function popDLE(dleUrl,SessionId,ImageDir,APNUM,dletrackingpathParam,DLEImageId,strDFLEDomainURL,strDLECountryCode,strFramingServicePath){

	passedAPnum = APNUM;

	dletrackingpath=dletrackingpathParam;

	dleProductImageId = DLEImageId;

	varDFLEDomainURL = strDFLEDomainURL;

	varDLECountryCode = strDLECountryCode;
	
	DLEWebserviceLocation = strFramingServicePath;
	
//Read Country Code to set header logo for UK/US
	
	 var objPoup = document.getElementById("popupContact");
	    var oH1 = objPoup.getElementsByTagName("H1");
	    if (strDLECountryCode == "UK") {
		oH1[0].className = "headerUK";
	    }
	    else if (strDLECountryCode == "US") {
		oH1[0].className = "headerUS";
	    }
   
   DetectFlashVersion();
    //End Read Country Code
    if (flashExists == 2)
    {
	    DLEOpenFlag();
	}
	else
	{
	    DLEFlashNotExists();
	} 

	// Create our background DIV
	var oDiv = document.createElement("DIV");
	oDiv.id = "backgroundPopup";
	oDiv.className = "backgroundPopup";
	document.body.appendChild(oDiv);

	// load/show popup
	loadPopup(dleUrl,SessionId,ImageDir);
	
	//centering with css
	centerPopup();
	
	document.getElementById("popupContact").className = "dragDLE";
    //Drag and Drop enabled
	//draggable('popupContact');

	disableSelection(document.getElementById("popupContact"));
	
}

/* Draggable Code */

var ie = document.all;
var nn6 = document.getElementById && !document.all;
var isdrag = false;
var x, y;
var dobj;
var setType = 1;

function draggable(elem) 
{

    var elem = document.getElementById(elem);
    this.elem = elem;
    var me = this;

    elem.onmousedown = function(ev)
    {
        var htype = '-moz-grabbing';
        if (ev == null) { ev = window.event; htype = 'move'; }
        var target = ev.target != null ? ev.target : ev.srcElement;
        var topelement = nn6 ? "HTML" : "BODY";
        orgCursor = target.style.cursor;

        while (target.tagName != topelement && target.className != "dragDLE") {
            target = nn6 ? target.parentNode : target.parentElement;
        }
        if (target.className == "dragDLE")
        {
            isdrag = true;
            dobj = target;
            tx = parseInt(dobj.style.left + 0, 10);
            ty = parseInt(dobj.style.top + 0, 10);

            x = nn6 ? ev.clientX : event.clientX;
            y = nn6 ? ev.clientY : event.clientY;

            document.onmousemove = me.moveHandler;
            document.onmouseup = me.cleanup;
            return false;
        }
    }
    this.cleanup = function(e)
    {
        document.onmousemove = null;
        document.onmouseup = null;
        dobj.style.cursor = orgCursor;
        isdrag = false;
    }
    var tyyy;
    this.moveHandler = function(e)
    {
        if (isdrag) {
            var txx = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
            var tyy = nn6 ? ty + e.clientY - y : ty + event.clientY - y;

            if (setType == 1)
            {
                var dobjXL = -(dobj.offsetWidth / 2);
                var dobjXR = -(dobj.offsetWidth / 2);
                var dobjY = dobj.offsetHeight / 2;

            }
            else if (setType == 2)
            {
                var dobjXL = 0;
                var dobjXR = -(dobj.offsetWidth);
                var dobjY = dobj.offsetHeight;
            }
            var objControl = dobj;
            var left = (objControl.offsetParent.clientWidth / 2) - (objControl.clientWidth / 2) + objControl.offsetParent.scrollLeft;
            var top = (objControl.offsetParent.clientHeight / 2) - (objControl.clientHeight / 2) + objControl.offsetParent.scrollTop;

            var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

            if (txx > dobjXL && txx < (document.body.offsetWidth + dobjXR) && tyy > 0 && (tyy + dobjY) < (screen.availHeight))
            {
                dobj.style.left = txx;
                dobj.style.top = tyy;
                return false;
            }
            else if (txx > dobjXL && txx < (document.body.offsetWidth + dobjXR) && tyy > 0 && (tyy + dobjY) < (screen.availHeight + scrollTop) && (screen.availHeight + scrollTop) < screen.height)
            {
                dobj.style.left = txx;
                dobj.style.top = tyy;
                return false;
            }
        }
    }
}



function disableSelection(element)
{
    element.onselectstart = function()
    {
        return false;
    };
    element.unselectable = "on";
    element.style.MozUserSelect = "none";
    element.style.cursor = "default";
}

