﻿ var step = 1;
 var dlrCoregPath = {};
 var HideImpulse = 0; //this var helps to show/hide impulse offer
 var bPopOffer = false;
 var Sequence = "";
 var SequenceArray = new Array(); 
 var sequenceIndex = 0;
 var bUsePreloadedSequence = false;
 var stepValue = 0;
 var PreviousStepValue = 0;
 var LastStepValue = 0;
 
 $(document).ready(function () {
     HideImpulse = getParameterByName("isDupe");
     
     dlrCoregPath.LoadSequenceArray();
     
     step = 0;
     
     dlrCoregPath.moveNext();
     //dlrCoregPath.loadOffer();
     bPopOffer = true;

 });

 dlrCoregPath.moveNext = function () {
     $("#dlrOffer_" + stepValue).hide();

     step = step + 1;
     stepValue = dlrCoregPath.GetStepValue(step);
     
     if (HideImpulse == 1 && $("#dlrOffer_" + stepValue).is(".impulseOffer")) {
         step = step + 1;
         stepValue = dlrCoregPath.GetStepValue(step);
     }

     dlrCoregPath.loadOffer();
     var offerCount = step + 1
     if ($("#dlrOffer_" + offerCount).length == 0) {
         $("#moveNext").hide();
     }
 };

 dlrCoregPath.movePrev = function () {
     $("#dlrOffer_" + stepValue).hide();

     step = step - 1;
     stepValue = dlrCoregPath.GetStepValue(step);

     if (HideImpulse == 1 && $("#dlrOffer_" + stepValue).is(".impulseOffer")) {
         if (step > 1) {
             step = step - 1;
             stepValue = dlrCoregPath.GetStepValue(step);
         }
     }

     dlrCoregPath.loadOffer();
 };

 dlrCoregPath.GetStepValue = function (stepIndex) {
     var stepVal = 0;

     if (bUsePreloadedSequence == true) {

         stepIndex = stepIndex - 1;
         stepVal = SequenceArray[stepIndex];

         if (stepIndex > 0) {
             PreviousStepValue = SequenceArray[stepIndex - 1];
             LastStepValue = SequenceArray[SequenceArray.length - 1];
         }


     } else {
         stepVal = step;
     }

     return stepVal;
 };

dlrCoregPath.LoadSequenceArray = function () {
    var i = 0;

    //get querystring sequence
    Sequence = dlrCoregPath.GetAdOfferSequence('order');

    if (Sequence.length != undefined) {
        bUsePreloadedSequence = true;
        SequenceArray = Sequence.split("");
    }
};

//this retrieves the 'sequence' querystring parameter from the URL
dlrCoregPath.GetAdOfferSequence = function (name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}

dlrCoregPath.loadOffer = function () {
    //hide prev & next
    $("#movePrev").show();
    $("#moveNext").show();

    //alert('step: ' + step);
    if (step == 1) {
        $("#movePrev").hide();
    }

    if (bUsePreloadedSequence == false) {

        if (HideImpulse == 1 && $("#dlrOffer_" + (stepValue - 1)).is(".impulseOffer")) {
            $("#movePrev").hide();
        }
    } else {

        if ((step == 2) && (PreviousStepValue == 1) && (HideImpulse == 1)) {
            $("#movePrev").hide();
        }

        if ((step == 5) && (LastStepValue == 1) && (HideImpulse == 1)) {
            $("#moveNext").hide();
        }

    }

    if ($("#dlrOffer_" + stepValue).length > 0) {
        $("#dlrOffer_" + stepValue).show();
    }
    else {
        step = step - 1
        $("#dlrOffer_" + stepValue).show();
    }
    ShowImpression();
};

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 = $("#dlrOffer_" + stepValue + " img[src^='RemoveMeToDownloadImpression']");
    if ($impressionImg.length > 0)
    {
        impressionSRC = $impressionImg.attr("src");
        $impressionImg.attr("src",impressionSRC.replace("RemoveMeToDownloadImpression:", ""));
        $impressionImg.show;
    }
}