var PopTimer = 0;
var PopFreq = 1;
var PopWidth = 1050;
var PopHeight = 800;
var PopMethod = 1;
var PopFocus = 0;
var Page_Popped = false;
var Page_Loaded = false;
var Page_Enter;





function InitPop()
{
    // Init timestamp when the site loads
    Page_Enter=new Date();
    
    switch(PopMethod)
    {
        case 1: // Click Pop
        	jQuery(document).bind('click', LoadStandardPop);
            break;
        case 2: // Timed Pop
        
            if (PopTimer == 0)
                LoadStandardPop(); // Load popup instantly
            else
                setTimeout("LoadStandardPop()", PopTimer*1000);
            break;
    }
}


function createCookie(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function RetrieveCount()
{
    var cookieName = CookieName;
    var popSuccesses = readCookie(cookieName);
    
    if (popSuccesses != null)
        popSuccesses = parseInt(popSuccesses);
    else
        popSuccesses = 0;

    return popSuccesses;
}

function IncrementCount()
{
    var cookieName = CookieName;
    var popSuccesses = readCookie(cookieName);
    
    if (popSuccesses != null)
        createCookie(cookieName, parseInt(popSuccesses) + 1, 1);
    else
        createCookie(cookieName, 1, 1);
}


function LoadStandardPop()
{
    // Don't allow the pop to run if it was already done so
    if (Page_Popped == true)
        return;
        
    var pLoaded = false;

    if(window.SymRealWinOpen){open=SymRealWinOpen;}
    if(window.NS_ActualOpen){open=NS_ActualOpen;}
          
    var pxLeft = 0;
    var pxTop = 0;
           
    if (screen.width > 0 && screen.height > 0)
    {
        pxLeft = (screen.width / 2) - (PopWidth / 2);
        pxTop = (screen.height / 2) - (PopHeight / 2) - 50;

        if (pxLeft < 0) pxLeft = 0;
        if (pxTop < 0) pxTop = 0;
    }

    //pageTracker._trackEvent('PopUnder', PopURL);
    pLoaded=open(PopURL,'3413443','toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,top=' + pxTop + ',left=' + pxLeft + ',width=' + PopWidth + ',height=' + PopHeight);

    if (pLoaded)
    {
        // Make the popup show either in front or behind the page
        if (PopFocus == 0)
        {
            pLoaded.blur();
            window.focus();
        }
        
        // We don't want to pop again on the same pop load.
        Page_Popped = true;
    
        // Increment the successfull pop count cookie
        IncrementCount();
    }
    else
    {
        LoadActivePop();
    }


}

function LoadActivePop()
{
    // Don't allow the pop to run if it was already done so
    if (Page_Popped == true)
        return;

    var pLoaded = false;

    if(window.SymRealWinOpen){open=SymRealWinOpen;}
    if(window.NS_ActualOpen){open=NS_ActualOpen;}
    
    var pxLeft = 0;
    var pxTop = 0;
           
    if (screen.width > 0 && screen.height > 0)
    {
        pxLeft = (screen.width / 2) - (PopWidth / 2);
        pxTop = (screen.height / 2) - (PopHeight / 2) - 50;

        if (pxLeft < 0) pxLeft = 0;
        if (pxTop < 0) pxTop = 0;
    }

    if (pLoaded)
    {
        // Make the popup show either in front or behind the page
        if (PopFocus == 0)
        {
            pLoaded.blur();
            document.xx1.DOM.Script.setTimeout("self.focus();");              
        }
        
        // We don't want to pop again on the same pop load.
        Page_Popped = true;        
        
        // Increment the successfull pop count cookie
        IncrementCount();
    }
    
}

if((RetrieveCount() < PopFreq)) InitPop();
