function showHide(objId) {
	if (document.getElementById(objId)) {
		var obj = document.getElementById(objId);
		if (cls.has(obj, 'show')) {
			cls.remove(obj, 'show');
			cls.add(obj, 'hide');
		} else {
			cls.remove(obj, 'hide');
			cls.add(obj, 'show');
		}
	}
}

function showElm(objId) {	
	if (document.getElementById(objId)) {
		var obj = document.getElementById(objId);
		if (cls.has(obj, 'hide')) {
			cls.remove(obj, 'hide');
			cls.add(obj, 'show');
		}
	}
}

function hideElm(objId) {	
	if (document.getElementById(objId)) {
		var obj = document.getElementById(objId);
		if (cls.has(obj, 'show')) {
			cls.remove(obj, 'show');
			cls.add(obj, 'hide');
		}
	}
}

function getParentByClass(objParentClass, objTarget) {
	var parent = false;
	do {
		if (cls.has(objTarget, objParentClass)) {
			parent = objTarget;
			break;
		}
		objTarget = objTarget.parentNode;
	} while (objTarget != document);
	return parent;
}

function highLightRow(e, how) {
	var e   = evt.fix(e);
	var td  = e.target;
	var obj = getParentByClass('highLightRow', td)
	if (obj) {
		if (how == 'on') {
			cls.add(obj, 'highlighted');
		} else {
			cls.remove(obj, 'highlighted');
		}
	}
}

function initHighLightRows() {
	var tr = document.getElementsBySelector('TR.highLightRow');
	for (var i = 0; i < tr.length; i++) {
		evt.add(tr[i], 'mouseover', function(e) {highLightRow(e, 'on');});
		evt.add(tr[i], 'mouseout', function(e) {highLightRow(e, 'off');});
	}
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g, "");
	if (isNaN(num)) {
		num = "0";
	}
	sign = num == (num = Math.abs(num));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10) {
		cents = "0" + cents;
	}
	if (cents == "00") {
		cents = "-";
	}
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
		num = num.substring(0, num.length - (4 * i + 3)) + " " + num.substring(num.length - (4 * i + 3));
	}
	return (sign ? "" : "-") + num + "," + cents;
}

function oznacitVse(checkbox, className) {
	var ch = document.getElementsBySelector('input.' + className);
	for (var i = 0; i < ch.length; i++) {
		ch[i].checked = checkbox.checked ? 'true' : null;
	}
}

fixOffsetLeft = function(obj) {var curleft = 0;if (obj.offsetParent) {while (obj.offsetParent) {curleft += obj.offsetLeft; obj = obj.offsetParent;}} else if (obj.x)	curleft += obj.x; return curleft;} 
fixOffsetTop  = function(obj) {var curtop  = 0;if (obj.offsetParent) {while (obj.offsetParent) {curtop  += obj.offsetTop; obj = obj.offsetParent;}} else if (obj.y) curtop += obj.y; return curtop;}

checkForm.addFieldType(
	"min4",
	new RegExp(".{4,}")
);

function initHide() {
	var objs = document.getElementsBySelector('.initHide');
	if (objs) {
		for (var i = 0; i < objs.length; i++) {
			cls.add(objs[i], 'hide');
		}
	}
}

// pravidelne reloadovanie okien s IM
function initImPopup() {
	// kazdych 30 sekund zavolame reloadovaciu funkciu
	setTimeout(reloadImPopup, 30000);
}

function reloadImPopup() {
	var area = document.getElementById("text");
	// ak ma uzivatel rozpisanu nejaku spravu, nerobime nic
	if (!area || !area.value) {window.location.reload();}


}

// otvaranie popupov (tyka sa hlavne IM)
function popup(url, w, h, winName) {
	w = (w) ? w : 400;
	h = (h) ? h : 500;
	var newWindow = window.open(url, winName, "width=" + w + ",height=" + h + ",status=yes,resizable=yes,scrollbars=yes");
	newWindow.focus();
	return newWindow;
}

