WDPRO = window.WDPRO || {};
WDPRO.IBCDL = WDPRO.ASDW || {};

WDPRO_LOADER.require("dom");
WDPRO_LOADER.require("event");
WDPRO_LOADER.require("selector");


WDPRO_LOADER.addCallback( function() {
    YAHOO.util.Event.onDOMReady(function() { 
        _initializePopup();
    });
	YAHOO.util.Event.onDOMReady( popupMarkDownLinks );
	YAHOO.util.Event.onDOMReady( popuptoolBoxCalculator );	
});




function _initializePopup()
{
    var AS			= WDPRO.ASDW,
    D				= YAHOO.util.Dom,
    E				= YAHOO.util.Event,
    Q				= YAHOO.util.Selector.query;

    var popups = D.getElementsByClassName("popup","a");
    for (var i=0; i<popups.length; i++) {
        var popupName = popups[i].id;
        E.on(popups[i], 'click', function(evt) {
            E.preventDefault(evt);
            var el  = this;
            var link = el.href;
            var confirm = "false";
            var disclaimerOptions = "width=504,height=345,status=0,scrollbars=no,toolbar=no,menubar=no,location=no,resizeable=no";
            var defaultOptions = "width=800,height=600,status=0,scrollbars=yes,toolbar=yes,menubar=yes,location=yes,resizeable=yes";
            var windowName = "newWindow";
            var popupName = el.id;
            var analyticsLinkId = "";

            //find popupAttribute
            var popupAttr   = popupAttributes[popupName]; //should have been defined by AnchorRenderer
            if (popupAttr != null) {
                if (popupAttr.popupConfirm != "") {
                    confirm = popupAttr.popupConfirm;
                }
                if (popupAttr.popupAttribute != "" ) {
                    options = popupAttr.popupAttribute;
                } else {
                    options = defaultOptions;
                }

                if (popupAttr.analyticsLinkId != "") {
                    analyticsLinkId = popupAttr.analyticsLinkId;
                }

                if (popupAttr.windowName != "" ) {
                    windowName = popupAttr.windowName;
                }
            } else {
                //check the class name for override
                if (D.hasClass(el,"popupConfirm")) {
                    confirm = "true";
                }
                options = defaultOptions;
            }
            if (confirm.toLowerCase() == "true") {
                link = "/tools/popup?link="+link+"&options="+escape(options);
                options = disclaimerOptions;
            }

            if (analyticsLinkId != null) {
                //Site catalyst got confused if this is a popup link and
                //will send a wrong c9 var
                s_wdpro.prop9 = analyticsLinkId;
            }


            window.open(link, windowName,options);
        });
    }

    if (D.get("buttonImage") != null) {
        E.on(D.get("buttonImage"), 'click', function(evt) {
            E.preventDefault(evt);
            var link = D.get("link").innerHTML;
            var options = getQuerystring('options');
            if (options == null) {
                options = 'width=800, height=800,resizable=1,scrollbars=1';
            }else {
                options = unescape(options);
            }
            window.open(link, 'newnewWindow', options );
            void(0);
            self.close();
        });
    }

}

var popupAttributes = [];

function addPopupAttributes(id,options) {
    popupAttributes[id] = options;
}

function getQuerystring(key) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == key) {
            return pair[1];
        }
    }
    return null;
}




/* 
	This function performs a popup for a link generated by markdown.
	Note: this simply generates popups for EVERY link in the contentColumn for the page that uses the body id(s) specified below.
	Could not find a way to generate an id or class by markdown.  If can find way, can target specific urls.
	Also, if the body id changes, this will break.
*/

/* to do: make window options more configurable for one-offs */

function popupMarkDownLinks() {	
	var $D = WDPRO.util.Dom;
	var arielsID = $D.get('wishes-reception-specialty-venues-ariels');

	// target body id for page(s)
	if( arielsID != null ) {
		var contentColumnDiv = $D.get('contentColumn');
		var links = contentColumnDiv.getElementsByTagName('a');

		for( var i=0; i < links.length; i++ ) {
			links[i].onclick = function() {				
				popupWindow( this.getAttribute('href'));
				return false;			
			}		
		}
	}	 
}

function popupWindow( popupUrl ) {	
	var $D = WDPRO.util.Dom
	window.open( popupUrl, 'popupWindow', 'width=800, height=600');
}



function popuptoolBoxCalculator() {	
	var $D = WDPRO.util.Dom;
	if( $D.get('wrapper') != null || $D.get('wrapper') != '') {
		var targetDiv = $D.get('wrapper');
		var links = targetDiv.getElementsByTagName('a');
	}

	for( var i=0; i < links.length; i++ ) {	
		if( links[i].getAttribute('id') == 'toolboxCalculator' ) {
			links[i].onclick = function() {				
				popupCalcWindow( this.getAttribute('href'));
				return false;			
			}
		}
	}	
}

function popupCalcWindow( popupUrl ) {	
	var $D = WDPRO.util.Dom
	window.open( popupUrl, 'popupWindow', 'width=1024, height=630');
}
