﻿//////////////////////////////////////////////////////////////////////////////////////////
// REQUIRES: cartMain.js
//           cookieHelper.js
//           bullsEyeSearch.js (if BE popup is used)
//           http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js
//           http://yui.yahooapis.com/2.8.0r4/build/container/container-min.js
//////////////////////////////////////////////////////////////////////////////////////////

var _sCurAdvId = "";
var _sCurAdvName = "";
var _sAdvURL = "";

var $yahoo = jQuery.noConflict();

YAHOO.namespace("abf.com.container");

YAHOO.util.Event.onDOMReady(function () {

    // Define various event handlers for Dialog
    var handleSubmit = function () {
        this.submit();

        addToCartFromPopup(_sCurAdvId, _sCurAdvName);

        _sCurAdvID = "";
        _sCurAdvName = "";
    };

    var handleCancel = function () {
        this.cancel();
        _sCurAdvID = "";
        _sCurAdvName = "";
    };

    /*
    var handleCancelSearch = function () {
    this.cancel();
    };

    var handleSearch = function () {
    this.cancel();
    doBullsEyeSearch(true);
    };
    */

    // !!! This declaration must come BEFORE any popups that subscribe to it. !!!
    var onDialogShow = function (e, args, o) {

        var frame = document.createElement('iframe');
        frame.src = _sAdvURL;
        frame.width = "635";

        //alert("Testing: your screen resolution is:" + screen.width + " x " + screen.height);

        if (screen.height < 960)
            frame.height = "425";

        else if (screen.height < 1024)
            frame.height = "600";

        else
            frame.height = "650";

        o.setBody(frame);
        o.center();
    };

    // Remove progressively enhanced content class, just before creating the module
    YAHOO.util.Dom.removeClass("dlgAdvPopup", "yui-pe-content");
    //YAHOO.util.Dom.removeClass("dlgBEPopup", "yui-pe-content");
    YAHOO.util.Dom.removeClass("pnlInstr", "yui-pe-content");

    // Instantiate the Dialogs
    YAHOO.abf.com.container.dlgAdvPopup = new YAHOO.widget.Dialog("dlgAdvPopup",
							{
							    width: "660px",
							    visible: false,
							    draggable: false,
							    close: true,
							    fixedcenter: true,
							    centered: true,
							    modal: true,
							    postmethod: 'none',
							    hideaftersubmit: true,
							    underlay: 'shadow',
							    constraintoviewport: true
							});
    YAHOO.abf.com.container.dlgAdvPopup.render();
    YAHOO.abf.com.container.dlgAdvPopup.showEvent.subscribe(onDialogShow, YAHOO.abf.com.container.dlgAdvPopup);
    YAHOO.util.Event.addListener("btnAddToWL", "click", handleSubmit, YAHOO.abf.com.container.dlgAdvPopup, true);
    YAHOO.util.Event.addListener("btnNoAdd", "click", handleCancel, YAHOO.abf.com.container.dlgAdvPopup, true);
    YAHOO.util.Event.addListener("btnCancel", "click", handleCancel, YAHOO.abf.com.container.dlgAdvPopup, true);

    YAHOO.abf.com.container.pnlInstr = new YAHOO.widget.Panel("pnlInstr", {
        visible: false,
        draggable: false,
        close: true,
        fixedcenter: true,
        centered: true,
        modal: true,
        underlay: "shadow",
        width: "644px",
        constraintoviewport: true
    });

    YAHOO.abf.com.container.pnlInstr.render("pnlInstrContainer");
    YAHOO.abf.com.container.pnlInstr.center();

    /*
    // !!! BULLSEYE POPUP IS NOT CURRENTLY SUPPORTED !!!
    YAHOO.abf.com.container.dlgBEPopup = new YAHOO.widget.Dialog("dlgBEPopup",
    {
    width: "660px",
    visible: false,
    draggable: false,
    close: true,
    fixedcenter: true,
    centered: true,
    modal: true,
    hideaftersubmit: true,
    underlay: 'shadow',
    postmethod: 'none',
    constraintoviewport: true
    });

    YAHOO.abf.com.container.dlgBEPopup.render();
    YAHOO.abf.com.container.dlgBEPopup.center();
    YAHOO.util.Event.addListener("btnSearchPU", "click", handleSearch, YAHOO.abf.com.container.dlgBEPopup, true);
    YAHOO.util.Event.addListener("btnCancelSearchPU", "click", handleCancelSearch, YAHOO.abf.com.container.dlgBEPopup, true);
    */

});

