WDPRO_LOADER.require("wdprodom");
WDPRO_LOADER.require("dom");
WDPRO_LOADER.require("menu");
WDPRO_LOADER.require("animation");
WDPRO_LOADER.require("connection");

WDPRO_LOADER.addCallback( function() {


/*
	try{
		oDebugDiv	= document.createElement("div");
		oDebugDiv.className = "debug";
		oDebugDiv.id = "DEBUGDIV";
		document.body.appendChild(oDebugDiv);
	} catch (e) { alert(e) }
	
*/	

	// style the footer lines
	if (document.getElementById("footer")) {
		var hr = document.getElementById("footer").getElementsByTagName("hr");
		for (var i = 0; i < hr.length; i++)	{
			hr[i].style.borderTop = "1px solid #8f6b37";
		}
	}

	//turn everything back on
	var arrElements = YAHOO.util.Dom.getElementsByClassName("abdGlobalNavSubMenu"); 
	for (i=0; i < arrElements.length; i++)
	{
		var oDiv = arrElements[i];
		oDiv.style.display = "block";
	}
	
	var oDiv	= document.getElementById("topBarDiv");
	if (oDiv)
	{
		oDiv.style.overflow = "visible";
	}
	
	
	//Initialize menu
	$W	= YAHOO.widget;
	
	var oMenu	= new $W.MenuBar("globalNavMenu",	{                                            
			autosubmenudisplay: true, 
		     hidedelay: 750, showdelay: 0,zIndex: 99, shadow: false});
	oMenu.render();		
	oMenu.show();	
		
	oMenu.subscribe("beforeShow", onMenuBeforeShow, null); 
	//oMenu.subscribe("beforeHide", onMenuBeforeHide, null); 
		

	
	//is this destination page?
	if (document.getElementById("destinationSubNav"))
	{		
		initializeSubNav();
	}
	

	// initialize javascript events to submit forms	
	var orderButton = document.getElementById("OrderBrochureButtonMedia");
	if (orderButton) {
		orderButton.onclick = function(){
			//if (document.getElementById("brochureForm")) {
			//	document.getElementById("brochureForm").submit();
			//}
			/*
			else if (document.getElementById("contactForm")) {
				document.getElementById("contactForm").submit();
			}
			*/
		}
	}	
});

function onMenuBeforeShow(p_sType, p_aArgs, params) 
{ 
	if(!this.body.parentNode.style.filter)	
		YAHOO.util.Dom.setStyle(this.body.parentNode,"opacity",.25);
	
	
	
	fadeIn(this.body.parentNode);
}

function setAlpha(obj,alpha)
{
	obj.style.opacity = alpha;
	if (obj.style.filter)
	{
		obj.style.filter = "alpha(opacity="+(alpha*100)+")"; // for IE
	}
}

function onMenuBeforeHide(p_sType, p_aArgs, params) 
{

}

function fadeIn(obj)
{
	var myAnim = new YAHOO.util.Anim(obj, { 
	    opacity: { from: 0.25, to: 1 }  
	    }, 0.5, YAHOO.util.Easing.easeOut); 
	myAnim.animate();
}

function fadeOut(obj)
{
	var myAnim = new YAHOO.util.Anim(obj, { 
	    opacity: { from: 1, to: 0 }  
	    }, 0.5, YAHOO.util.Easing.easeOut); 
	myAnim.animate();
}

function initializeSubNav()
{
	var subNav	= document.getElementById("destinationSubNav");	
	var links		= YAHOO.util.Dom.getElementsByClassName("subNavLink", "a", subNav);
	
	var ajaxlinks = YAHOO.util.Dom.getElementsByClassName("ajaxlink","a", document.getElementById("rightColumn") );
	for (var i = 0; i < ajaxlinks.length; i++)	
	{
		var link		= ajaxlinks[i];
		var linkHref	= link.href.replace(/destination\?name=/,"content?name=");
		var args	= { href: linkHref, clickedLink: links[2], allLinks: links };
		link.onclick = function(){ return false;}
		YAHOO.util.Event.addListener(link, "click", subNavOnClick, args);	
	}
	
	for(i=0; i < links.length; i++)
	{
		var link		= links[i];
		var linkHref	= link.href.replace(/destination\?name=/,"content?name=");
		var args	= { href: linkHref, clickedLink: link, allLinks: links };
		link.onclick = function(){return false;}
		YAHOO.util.Event.addListener(link, "click", subNavOnClick, args);	
	}	
}

var timeoutID = 0;
function showLoadingMessage()
{

	var centerCol	= getTarget("centerColumn");
	centerCol.innerHTML = "<div class=\"loading\">Loading</div>"
}


