window.onload = function() { AD_init(); }


function AD_init() { /* this function executes as soon as the page loads */
	createMainMenus("mainMenu");
	createTopNav();
	renderContentMenu();
	if (browser == "Internet Explorer" && version == "6") {
		fnLoadPngs();
	}
	checkAjaxLoadIfNeeded();
}
function checkAjaxLoadIfNeeded() {
	var sUrl = window.location.href;
	if (sUrl.split("#").length > 1) {
		retrievedUrl = sUrl.split("#")[1];
		loadAsyncPage (retrievedUrl);
	}
}
function loadAsyncPage (sUrl, selectedItem) {
	var div = document.getElementById('contentContainer');
	var handleSuccess = function(o){
	
		if(o.responseText !== undefined){
			div.innerHTML = o.responseText;
		}
	}
	
	var handleFailure = function(o){
	
		if(o.responseText !== undefined){
			div.innerHTML = "<ul><li>Transaction id: " + o.tId + "</li>";
			div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
			div.innerHTML += "<li>Status code message: " + o.statusText + "</li></ul>";
		}
	}
	
	var callback =
	{
	  success:handleSuccess,
	  failure:handleFailure,
	  argument: { foo:"foo", bar:"bar" }
	};
	

	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl + "-asc.htm", callback);
	
	
	//handle the selected item
	if (selectedItem) {
		selectedItem.blur();
		
		if (selectedItem.nodeName == "DT") {
			var allElements = document.getElementsByTagName("span");
			for (i=0; i<allElements.length; i++) {
				if (allElements[i].className == "openSub") {
					allElements[i].className = "closedSub";
				}
			}
		}
		var allElements = document.getElementsByTagName("a");
		for (i=0; i<allElements.length; i++) {
			if (allElements[i].className == "selectedLink") {
				allElements[i].className = "unselectedLink";
			}
			if (selectedItem.className == "unselectedLink") {
				selectedItem.className = "selectedLink";
			}
		}
	}
	window.location='#' + sUrl;
}

function onMenuBarBeforeRender(p_sType, p_sArgs) {
	var oSubmenuData =
		{
		"home": [ 
		{ text: "Post N’Go", url: "http://360.yahoo.com" },
		{ text: "•	Franking Label Printer", url: "http://promo.yahoo.com/broadband" },
		{ text: "PIM", submenu: { id: "pim", itemdata: [
				{ text: "Yahoo! Mail", url: "http://mail.yahoo.com" },
				{ text: "Yahoo! Address Book", url: "http://addressbook.yahoo.com" },
			] }
		}, 
		{ text: "Member Directory", url: "http://members.yahoo.com" }
		],
		"downloads": [
		],
		"about": [
		],
		"postal": [
		{ text: "Post N'Go", url: "postNGo.htm" },
		{ text: "Franking Label Printer", url: "http://promo.yahoo.com/broadband" },
		{ text: "Parcel Container", url: "http://promo.yahoo.com/broadband" }
		]
	};
	
	/*
	this.getItem(3).cfg.setProperty("submenu", { id: "postal", itemdata: oSubmenuData["postal"] });
	this.getItem(1).cfg.setProperty("submenu", { id: "downloads", itemdata: oSubmenuData["downloads"] });
	this.getItem(2).cfg.setProperty("submenu", { id: "review", itemdata: oSubmenuData["review"] });
	this.getItem(3).cfg.setProperty("submenu", { id: "home", itemdata: oSubmenuData["home"] });
	*/
}
function createMainMenus(containerId) {
	YAHOO.util.Event.onContentReady(containerId, function () {
		var oMenuBar = new YAHOO.widget.MenuBar(containerId, { autosubmenudisplay: true, showdelay: 250, hidedelay:  750, lazyload: true });
		//oMenuBar.beforeRenderEvent.subscribe(onMenuBarBeforeRender);
		oMenuBar.render();            
	});	
}
function renderContentMenu() {
	var target = "contentMenu";
	YAHOO.example.onMenuReady = function() {
		// Instantiate and render the menu

		var oMenu = new YAHOO.widget.Menu(
							target, 
							{
								position:"static", 
								hidedelay:750, 
								lazyload:true 
							}
						);

		oMenu.render();

	};

	// Initialize and render the menu when it is available in the DOM

	YAHOO.util.Event.onContentReady(target, YAHOO.example.onMenuReady);
}

