﻿/*
* @projectDescription 	Keppra.XR.DayOfApproval project js library
*                       project specific class (def|namespace)
* @author	Tom Newton tnewton@rosettamarketing.com
* @version	0.1
*
* REQUIRES rosetta application class (application.js)
*/
var keppraDOA = keppraDOA ? keppraDOA : new Object(); // create the default namespace
keppraDOA.application = new ROSETTA.application(); // instantiate application obj (REQUIRES application.js)
keppraDOA.loaderBase = "/includes/js/libs/yui/";
// Pre-load global external API(s) from app AJAX API, then launch onload callback
keppraDOA.application.loadLibrary("yuiloader"); //required for YUI
keppraDOA.application.setOnLoadCallback(function(){
    var loader = new YAHOO.util.YUILoader(); //instantiate yui loader obj to our default namespace
    loader.base = keppraDOA.loaderBase;
    loader.require(['animation','connection','container','element','cookie','button']);//CORE YUI Components
    loader.onSuccess = function() {
        keppraDOA.init();
    }; 
    loader.insert();  
});
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * App Init
============================================ */
keppraDOA.init = function() {
   //interstitial modal
   keppraDOA.imodal = new keppraDOA.interstitial();
   keppraDOA.imodal.init();
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*  Interstitial Dialog Object/Methods
============================================ */
keppraDOA.interstitial = function(){
    this.targetClass = "interstitial_replace"; // fake class name of the anchors that will return the modal dialog and have href replaced
    this.xmlPath = "/includes/xml/interstitial_dialog.xml";
    this.panel_id = "yuidialog_ipanel";
    this.golink_id = "imodal_go"; // id of div or container that will receive the link and link name to continue
    this.nogolink_id = "imodal_nogo"; // id of div or container that will receive the link and link name to not continue
    this.golink_text = "I understand and wish to continue to "; // link text that will precede the name of the actual site
    this.nogolink_text = "I would like to return to the previous page on KeppraXR.com"; // link text that will say no thanks mofo, but nicer
    this.close_id = "md_close"; //id of elem that close/hide form
    this.pageContent; //place holder for xml/html content
    this.targetLinks = []; //empty array of link targets
    
    this.interstitialPanel = new YAHOO.widget.Panel(this.panel_id, {visible:false, draggable:false, close:false, modal:true, zIndex:20000, underlay:"none", iframe:false, constraintoviewport:true, monitorresize:true, width:"606px", height:"414px", effect:[{effect:YAHOO.widget.ContainerEffect.SLIDE,duration:0.5}]});
    
};
keppraDOA.interstitial.prototype.init = function(){
    var anchors = document.getElementsByTagName("a");
    var obj = this;
    
    for(i=0;i<anchors.length;i++){
        if (anchors[i].className == this.targetClass){
            //load any required files, css for the taf in this case
            var loader = new YAHOO.util.YUILoader();
            loader.addModule({ 
                name: "interstitial_css",
                type: "css",
                fullpath: "includes/css/interstitial_dialog.css",
                varName: "INTERSTITIAL_CSS"
            });
            loader.require(['interstitial_css']);
            loader.onSuccess = function() {
                obj.loadContent();
            }; 
            loader.insert(); 
            break;
        }
    }
};
keppraDOA.interstitial.prototype.loadContent = function(){
    //Load XML Content
    var obj = this; //internal obj re-reference
    var successHandler = function(o) {
        var xml = o.responseXML;
        var pages = xml.getElementsByTagName("page");
        var pageName = pages[0].getAttribute("name");
        obj.pageContent = pages[0].childNodes[0].nodeValue;
        obj.renderPanel();
    }
    //define the AJAX failure handler
    var failureHandler = function(o) {
        //alert the status code and error text
        alert(o.status + " : " + o.statusText);
    }
    //define the callback object
    var callback = {
        success:successHandler,
        failure:failureHandler
    };
    //initiate the transaction
    var transaction = YAHOO.util.Connect.asyncRequest("GET", obj.xmlPath, callback, null);
};

keppraDOA.interstitial.prototype.renderPanel = function(){
    var obj = this; //internal obj re-reference
    obj.interstitialPanel.setBody(obj.pageContent); //set the yui panel content to our xml
    obj.interstitialPanel.render(document.body); //render the panel to the document body
    document.getElementById(obj.panel_id).className = ""; //erase the yui classname from the yui panel container
    // set the click listener for the close button on the panel
    YAHOO.util.Event.addListener(obj.close_id, "click", function(){
        obj.hidePanel()
    });
    // now that the panel is rendered in the body, lets loop through the links and change the href 
    // to the appropriate javascript method that will show the modal panel instead of bringing the user
    // to the damn interstitial page.
    var links = document.getElementsByTagName("a");
    for(j=0;j<links.length;j++){
        if (links[j].className == obj.targetClass){
            var newid = "imo_replaced_" + j;
            links[j].id = newid;
            links[j].onclick = obj.showPanel;
        }
    }
}
keppraDOA.interstitial.prototype.showPanel = function(){
    //grab the urlvar and name for the link and unescape them
    var urlvar = unescape(keppraDOA.application.util.getQueryString(this.href, "urlvar"));
    var urlname = unescape(keppraDOA.application.util.getQueryString(this.href, "urlname"));
    //grab the target divs on the panel and replace the links on em
    var golink_area = document.getElementById(keppraDOA.imodal.golink_id);
    var nogolink_area = document.getElementById(keppraDOA.imodal.nogolink_id);
    //clear any previous results out
    golink_area.innerHTML = "";
    nogolink_area.innerHTML = "";
    //create the new links and append to appropriate div
    var tmpLinkGo = document.createElement("a");
    tmpLinkGo.href = urlvar;
    tmpLinkGo.target = "_blank";
    tmpLinkGo.appendChild(document.createTextNode(keppraDOA.imodal.golink_text + urlname));
    golink_area.appendChild(tmpLinkGo);
    //link opens in a new window, so hide the panel if it's clicked
    YAHOO.util.Event.addListener(tmpLinkGo, "click", function(){
        keppraDOA.imodal.hidePanel()
    });
    //nogo (close) link
    var tmpLinkNoGo = document.createElement("a");
    tmpLinkNoGo.href = "javascript: keppraDOA.imodal.hidePanel();";
    tmpLinkNoGo.appendChild(document.createTextNode(keppraDOA.imodal.nogolink_text));
    nogolink_area.appendChild(tmpLinkNoGo);
    //alert(unescape(urlname));
    keppraDOA.imodal.interstitialPanel.cfg.setProperty("context",[this.id,"tl","bl"]);
    keppraDOA.imodal.interstitialPanel.show();
    return false;
    
}
keppraDOA.interstitial.prototype.hidePanel = function(){
    this.interstitialPanel.hide();
}