function showAdvertiserPopup(sAdvId, sAdvName) {
    
    if (sAdvId.length == 0) return false;
    
    // If there are 1+ items in the cart, show the popup; otherwise redirect to normal details page as planned
    if (getCartItemCount() > 0) {

        _sCurAdvId = sAdvId;
        _sCurAdvName = sAdvName;

        var cstrAddToWLBtnId = "btnAddToWL";
        var cstrNoAddBtnId = "btnNoAdd";

        // Default the image visibilities
        if ($yahoo("#" + cstrAddToWLBtnId).length == 1 && $yahoo("#" + cstrNoAddBtnId).length == 1) {
            $yahoo("#" + cstrAddToWLBtnId).show();
            $yahoo("#" + cstrNoAddBtnId).hide();
        }

        // If this item has already been added, hide the "add" button.
        if ($yahoo("img.selectionBox_" + _sCurAdvId).length > 0 && $yahoo("#" + cstrAddToWLBtnId).length == 1 && $yahoo("#" + cstrNoAddBtnId).length == 1) {
            if (!selectorImageIsActive(_sCurAdvId)) {
                $yahoo("#" + cstrAddToWLBtnId).hide();
                $yahoo("#" + cstrNoAddBtnId).show();
            }
        }

        _sAdvURL = "/AdvertiserPopup.aspx?id=" + sAdvId;

        YAHOO.abf.com.container.dlgAdvPopup.show();

        return false;

    } else {

        // User has clicked on a banner with no items in the cart yet.
        // Let the browser redirect straight to the advertiser page without a popup.
        _sAdvURL = "";
        return true;
    }
}
function showBullsEyePopup() {
    YAHOO.abf.com.container.dlgBEPopup.show();
    return false;
}

function addSponsorToCart(strAdvertiserId, strAdvertiserName) {
    addToCartFromPopup(strAdvertiserId, strAdvertiserName);
    return false;
}
function addToCartFromPopup(strAdvertiserId, strAdvertiserName) {

    var strImgIdFound = $yahoo("img.selectionBox_" + strAdvertiserId).attr('id');
    if (strImgIdFound != undefined && strImgIdFound.length > 0) {
        addToCartUI(strAdvertiserId, strAdvertiserName);
    }
}
function cbFunctionOnCartAdd(strAdvertiserId, strAdvertiserName) {
    // This function gets called after an item is added to the cart.
    // It is called from CartAddBase() within cartMain.js
    if (getCartItemCount() == 1) {

        // if needs instruc popup then
        var coInstrCookie = getCookieById('needInstr');
        if (null == coInstrCookie || 0 == coInstrCookie.length) {
            var dtExdate = new Date();
            dtExdate.setDate(dtExdate.getDate() + 90);
            setCookie("needInstr", "false", dtExdate.toGMTString());
            showInstructions(strAdvertiserName, strAdvertiserId);
        }
    }
}
function cbFunctionOnCartRemove(strAdvertiserId) { }

function hideInstrPanel() {
    YAHOO.abf.com.container.pnlInstr.hide();
    return false;
}

