version = navigator.appVersion.toLowerCase();
agent = navigator.userAgent.toLowerCase();
ie = (version.indexOf('msie')>-1);
win = (version.indexOf('windows')>-1);
opera = (agent.indexOf('opera')>-1);    

function addBookmark(title,url) { 
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,""); 
	} else if( document.all ) { 
		window.external.AddFavorite( url, title); 
	} else if( window.opera && window.print ) { 
		return true; 
	} 
}

function toggle_list(w) {
/*
 * Opens and closes folders (expandable sitemap)
 */
	ul=w.nextSibling.nextSibling.nextSibling;

	if(ul)
		if (ul.nodeName.toLowerCase()=="ul")
			if(ul.className=="off"){
				ul.className="on"
				w.className="open on"
			} else {
				ul.className="off"
				w.className="open off"
			}
	return false;
}

function toggle_display(id) {
/*
 * Shows and hides elements of the given Id
 */
	if (document.getElementById(id)) {
		if (document.getElementById(id).style.display == "block") {
			document.getElementById(id).style.display = "none";
		} else {
			document.getElementById(id).style.display = "block";
		}
	return true;
	} else {
	return false;
	}
}

function write_print(text) {
	document.write('\074a a  class="print" href=\"javascript:window.print();\"\076'+text+'\074/a\076');
}

function write_addtofavorite(text, URL, webname) {
	if ((opera) && (win)) return false;
	else if ((ie) && (win)) document.write('\074a class="AddToFavorite" href='+"'javascript:window.external.addFavorite(\""+URL+"\",\""+webname+"\")'"+'\"\076'+text+'\074/a\076 \074span\076|\074/span\076');
	return false;
}

function verify_contactform() {
	if (document.getElementById("mail_form_name").value=="") {
		alert("E-mail není možné odeslat - nezadali jste vaše jméno.");
		document.getElementById("mail_form_name").focus();
		return false;
	} else if (document.getElementById("mail_form_email").value=="") {
		alert("E-mail není možné odeslat - váš e-mail není zadán.");
		document.getElementById("mail_form_email").focus();
		return false;
	} else if (document.getElementById("mail_form_content").value=="") {
		alert("E-mail není možné odeslat - tělo zprávy je prázdné.");
		document.getElementById("mail_form_content").focus();
		return false;
	} else return true;
}

function verify_commentform() {
	if (document.getElementById("add_comment_comment").value=="") {
		alert("Vkládaný komentář je prázdný a tudíž nemohl být uložen. Opravte prosím jeho obsah a znovu jej odešlete.");
		document.getElementById("add_comment_comment").focus();
		return false;
	} else return true;
}

/*
 * Opens gallery picture
 */

function open_gallery_image(address) {
	width = 820;
	leftPos = (window.screen.width/2)-(width/2);
	height = (window.screen.height-400);
	topPos  = ((window.screen.height)/2)-(height/2)-30;
	close_gallery_image_window();
	return window.open(address, "image_window", "width="+width+", height="+height+", left="+leftPos+", top="+topPos+",dependent=yes, titlebar=no, scrollbars=yes, resizable=yes, status=no");
}

function close_gallery_image_window() {
	if (window.image_window && !window.image_window.closed)	image_window.close()
}

function insert_smile(what, e) {
	var text = document.getElementById(e).value;
	document.getElementById(e).value = text + ' ' + what;
}

//return a formatted price
function formatCurrency(price, currencyFormat, currencySign, currencyBlank)
{
	var blank = '';
	if (currencyBlank > 0)
		blank = ' ';
	if (currencyFormat == 1)
		return currencySign + blank + formatNumber(price, priceDisplayPrecision, ',', '.');
	if (currencyFormat == 2)
		return (formatNumber(price, priceDisplayPrecision, ' ', ',') + blank + currencySign);
	if (currencyFormat == 3)
		return (currencySign + blank + formatNumber(price, priceDisplayPrecision, '.', ','));
	if (currencyFormat == 4)
		return (formatNumber(price, priceDisplayPrecision, ',', '.') + blank + currencySign);
	return price;
}

//return a formatted number
function formatNumber(value, numberOfDecimal, thousenSeparator, virgule)
{
	numberOfDecimal = parseInt(numberOfDecimal);
	value = Number(value).toFixed(numberOfDecimal);
	var val_string = value+'';
	var tmp = val_string.split('.');
	var abs_val_string = (tmp.length == 2) ? tmp[0] : val_string;
	var deci_string = ('0.' + (tmp.length == 2 ? tmp[1] : 0)).substr(2);
	var nb = abs_val_string.length;

	for (var i = 1 ; i < 4; i++)
		if (value >= Math.pow(10, (3 * i)))
			abs_val_string = abs_val_string.substring(0, nb - (3 * i)) + thousenSeparator + abs_val_string.substring(nb - (3 * i));

	if (parseInt(numberOfDecimal) == 0)
		return abs_val_string;
	return abs_val_string + virgule + (deci_string > 0 ? deci_string : '00');
}

function in_array(value, array)
{
	for (var i in array)
		if (array[i] == value)
			return true;
	return false;
}

$(function init() {
	// copyright Drupal, GNU General Public License
	$('textarea.resizable:not(.textarea-processed)').each(function() {
		var textarea = $(this).addClass('textarea-processed'), staticOffset = null;
		
		// When wrapping the text area, work around an IE margin bug.  See:
		// http://jaspan.com/ie-inherited-margin-bug-form-elements-and-haslayout
		$(this).wrap('<div class="resizable-textarea"><span></span></div>')
		  .parent().append($('<div class="grippie"></div>').mousedown(startDrag));
		
		var grippie = $('div.grippie', $(this).parent())[0];
		grippie.style.marginRight = (grippie.offsetWidth - $(this)[0].offsetWidth) +'px';
		
		function startDrag(e) {
			staticOffset = textarea.height() - e.pageY;
			textarea.css('opacity', 0.25);
			$(document).mousemove(performDrag).mouseup(endDrag);
			return false;
		}
		
		function performDrag(e) {
			textarea.height(Math.max(32, staticOffset + e.pageY) + 'px');
			return false;
		}
		
		function endDrag(e) {
			$(document).unbind("mousemove", performDrag).unbind("mouseup", endDrag);
			textarea.css('opacity', 1);
		}
	});
});
