/*=============================================================================
    PRELOAD IMAGES
==============================================================================*/
function preloadImages()
{
	var images = new Array();
	images[0] = new Image();
	images[0].src = "/images/menu/about_o.gif";
	images[1] = new Image();
	images[1].src = "/images/menu/contact_o.gif";
	images[2] = new Image();
	images[2].src = "/images/menu/gallery_o.gif";
	images[3] = new Image();
	images[3].src = "/images/menu/products_o.gif";
	images[4] = new Image();
	images[4].src = "/images/menu/services_o.gif";
}
preloadImages();

/*=============================================================================
    AJAX LIB
==============================================================================*/
function ajaxRequest(url, method, containerResponse)
{
	var req;
	if(window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if(window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP");
	
	// if browser doesn't support ajax return true
	if(!req)
		return false;
	
	req.open(method.toUpperCase(), url , true );
	
	// process function
 	req.onreadystatechange=function()
 	{
  		if (req.readyState == 4) 
  			if(req.status == 200)
  				containerResponse.innerHTML = req.responseText;
	}
	
	// send request
	req.send(null);
	
	return true;
}

/*=============================================================================
    ALERT DIALOG
==============================================================================*/
// errorMesssage - display message
// elementFocus - set focus to element after hide alert (optional)
function myAlert(errorMessage, elementFocus)
{
	var dialogAlertId = "dialog_alert";
	
	var alertBg = document.createElement("div");
	alertBg.setAttribute("id", dialogAlertId);
	document.getElementsByTagName("body").item(0).appendChild(alertBg);

	var dialog = document.createElement("div");
	dialog.className = "dialog";
	dialog.onclick = function(e) { e = e||event; e.cancelBubble = true; return false; }
	alertBg.appendChild(dialog);
	
	var text = document.createElement("div");
	text.className = "text";
	text.innerHTML = errorMessage;
	dialog.appendChild(text);
	
	var button = document.createElement("a");
	button.setAttribute("href","#");
	button.innerHTML = "OK";
	button.className = "button";
	button.onfocus = function() { this.className += " focus"; }
	button.title = "Close";
	button.onblur = function() { this.className += this.className.replace(" focus",""); }
	dialog.appendChild(button);
	button.onclick = function()
	{
		alertBg.parentNode.removeChild(alertBg);
		if(elementFocus)
		{
			elementFocus.focus();
		}
		return false;
	}

	var scroll = getPageScroll();
	
	alertBg.style.width = document.documentElement.scrollWidth + "px";
	alertBg.style.height = document.documentElement.scrollHeight + "px";
	
	var innerHeight = window.innerHeight ? window.innerHeight : ( screen.availHeight - 100 )
	
	dialog.style.position = "absolute";
	dialog.style.top = ( ( scroll.y + ( innerHeight/2 )  ) - ( dialog.offsetHeight / 2 ) ) + "px";
	dialog.style.left = ( (parseInt(alertBg.style.width) / 2) - ( dialog.offsetWidth / 2 ) ) + "px";
	
	// shadow
	var shadow = document.createElement("div");
	shadow.className = "shadow";
	shadow.style.position = "absolute";
	shadow.style.background = "#000";
	shadow.style.top = (dialog.offsetTop + 3) + "px";
	shadow.style.left = (dialog.offsetLeft + 3) + "px";
	shadow.style.width = dialog.offsetWidth + "px";
	shadow.style.height = dialog.offsetHeight + "px";
	shadow.style.MozOpacity = 0.15;
	shadow.style.filter = "alpha(opacity=15)";
	alertBg.appendChild(shadow);
	
	alertBg.onclick = function()
	{
		dialog.className += " alert";
		setTimeout( function()
		{
			dialog.className = dialog.className.replace(" alert", "");
			button.focus();
		}, 1000 );
	}
	
	button.focus();
}

function getPageScroll()
{
	var scroll = new Object();
	if (self.pageYOffset)
	{
		scroll.x = self.pageXOffset;
		scroll.y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		scroll.x = document.documentElement.scrollLeft;
		scroll.y = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		scroll.x = document.body.scrollLeft;
		scroll.y = document.body.scrollTop;
	}
	return scroll;
}

/*=============================================================================
    EVENT LISTENER
==============================================================================*/
function addEvent(obj, evType, fn)
{
	if(obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false); 
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}
	else 
	{
		return false;
	}
}

/*=============================================================================
    COOKIES
==============================================================================*/
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,path) 
{
	var str = name + "=" + value;
	if (path!==null)
		str += ";path="+path;
	document.cookie = str;
};

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr==-1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset,endstr));
};

function delCookie(name) {
  if (getCookie(name)) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function showHideCategories()
{
	var button = document.getElementById("product_categories");
	if(!button)
		return;
	button.showHideProperty = true;
	button.onclick = function()
	{
		this.blur();
		var list = this.parentNode.nextSibling.nextSibling ?
			this.parentNode.nextSibling.nextSibling : this.parentNode.nextSibling;
		list.style.display = this.showHideProperty ? "block" : "none";
		this.showHideProperty = !this.showHideProperty;
		return false;
	}	
}

function flashMenuButton()
{
	var button = document.getElementById("flash_menu");
	var mainMenu = document.getElementById("main_menu");
	var flashMenu = document.getElementById("main_menu_flash");
	
	if(!button || !mainMenu)
		return;
	
	button.onclick = function()
	{
		this.blur();
		if(!window.getCookie)
			return false;

		var status = getCookie("flashMenu");
		
		if(status == null)
			status = 1;

		mainMenu.className = status == 1 ? "active" : "";
		flashMenu.className = status == 0 ? "active" : "";
		
		this.className = status == 0 ? "on" : "off";
		this.title = (status == 0 ? "Wyłącz" : "Włącz" ) + " animowane menu";
		
		delCookie("flashMenu");
		status = status == 1 ? 0 : 1;
		setCookie("flashMenu",status,"/");

		return false;
	}
}

function run()
{
	flashMenuButton();
	showHideCategories();	
}

addEvent(window, 'load', run);