emPopUpText = {
	popUp : function(e) {
		e = evt.fix(e);
		var anchor     = e.target;
		while (anchor.tagName != 'A') {
			anchor = anchor.parentNode;
		}
		var width      = (typeof(emPopUpWidth)  == 'undefined') ? 500  : emPopUpWidth;
		var height     = (typeof(emPopUpHeight) == 'undefined') ? 300  : emPopUpHeight;
		//var width      = 500;
		//var height     = 300;
		var left       = (screen.width  - width)  / 2;
		var top        = (screen.height - height) / 2;
		var scrollbars = 'yes';
		imgWin         = window.open(anchor.href,'', 'scrollbars='+scrollbars+', width='+width+', height='+height+', top='+top+', status=no, left='+left);
		//imgWin.loadTl  = anchor.title;
		e.preventDefault();
		return false;
	},

	init : function() {
		var anchors = document.getElementsByTagName('A');
		for (i=0; i<anchors.length; i++) {
			if (cls.has(anchors[i], 'emPopUpText')) {
				evt.add(anchors[i], 'click', emPopUpText.popUp);
			}
		}
	}
}

searchText = {
	del : function() {
		searchField = document.getElementById('searchField');
		if (searchField) {
			searchField.value = '';
		}
		evt.remove(searchField, 'focus', searchText.del);
	},

	init : function() {
		searchField = document.getElementById('searchField');
		if (searchField && searchField.value == '') {
			searchField.value = 'Vyhľadávanie...';
			evt.add(searchField, 'focus', searchText.del);
		}
	}
}

stats = {
	init : function() {
		var anchors = document.getElementsByTagName('A');
		if (anchors) {
			for(var i = 0; i < anchors.length; i++) {
				if (cls.has(anchors[i], 'saveStats')) {
					evt.add(anchors[i], 'click', stats.save);
				}
			}
		}
		if (typeof(cookieStatsValue) != 'undefined') {
			stats.saveCookie('stats-source', cookieStatsValue, 1);
		}
	},

	saveCookie : function (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=/";
	}
}

// globalni init
if (document.all && !window.opera){
	initHide();
	searchText.init();
	initHighLightRows();
	emPopUpText.init();
	stats.init();
} else if (document.addEventListener && !window.opera) {
	document.addEventListener("DOMContentLoaded", initHide, null);
	document.addEventListener("DOMContentLoaded", searchText.init, null);
	document.addEventListener("DOMContentLoaded", initHighLightRows, null);
	document.addEventListener("DOMContentLoaded", emPopUpText.init, null);
	document.addEventListener("DOMContentLoaded", stats.init, null);
} else {
	evt.add(window, "load",   initHide);
	evt.add(window, 'load',   searchText.init);
	evt.add(window, "load",   initHighLightRows);
	evt.add(window, "load",   emPopUpText.init);	
	evt.add(window, 'load', stats.init);
}

function AddFavorite(linkObj,addUrl,addTitle)
{
  if (document.all && !window.opera)
  {
	window.external.AddFavorite(addUrl,addTitle);
	return false;
  }
  else if (window.opera && window.print)
  {
	linkObj.title = addTitle;
	return true;
  }
  else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
  {
	if (window.confirm('Pridať obľúbenú stránku ako nový panel?'))
	{
	  window.sidebar.addPanel(addTitle,addUrl,'');
	  return false;
	}
  }
  return false;
}

function saveCookie(name,value,days) {
	if (days) {
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires="; expires="+date.toGMTString()
	} else 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 deleteCookie(name) {
	saveCookie(name,"",-1)
}

function firstVisit() {
	var nextVisit = readCookie("visit");
	var obj = document.getElementById('deliveryXmasBox'); 

	if (!nextVisit) {
		if (cls.has(obj, 'hide')) {
			cls.remove(obj, 'hide');
			cls.add(obj, 'show');
		}
		setTimeout("hideElm('deliveryXmasBox')",  10000);
		saveCookie("visit", 1, 1);
	} else {
		cls.remove(obj, 'show');
		cls.add(obj, 'hide');
	}
}

evt.add(window,"load", firstVisit);

/* --- MAIN MENU --- */

$(document).ready(function(){
	if (typeof document.body.style.maxHeight == "undefined")
	{
		$("#main-menu li, .minimized div.box").hoverClass("hover");
	}
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c); },
			function() { $(this).removeClass(c); }
		);
	});
};


$(document).ready(function() {
	function addMega(){
		$(this).addClass("hovering");
	}

	function removeMega(){
		$(this).removeClass("hovering");
	}

	var megaConfig = {
		interval: 200,
		sensitivity: 7,
		over: addMega,
		timeout: 500,
		out: removeMega
	};
	$("#main-menu li.mega").hoverIntent(megaConfig);
	$("#company-menu li.mega").hoverIntent(megaConfig);
});