function createTopNav() {
	var topNavDiv = document.createElement("div");
	topNavDiv.id = "topLinx";

	var topLinx = "log in*URL=login.htm"; /* sample: log in*URL=login.aspx!contact us*URL=logout.aspx */
	var topLinxVars = topLinx.split("!");
	//creating parent nodes
	var node_ul = document.createElement("ul");
	node_ul.id = "secondBarSubdiv";

	//generating the <li> with <a>
	for (i=0; i<topLinxVars.length; i++) {
		var node_li = document.createElement("li");
		var node_a = document.createElement("a");
		var checkAction = topLinxVars[i].split("*")[1];
			var url = checkAction.split("URL=")[1];
			var action = checkAction.split("ACTION=")[1];
		topLinkId = "li" + i;
		node_li.id = topLinkId;

		//node_li.onclick = function(x) { return function() { setSelected(x); }; }("li"+i);

		if (url) {
			node_a.href = url;
		}
		else if (action)
		{
			node_a.onclick = function(x) { return function() { AD_topLinkAction(x); }; }(action);
			node_a.href = "#";
		}

		node_a.innerHTML = topLinxVars[i].split("*")[0];
		node_li.appendChild(node_a);
		if (i<topLinxVars.length-1) node_li.innerHTML += "|";
		node_ul.appendChild(node_li);
	}
	
	topNavDiv.appendChild(node_ul);
	document.getElementById("secondBar").appendChild(topNavDiv);
}

function AD_topLinkAction(x){
	alert(x);
}

function loadSubs(which) {
	which.blur();
	var target = which.parentNode.getElementsByTagName("span")[0];
	if (target.className == "closedSub") {
		target.className = "openSub";
	}
	else {
		target.className = "closedSub";
	}
}




//cookies routine

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


//browser detection
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
if (browser=="Internet Explorer" && version<=6) {
	var awfulBrowser=true;
}
//end browser detection

// load pages (ajax)
function loadAsyncPage_old(sUrl, selectedItem) {
	var div = document.getElementById('contentContainer');
	var handleSuccess = function(o){
	
		if(o.responseText !== undefined){
			div.innerHTML = o.responseText;
		}
	}
	
	var handleFailure = function(o){
	
		if(o.responseText !== undefined){
			div.innerHTML = "<ul><li>Transaction id: " + o.tId + "</li>";
			div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
			div.innerHTML += "<li>Status code message: " + o.statusText + "</li></ul>";
		}
	}
	
	var callback =
	{
	  success:handleSuccess,
	  failure:handleFailure,
	  argument: { foo:"foo", bar:"bar" }
	};
	
	//var sUrl = "postNGo.htm";
	
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
	
	//handle the selected item
	if (selectedItem) {
		selectedItem.blur();
		
		if (selectedItem.nodeName == "DT") {
			var allElements = document.getElementsByTagName("span");
			for (i=0; i<allElements.length; i++) {
				if (allElements[i].className == "openSub") {
					allElements[i].className = "closedSub";
				}
			}
		}
		var allElements = document.getElementsByTagName("a");
		for (i=0; i<allElements.length; i++) {
			if (allElements[i].className == "selectedLink") {
				allElements[i].className = "unselectedLink";
			}
			if (selectedItem.className == "unselectedLink") {
				selectedItem.className = "selectedLink";
			}
		}
	}
}

/* png fix */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (var i = document.all.length - 1, obj = null; (obj = document.all[i]); i--) {
		if (itsAllGood && obj.currentStyle.backgroundImage.match(/\.png/i) != null) {
			fnFixPng(obj);
			obj.attachEvent("onpropertychange", fnPropertyChanged);
		}
	}
}

function fnPropertyChanged() {
	if (window.event.propertyName == "style.backgroundImage") {
		var el = window.event.srcElement;
		if (!el.currentStyle.backgroundImage.match(/x\.gif/i)) {
			var bg	= el.currentStyle.backgroundImage;
			var src = bg.substring(5,bg.length-2);
			el.filters.item(0).src = src;
			el.style.backgroundImage = "url(x.gif)";
		}
	}
}

function fnFixPng(obj) {
	var bg	= obj.currentStyle.backgroundImage;
	var src = bg.substring(5,bg.length-2);
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
	obj.style.backgroundImage = "url(x.gif)";
}