﻿//Namespace
var dlrCoregPath = {};

var bPopOffer = false;
var StepNumber = 0;
var SortedOffers = new Array();

$(document).ready(function () {
    dlrCoregPath.LoadOfferOrder();
    dlrCoregPath.moveNext();
    bPopOffer = true;
});

dlrCoregPath.LoadOfferOrder = function () {
    var i = 0;
    var order;
    var CustomSorting = new Array();
    var OffersFromMarkup = new Array();
    var CustomSortNumber;
    var CustomSortIndex;
    var SortedIndexCounter = 0;
    $(".offer").hide();

    //Go through the offers rendered in the markup add them to an array. The default sorting should be based on the number at the end of ID attribute of the DIV
    $("div .offer").each(function () {
        var OfferIndex;
        var OfferIDAttribute = new String;
        OfferIDAttribute = this.id;
        OfferIndex = OfferIDAttribute.replace("dlrOffer_", "");
        OfferIndex = OfferIndex - 1; //Make the Offer Index zero base.
        OffersFromMarkup[OfferIndex] = OfferIDAttribute;
    });

    //get a custom sorting from the query string
    order = getParameterByName('order');

    //If we found an order parameter
    if (order.length != undefined) {
        CustomSorting = order.split("");
        //loop through the custom sorting
        for (i = 0; i < CustomSorting.length; i++) {
            CustomSortNumber = CustomSorting[i];
            if (CustomSortNumber != undefined) {
                //Make the CustomSortIndex zero base because our arrays are zero base
                CustomSortIndex = CustomSortNumber - 1;
                if (OffersFromMarkup[CustomSortIndex] != undefined) {
                    SortedOffers[SortedIndexCounter] = OffersFromMarkup[CustomSortIndex];
                    SortedIndexCounter++;
                }
            }
            else {
                SortedOffers[i] = null;
            }
        }
    }

    if (SortedOffers.length == 0) {
        //Loop through the OffersFromMarkup and removed any undefined elements
        for (i = 0; i < OffersFromMarkup.length; i++) {
            if (OffersFromMarkup[i] != undefined) {
                SortedOffers[SortedIndexCounter] = OffersFromMarkup[i];
                SortedIndexCounter++;
            }
        }
    }

    //     //START Debug
    //     var tmp = "";
    //     for (i = 0; i < SortedOffers.length; i++) {
    //         tmp = tmp + SortedOffers[i] + "\n\r";
    //     }
    //     alert(tmp);
    //     //END Debug

};

//set delay before calling movenext function
dlrCoregPath.OfferSetDelay = function () {
    setTimeout('dlrCoregPath.moveNext()', 3000);
};

dlrCoregPath.moveNext = function () {
    if (SortedOffers.length > StepNumber){
        StepNumber++;
        $(".offer").hide();
        $("#" + SortedOffers[StepNumber - 1]).show();
        ShowImpression();

        dlrCoregPath.ShowHidePrevNexButtons();
    }

};

 dlrCoregPath.movePrev = function () {
     if (StepNumber > 1) {
         StepNumber--;

         $(".offer").hide();
         $("#" + SortedOffers[StepNumber - 1]).show();

         dlrCoregPath.ShowHidePrevNexButtons();   
     }
 };

 dlrCoregPath.ShowHidePrevNexButtons = function () {
     if (SortedOffers.length == StepNumber) {
         $("#moveNext").hide();
     }
     else {
         $("#moveNext").show();
     }

     if (StepNumber <= 1) {
         $("#movePrev").hide();
     }
     else {
         $("#movePrev").show();
     }
 }

function popWindow(URL) 
{
    if (URL != "")
    {
        var height = screen.availHeight;
        var width = screen.availWidth;
        window.open(URL, 'myNewWindow' + Math.floor(Math.random()*50000), 'width=' + width + ',height=' + height + ', scrollbars=1, left=0, right=0, top=0'); 
        window.focus()
    }
     
}

function getParameterByName( name ) 
{ 
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
  var regexS = "[\\?&]"+name+"=([^&#]*)"; 
  var regex = new RegExp( regexS ); 
  var results = regex.exec( window.location.href ); 
  if( results == null ) 
    return 1; 
  else 
    return results[1]; 
}

function PopOffer(sOffer)
{
    if (!bPopOffer) return; //exit
    bPopOffer = false;
    var ctl = $("body");
    var oldColor = ctl.css('background-color');
    ctl.html('<center><iframe src="' + sOffer + '" scrolling="no" frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0" id="iframe1" height="1150" width="974" style="margin:0 auto"></iframe></center>');
    ctl.css('background', oldColor);
    alert("Don't Leave! We have more options for you!\n\nJust click \'CANCEL\' on the next window!");
    return "\nPress 'CANCEL' to view options!\n";
    
}

//Changes the src attribute of certain img tags to force the browser to download an impression pixel.
function ShowImpression()
{
    var $impressionImg;
    var impressionSRC;
    //Within the content div look for the image tag with a src attribute that starts with the word 'RemoveMeToDownloadImpression'
    $impressionImg = $("#" + SortedOffers[StepNumber - 1] + " img[src^='RemoveMeToDownloadImpression']");
    if ($impressionImg.length > 0)
    {
        impressionSRC = $impressionImg.attr("src");
        $impressionImg.attr("src",impressionSRC.replace("RemoveMeToDownloadImpression:", ""));
        $impressionImg.show;
    }
}
