/* 

	Author: Greg Gawlik - Web Designers
	        http://www.webdesigners.pl/

	Popups scripts based on geniuine Pornel Idea
	Idea: Kornel Lesinski
	      http://pornel.net/pups/

*/


var popupAddWidth = 0;
var popupAddHeight = 129;

var popupsMaker = 
{
	makePopups: function() {

		var db = document.body;

		if (!db || !db.getElementsByTagName) { return false; }

		var as = db.getElementsByTagName('a');
		for(var i=0;i<as.length;i++) {
			
			if((as[i].title).match(/\(([0-9]+)x([0-9]+)x([0-1]+)\)/))
			  {
				as[i].onclick = this.click;
			  }
		}
		
		return true;
	},

	click: function()
	{

		try {
			if (this.pp_win && this.pp_win.close && !this.pp_win.closed) {this.pp_win.close(); this.pp_win=false;}
		}
		
	  	catch(e){}
		
		try {
			var title = this.getAttribute('title');

			var titleandsize = popupsMaker.title2size(title);
			var gpwidth = titleandsize[1] + popupAddWidth;
			var gpheight = titleandsize[2] + popupAddHeight;
			var gpscroll = titleandsize[3];

			if(gpscroll == 0)	var scrollbars = "no";
			else				var scrollbars = "yes";
			
			var winopts = "dependent=yes,toolbar=no,resizable=yes,scrollbars=" + scrollbars + ",width=" + gpwidth + ",height=" + gpheight;
			window.open(this.href,'_blank',winopts);
			return false;
		}
	  	catch(e){}

	  	return true;

	},

	title2size: function(str)
	{
		if (str)
		{
			var out = str.match(/\(([0-9]+)x([0-9]+)x([0-1]+)\)/);
			if (out) return new Array(str, parseInt(out[1]), parseInt(out[2]), parseInt(out[3]));
		}
		
		return new Array(str, 800, 800, 1);
	},

	initNow: function() {

		this.makePopups();
	}
};

var newWindowMaker = 
{
	makeNewWindow: function() {

		var db = document.body;

		if (!db || !db.getElementsByTagName) { return false; }

		var as = db.getElementsByTagName('a');
		for(var i=0;i<as.length;i++) {
			
			if((as[i].title).match('This link will open in new window'))
			  {
				as[i].onclick = this.click;
			  }
		}
		
		return true;
	},
	
	click: function()
	{
		try {
			if (this.pp_win && this.pp_win.close && !this.pp_win.closed) {this.pp_win.close(); this.pp_win=false;}
		}
		
	  	catch(e){}
		
		try {
			var winopts = "dependent=yes,toolbar=no,resizable=yes,scrollbars=yes";
			window.open(this.href,'_blank', "");
			return false;
		}
	  	catch(e){}

	  	return true;
	},
	
	initNow: function() {

		this.makeNewWindow();
		return true;
	}
};

var dropDown = 
{
	startList: function() {
		
		var navRoot = document.getElementById("menu");
		if(isObject(navRoot))
		  {
			for (var i=0; i<navRoot.childNodes.length; i++) {
				
				var node = navRoot.childNodes[i];
				if(node.nodeName=="LI")
				  {
				  	/* Mouse events */
					node.onmouseover = this.setOver;
					node.onmouseout = this.setOut;

					/* Keyboard events */
					var child_ulnode = node.getElementsByTagName("ul");
					if(isObject(child_ulnode) && child_ulnode.length > 0)
					  {
						node.childNodes[0].onfocus = this.setOverKeyboard;
						node.childNodes[0].onblur = this.setOutKeyboard;
						
						var child_anodes = child_ulnode[0].getElementsByTagName("a");
						if(isObject(child_anodes))
						  {
							for(var an=0; an<child_anodes.length; an++) {
								
								child_anodes[an].onfocus = this.setOverKeyboardInside;
								child_anodes[an].onblur = this.setOutKeyboardInside;
							}
						  }
					  }
						
					/* Creating arrow and shadow elements */
					var shadow_div = document.createElement("div");
					var arrow_div = document.createElement("div");
					
					/* Adding classes for elements */
					shadow_div.className = 'menu_shadow';
					arrow_div.className  = 'menu_arrow';
						
					/* Searching for list - child */
					var nodeChilds = node.childNodes;
					for(var a=0; a < nodeChilds.length; a++) {
							
						if(nodeChilds[a].nodeName == "UL") 
						  {
						  	/* Reference to ul node list */
							var ulnode = nodeChilds[a];
							
							/* Temporary unhiding menu for getting true height */
							ulnode.style.display = 'block';
							var ul_height = ulnode.offsetHeight;
							ulnode.style.display = '';
							
							/* Setting height for shadow box */
							shadow_div.style.height = ul_height + "px";
							
							/* Inserting created elements */
							ulnode.parentNode.insertBefore(shadow_div, ulnode.nextSibling);
							ulnode.parentNode.insertBefore(arrow_div, ulnode.nextSibling);
						  }
					}
				  }
			}
		  }
	
	  return true;
	 
	},
	
	setOver: function () {
		
		this.className="over";
		return true;
	},
	
	setOut: function() {
		
		this.className=this.className.replace("over", "");
		return true;
	},
	
	setOverKeyboard: function() {
		
		this.parentNode.className="over";
		return true;
	},
	
	setOutKeyboard: function() {
		
		this.parentNode.className=this.parentNode.className.replace("over", "");
		return true;
	},
	
	setOverKeyboardInside: function() {
		
		this.parentNode.parentNode.parentNode.className = "over";
		return true;
	},
	
	setOutKeyboardInside: function() {
		
		this.parentNode.parentNode.parentNode.className = this.parentNode.parentNode.parentNode.className.replace("over", "");
		return true;
	},
	
	initNow: function() {

		if(document.getElementById)
		   this.startList();
		   
		return true;
	}
};

var googleMapLink =
{
	makeLink: function() {
		
		var d = document;
		
		if(d.getElementById) {
			
			var link = d.getElementById("location_map_link");
			var link_menu = d.getElementById("location_map_link_menu");
			
			if(isObject(link)) {
				link.href = "/popup/d/location_map.html";
				link.title = "(700x536x1)";
			}
			if(isObject(link_menu)) {
				link_menu.href = "/popup/d/location_map.html";
				link_menu.title = "(700x536x1)";
			}
		}
		return true;
	},
	initNow: function() {
		
		this.makeLink();
	}
	
};

function isNull(a) {
    return typeof a == 'object' && !a;
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isFunction(a) {
    return typeof a == 'function';
}
function getCookieVal (offset) {

	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {

	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {

		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;

	}
	return null;
}
function SetCookie (name, value, expires, path, domain, secure) {

    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}
function DeleteCookie (name) {

	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function initNow() {
	
	googleMapLink.initNow();
	dropDown.initNow();
	popupsMaker.initNow();
	newWindowMaker.initNow();
}
