﻿/// <reference path="../Edentity.Global.js" />
/// <reference path="../External/jquery-1.4.1-vsdoc.js" />

Edentity.RegisterNamespace("SDM.Modules.PromoPopup");

(function(Popup, $) {

    var defaults = {
        ContainerID: '',
		RedirectUrl: '',
        SwfContainerID: '',
		SwfHeight: 500,
        SwfUrl: '',
		SwfWidth: 500,
        VersionExpressInstall: '6.0.65',
        VersionFlash: '9.0.0'
    };

    var $preview = null;
    var $jDialog = null;
    var opts = null;

    Popup.Init = function(p) {
	
        opts = $.extend({}, defaults, p || {});

		// Check cookie to see whether to display popup
		if (!hasVisitedCookie()) {
			displayDialog();
			configStyles();
			loadFlash();
		}		
    }
	
	Popup.Hide = function() {		
	
		$preview.dialog("close");
	}
	
	Popup.SetVisitedCookie = function() {
		
		var cookieValue = unescape(Shoppers.Cookie.GetPromoCookie());
		
		if (cookieValue == null || cookieValue == '') {
			Shoppers.Cookie.SetPromoCookie(location.href);
		} else {
			Shoppers.Cookie.SetPromoCookie(cookieValue + '; ' + location.href);
		}
	}

    function configStyles() {
        $preview.removeClass('ui-dialog-content ui-widget-content');
		//$preview.dialog("option", "position", "top");
    }
	
	function hasVisitedCookie() {
		
		var cookieValue = unescape(Shoppers.Cookie.GetPromoCookie());
		if (cookieValue == null || cookieValue == '') return false;
		
		var urls = cookieValue.split('; ');
		
		// Iterate through the list of URLs in the cookie and only compare the URLs
		// after their querystrings and hash values are stripped out.
		
		for (var i = 0; i < urls.length; i++) {
		
			var visitedURL = getBareURL(urls[i]);
			var currURL = location.protocol + "//" + location.hostname + location.pathname;
			
			if (visitedURL.toLowerCase() == currURL.toLowerCase()) return true;
		}
		
		return false;
	}
	
	function getBareURL(url) {
		
		var queryPos = url.indexOf('?');
		if (queryPos != -1) url = url.substr(0, queryPos);
		var hashPos = url.indexOf('#');
		if (hashPos != -1) url = url.substr(0, hashPos);
		
		return url;
	}
	
	function displayDialog() {
	
        $preview = $('#' + opts.ContainerID).show();

        // Initialize dialog
        $jDialog = $preview.dialog({
            autoOpen: true,
            closeOnEscape: true,
            dialogClass: 'BasePromoPopup',
            modal: true,
            width: opts.SwfWidth
        });
	}
	
	function loadFlash() {
	
		if (swfobject.hasFlashPlayerVersion(opts.VersionExpressInstall) && opts.SwfUrl.match(/swf$/)) {

			var attributes = {
				id: opts.SwfContainerID
			};

			var params = {
				wmode: "transparent",
				allowfullscreen: "false",
				quality: "high"
			};

			var flashvars = {
				hidePromoPopupFn: "SDM.Modules.PromoPopup.Hide",
				redirectUrl: opts.RedirectUrl,
				setVisitedCookieFn: "SDM.Modules.PromoPopup.SetVisitedCookie"
			};

			swfobject.embedSWF(opts.SwfUrl, opts.SwfContainerID, opts.SwfWidth, opts.SwfHeight, "9.0.0", null, flashvars, params, null);
			
		}
		else {
			
			$('#' + opts.SwfContainerID + ' div').show();
			Popup.SetVisitedCookie();
			
		}
	}

} (SDM.Modules.PromoPopup, jQuery));
