
/**
 * Vypise zpravu do console firebugu
 * @param {Object} message
 */
function debug(message) {
	if(console != null) {
		console.log(message);
	}
}

/**
 * Timer pro hodnoceni
 */
var feedbackTimer = false;

/**
 * Zobrazi hodnoceni
 */
function displayFeedback() {
	var span = $A($("hodnoceni").getElementsByTagName('span')).first();
  	span.style.background="url(images/icons/ico_hodnoceni_a.gif) top left no-repeat";
	$("feedback").className="visible";
	if(feedbackTimer != false)
		clearTimeout(feedbackTimer);
}

/**
 * Schova hodnoceni
 */
function hideFeedback() {
	feedbackTimer = setTimeout("hideFeedback2()", 2000)
}

function hideFeedback2() {
	$("feedback").className="noDisplay";
	var span = $A($("hodnoceni").getElementsByTagName('span')).first();
  	span.style.background="url(images/icons/ico_hodnoceni_p.gif) top left no-repeat";
}

function showInputVal(obj, txt) {
	if(obj.value=='') {
		obj.value=txt;
	}
}

function hideInputVal(obj, txt) {
	if(obj.value==txt) {
		obj.value='';
	}
}

function open_popup(dokument,sirka,vyska,centrovat,pozice_l,pozice_h) {
  if (centrovat == 1) {
	  so = screen.width;
		vo = screen.height;
		zleva = ((so - sirka) / 2) - 13;
//		shora = (vo - vyska) / 2;
		shora = 100;
	} else {
	  zleva = pozice_l;
		shora = pozice_h;
	}
  popup_win = window.open(dokument,"popup_okno","width="+sirka+",height="+vyska+",left="+zleva+",top="+shora+",location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0");
  popup_win.focus();
}

/**
 * Otevre v puvodnim okne stranku a zavre popup
 * @param {Object} url
 */
function pl(url){
	window.opener.location.href=url;
	window.close();
	window.opener.focus();
}

function nove_okno(adresa) {
	window.open(adresa,"","");
}

function pl(url){
	window.opener.location.href=url;
//	window.close();
//  popup_win.focus();
}


function open_popup2(dokument,sirka,vyska,centrovat,pozice_l,pozice_h) {
  if (centrovat == 1) {
	  so = screen.width;
		vo = screen.height;
		zleva = ((so - sirka) / 2) - 13;
//		shora = (vo - vyska) / 2;
		shora = 100;
	} else {
	  zleva = pozice_l;
		shora = pozice_h;
	}
  popup_win = window.open(dokument,"popup_okno","width="+sirka+",height="+vyska+",left="+zleva+",top="+shora+",location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0");
  popup_win.focus();
}

function nove_okno(adresa) {
	window.open(adresa,"","");
}

function pl(url){
	window.opener.location.href=url;
	window.close();
//  popup_win.focus();
}


/**
 * Textarey zvetsujici se podle obsahu
 *
 * @requires Prototype, Effects
 * @version 0.1
 * @author jakub.roztocil@symbio.cz
 */
var resizeableTextareas = {

	minHeight: 150,

	init: function(selector) {
		$$(selector || 'textarea').each(function(ta){
			resizeableTextareas.initTextarea(ta);
		});
	},

	initTextarea: function(ta) {
		ta = $(ta);
		ta.minHeight = ta.offsetHeight;
		ta.style.overflow = 'hidden';
		resizeableTextareas.setSize(ta);
		$(ta).observe('keyup', resizeableTextareas.callback);
		$(ta).observe('change', resizeableTextareas.callback);
	},

	callback: function(e) {
		resizeableTextareas.setSize($(Event.element(e)));
	},

	setSize: function(ta) {
		var height;
		if (ta.scrollHeight > ta.offsetHeight - 2) {
			height = ta.scrollHeight;
		} else if (ta.scrollHeight < ta.offsetHeight) {
			height = ta.scrollHeight < ta.minHeight
							? ta.minHeight
							: ta.scrollHeight;
		}
		if (height) {
			new Effect.Morph(ta, {style:'height: ' + (height) +  'px', duration: .3});
		}
	}

}