function showInstructions(sCompName, strAdvertiserId) {

    sURL = "/Advertiser.aspx?id=" + strAdvertiserId;

    var sYRLText = ("\"<span style='font-weight: bold; color: #41BB42;'>Your <span style='font-family: Georgia, Sans-Serif; font-style: italic;'>Request</span> List</span>\"");

    var sHdr = "<table cellpadding='0' cellspacing='0' width='644'>";
    sHdr += "<tr style='height: 38px;'>";
    sHdr += "  <td><img src='/images/welcome-new-visitor-hdr.png' alt='Welcome New Visitor' height='38' width='644' /></td>";
    sHdr += "</tr>";
    sHdr += "</table>";

    var sBody = "<table style='background-color: #fff;' cellpadding='0' cellspacing='0' width='644'>";
    sBody += "<tr>";
    sBody += " <td valign='bottom' style='width: 186px;'><img src='/images/welcome-bg-sm.png' alt='' height='345' width='186' /></td>";

    sBody += " <td valign='top' align='center' style='background-color: #fff;'>";

    sBody += "<table style='background-color: #fff;' cellpadding='0' cellspacing='0' width='100%'>";
    sBody += "<tr valign='top'>";
    sBody += "<td align='left' style='padding-top: 20px; padding-right: 20px;;'>";

    sBody += "<p><span style='font-weight: bold; font-size: 12pt;'>" + sCompName + " has been added to <br />" + sYRLText + "</span></p>"

    sBody += "<table width='100%' cellpadding='5' cellspacing='0'>"
    sBody += "<tr valign='top'>"
    sBody += "<td><img src='/images/icon-star.jpg' border='0' height='16' width='16' /></td>"
    sBody += "<td>You may add up to " + _cintMaxCartItems + " additional franchises to <br />" + sYRLText + ".</td>"
    sBody += "</tr>"
    sBody += "<tr style='height: 10px;'><td colspan='2'/></tr>"

    sBody += "<tr valign='top'>"
    sBody += "<td><img src='/images/icon-star.jpg' border='0' height='16' width='16' /></td>"
    sBody += "<td>" + sYRLText + " will follow you around the site as you browse.</td>"
    sBody += "</tr>"
    sBody += "<tr style='height: 10px;'><td colspan='2'/></tr>"

    sBody += "<tr valign='top'>"
    sBody += "<td><img src='/images/icon-star.jpg' border='0' height='16' width='16' /></td>"
    //sBody += "<td>When you are ready to complete your request and stop searching, click the 'Request Information' button to 'SUBMIT' your information.</td>"
    sBody += "<td>When you have completed " + sYRLText + " and are ready to Request Information, simply click the 'Request Information button' and complete the form.</td>"
    sBody += "</tr>"

    //Per Bill: 02/18/2010
    sBody += "<tr style='height: 10px;'><td colspan='2'/></tr>"
    sBody += "<tr>"
    //sBody += "<td colspan='2'>To request information ONLY from <b>" + sCompName + "</b> right now, click <a href='" + sURL + "'>here</a>.</td>"
    sBody += "<td colspan='2'>To assist in making a smart choice, you may want to consider requesting information from more than one opportunity.  Compare franchise fees, royalty fees, and start-up costs to make an informed decision.</td>"
    sBody += "</tr>"
    sBody += "<tr style='height: 10px;'><td colspan='2'/></tr>"

    sBody += "<tr>"
    sBody += "<td align='center' colspan='2'><a href='Continue Browsing' onclick='return hideInstrPanel();'><img src='/images/continue_browsing.gif' border='0' height='36' width='182' /></a></td>"
    sBody += "</tr>"
    sBody += "<tr style='height: 10px;'><td colspan='2'/></tr>"

    sBody += "</table>"
    sBody += "</td>";
    sBody += "</tr>";
    sBody += "</table>";
    sBody += "   <img src='/images/abf-logo_sm.gif' alt='' height='63' width='398' />";
    sBody += " </td>";
    sBody += "</tr>";
    sBody += "</table>";

    YAHOO.abf.com.container.pnlInstr.setHeader(sHdr);
    YAHOO.abf.com.container.pnlInstr.setBody(sBody);
    //YAHOO.abf.com.container.pnlInstr.setFooter("");
    YAHOO.abf.com.container.pnlInstr.center();
    YAHOO.abf.com.container.pnlInstr.show();

    return false;
}