function subNavOnClick(e, args)
{
	var centerCol	= getTarget("centerColumn");
	setAlpha(centerCol,0.0);
	
	//clear all class names
	var links	= args.allLinks;	
	for (i=0; i < links.length; i++)	
	{
		var link		= links[i];
		var parent	= link.parentNode;
		link.className		= "subNavLink";
		parent.className 	= "subNavLink";
	}
	
	//set current link classname
	args.clickedLink.className			= "subNavLink selected";
	args.clickedLink.parentNode.className	= "subNavLink selected";
	
	//display spinner
	
	centerCol.innerHTML	= "<div class=\"loading\"></div>";
	
	timeoutID = setTimeout(showLoadingMessage,500);
	
	
	var callback = 
	{ 
	   success: subNavLoadComplete
	  ,failure: subNavLoadFail
	  ,argument: link
	};
		
	var objTransaction = YAHOO.util.Connect.asyncRequest('GET', args.href
		, callback, null); 

	return false;			
}


function getTarget(str)
{
	var target;
	var modules = document.getElementById(str).childNodes;
	for (var i = modules.length - 1; i >= 0; i--)
	{
		if (modules[i].className == "module") {
			target = modules[i];
			i = -1;
		}
	}
	
	return target;
}


function subNavLoadComplete(o)
{	
	clearTimeout(timeoutID);
	var centerCol	= getTarget("centerColumn");
	var html		= o.responseText;
	centerCol.innerHTML	= html;
	
	//call tracking script
	idxStart		= html.indexOf("<!-- ###SCRIPT-START###") + ("<!-- ###SCRIPT-START###").length;
	idxEnd		= html.indexOf("###SCRIPT-END###  -->");
	if ((idxStart >= 0) && (idxEnd >= 0))
	{
		var script	= html.substring(idxStart,idxEnd);
		eval(script);	
	}
	
	executeScripts(html); //scripts will not be executed when put on the page. Need to parse script tags and call eval()
	
	setAlpha(centerCol,1);	
}

function subNavLoadFail(o)
{
	clearTimeout(timeoutID);
	var centerCol	= getTarget("centerColumn");
	setAlpha(centerCol,0.25);
	centerCol.innerHTML	= "Error loading content. Please try again.";
	setAlpha(centerCol,1);		
}

function executeScripts(html)
{
	var idxStart 	= 0;
	var idxEnd	= 0;
	var script	= "";
	var idx	= html.indexOf("<script");
	while (idx >= 0)
	{
		idxStart	= html.indexOf(">",idx)+1;
		if (idxStart < 0) break;
		idxEnd	= html.indexOf("</script",idxStart);
		if ((idxStart >= 0) && (idxEnd >= 0))
		{
				script	= html.substring(idxStart+1,idxEnd);	
				
				script = script.replace("<!--","");
				script = script.replace("//-->","");
				eval(script);	
		}		
		idx	= html.indexOf("<script",idxEnd);				
	}
}

//for backward-compatibility
Abd = { ContentFeed: "",
		nameFilter: new RegExp('name=[^&]*'), 
 		ecardFilter: new RegExp('ecard=[^&]*'), //added for ecard ajax call, requires ecard param
		videoFilter: new RegExp('video=[^&]*'), //added for video ajax call, requires video param
						
		getEcard: function(strLink) {
			Abd.getContent(strLink,Abd.EcardFeed,this.ecardFilter,"ecard=","ecardImageContainer","eCard");
		},
				
		getVideo: function(strLink) {
			Abd.getContent(strLink,Abd.VideoFeed,this.videoFilter,"video=","videoPlayerContainer","video");
		},
				
		getContent: function (strLink, feed, filter,regexName,targetID, error) {
			if (feed && (window.location.toString().indexOf('Printable') == strLink.indexOf('Printable')) ) {
				var strPageName = strLink.match(this.nameFilter).toString().replace('name=', '');
				var strItemName = strLink.match(filter).toString().replace(regexName, '');
		
				var AJAX = {
					handleSuccess:function(o){					
						var objVideoPlayerContainer = document.getElementById(targetID);
						if ( objVideoPlayerContainer ) { 
							objVideoPlayerContainer.innerHTML = o.responseText.replace(/writeContent\(\'/g, '</script>').replace(/\'\)\;\<\/script\>/g, '');
							executeScripts(o.responseText);
						}
					},
					handleFailure:function(o){
						alert("Sorry, we were unable to retrieve the "+ error +". Please select one from the menu below.");					
					},
					startRequest:function() {
						YAHOO.util.Connect.asyncRequest('GET',feed + '?name=' + strPageName + "&"+regexName + strItemName, callback, null);
					}
				};
				var callback = {
					success: AJAX.handleSuccess,
					failure: AJAX.handleFailure,
					scope: AJAX
				};
				AJAX.startRequest();
			}
			else {
				window.location = strLink;
			}
		}

};   

function writeContent(strContent) {
	document.write(strContent);
}

function navigateAway(url) {
	if(confirm('You are about to navigate away from this website. Continue?'))
	location.href=url;
}