/**
 * Doporuceni stranky
 */
var rec = {

	wrap: null, // obalujici div
	form: null, // formular
	toggleLink: null, // togglovaci link
	origText: null, // puvodni text togglovaciho linku

	loader: null,

	isOn: false,

	messageDiv: document.createElement('div'),
	formContentDiv: null,

	init: function() {
		rec.wrap = $('doporucit-wrap');
		if (rec.wrap) {
			rec.form = $('doporucit');
			rec.wrap2 = $('doporucit-wrap2');
			rec.loader = $('doporucujeme-loader');
			rec.formContentDiv = rec.form.down('div');
	
			rec.height = rec.form.offsetHeight;
			rec.wrap2.hide();
			rec.wrap.addClassName('wrap-off');
			rec.toggleLink = $('doporucit_link');
			rec.origText = $('doporucit_link_text').firstChild.data;
			rec.toggleLink.onclick = function() {
				this.blur();
				rec.toggle();
				return false;
			}
		}
	},

	toggle: function() {
		var to, text, options = {
			duration: .3,
			afterFinish: rec.afterToggle
		};

		rec.preToggle();
		if (rec.isOn) {
			new Effect.SlideUp(rec.wrap2, options)
		} else {
			new Effect.SlideDown(rec.wrap2, options);
		}

		rec.isOn = !rec.isOn;
	},

	preToggle: function() {
		if (rec.isOn) {
			rec.wrap.removeClassName('wrap-on');
			rec.wrap.addClassName('wrap-move-to-off');
		} else {
			rec.wrap.removeClassName('wrap-off');
			rec.wrap.addClassName('wrap-move-to-on');
		}
	},

	afterToggle: function() {
		var text;
		if (rec.isOn) {
			text = '我们的联系方式';
			rec.wrap.addClassName('wrap-on');
			rec.wrap.removeClassName('wrap-move-to-on');
			rec.scroll();
		} else {
			text = rec.origText;
			rec.setMessage(null);
			rec.wrap.addClassName('wrap-off');
			rec.wrap.removeClassName('wrap-move-to-off');
		}



		$('doporucit_link_text').firstChild.data = text;
	},

	scroll: function() {

		var scrolled = document.viewport.getScrollOffsets().top;
		var viewportHeight = document.viewport.getHeight();
		var pageHeight;
		if (document.body.scrollHeight > document.body.offsetHeight) {
			pageHeight = document.body.scrollHeight;
		} else {
			pageHeight = document.body.offsetHeight;
		}
		new Effect.Tween(null, scrolled, 150 /* vyresi problem s urcenim vysky body  */ + pageHeight - viewportHeight, {duration: .3},
			function (p) {window.scrollTo(0, p.round());}
		);
	},


	

	locked: function(setLocked) {
		if (arguments.length > 0) {
			rec.isLocked = arguments[0];
		}
		return rec.isLocked;
	},

	setMessage: function(message) {
		if (message === null) {
			if (!rec.formContentDiv.parentNode) {
				rec.messageDiv.parentNode.replaceChild(rec.formContentDiv, rec.messageDiv);
			}
		} else {
			rec.messageDiv.innerHTML = message;
			if (!rec.messageDiv.parentNode) {
				rec.formContentDiv.parentNode.replaceChild(rec.messageDiv, rec.formContentDiv);
			}
		}
	}

}

if (typeof Effect != "undefined") {
	Effect.Height = function(element, options) {
		return new Effect.Tween(null, options.from, options.to, options,
			function (p) {
				element.style.height = p.round() + 'px';
			}
		);
	}
}




/**
 * hlavni funkce, vola se ve scriptu pred koncem <body>
 */
function MAIN() {
	rec.init();
	resizeableTextareas.init();
}

