// Version: 0.0.2
// Last Modified: 22. 5. 2006
// Last Modified: 30. 4. 2009 / JPt
// Author: Martin Cohen <mail@martincohen.info>
// Before modification, please contact author at mail@martincohen.info

function fixEolas() {
	var objects = document.getElementsByTagName("object");
	var out = '';
	for (var i=0; i<objects.length; i++) {
	   out = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ';
	   out += 'width="' + objects[i].getAttribute("width") + '" ';
	   out += 'height="' + objects[i].getAttribute("height") + '" ';
	   out += '/>'+"\n";
	     for (var j = 0; j < objects[i].childNodes.length; j++) {
	       if(objects[i].childNodes[j].tagName.toLowerCase() == 'param') {
	           out += '<param name="' + objects[i].childNodes[j].getAttribute("name") +
	               '" value="' + objects[i].childNodes[j].getAttribute("value") + '" />'+"\n"
	       }
	   }
	     out += '</object>';
	     objects[i].outerHTML = out;
	}
}

/* function hideKKText() {
	var nMem = $('nMembership');
	if (nMem.checked) {
		$('souhlas-kk-text').hide();
	}
}

function modifyMembership() {
	var nMem = $('nMembership');
	var yMem = $('yMembership');
	var sMem = $('sMembership');
	var hMem = $('clenstviKKCheckForm');
	var nar  = $('b2c_rokNarozeni');
	if (yMem.checked) {
		$('souhlas-kk-text').show();
		sMem.enable();
	} 
	if (nMem.checked) {
		$('souhlas-kk-text').hide();
		sMem.disable();
	}
	setFooter();
}

function hideNSText() {
	var nNews = $('nNewsletter');
	if (nNews.checked) {
		$('souhlas-ns-text').hide();
	}
}

function modifyNewsletter() {
	var nNews = $('nNewsletter');
	var yNews = $('yNewsletter');
	var sNews = $('sNewsletter');
	if (yNews.checked) {
		$('souhlas-ns-text').show();
		sNews.enable();
	}
	if (nNews.checked) {
		$('souhlas-ns-text').hide();
		sNews.disable();
	}
	setFooter();
} */

// 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;
}

function popItUp(url,w,h) {
	x = window.screen.width; 
	y = window.screen.height; 
	moveX=(x/2)-w/2; 
	moveY=(y/2)-h/2;
	window.open(url, '_blank', 'location=no,status=no,resizable=no,scrollbars=yes,width='+w+',height='+h+',left='+moveX+',top='+moveY+',screenX='+moveX+',screenY='+moveY);
	return false;
}

// zaskrtnutie vsetkych checkboxov v rade
function checkAll(state, field) {
	if (field) {
		if (field.length > 0) {
			for (var i = 0; i < field.length; i++) {
				field[i].checked = state;
			}
		} else {
			field.checked = state;
		}
		return true;
	}
	return false;
}

var AjaxIndicator = Class.create();
	
AjaxIndicator.prototype = {
	on : function() {
		this.indicator.style.visibility = 'visible';
	},

	off : function() {
		this.indicator.style.visibility = 'hidden';
	},
	
	initialize : function(container) {
		if (document.getElementsByTagName) {
			if (container) {

				this.container = container;
				new Insertion.Top(this.container, '<div id="ajax-indicator">&nbsp;</div>');
				this.indicator = $('ajax-indicator');
				this.off();

			} else {

				new Insertion.bottom(document.body, '<div id="ajax-indicator">&nbsp;</div>');
				this.indicator = $('ajax-indicator');

				this.off();
			}
		}
	}
}

// JuicyTooltip Class
// Prototype Library needed (tested on 1.5.1)
// made by Bumbo
// version 1.2.1

var JuicyTooltip = Class.create();

JuicyTooltip.prototype = {
	mouseover: function(e) {
		//Event.stop(e);

		var position = this._getPosition(e);

		this.tooltip.setStyle({top:position.y + "px", left:position.x + "px"});
		
		// remmember listeners for further disabling
		this.mousemovelistener = this.mousemove.bindAsEventListener(this);
		Event.observe(this.indicator, 'mousemove', this.mousemovelistener);

		this.tooltip.show();
	},
	
	mouseout: function(e) {
		//Event.stop(e);
		
		Event.stopObserving(this.indicator, 'mousemove', this.mousemovelistener);
		
		this.tooltip.hide();
	},
	
	mousemove: function(e) {
		Event.stop(e);

		var position = this._getPosition(e);

		this.tooltip.setStyle({top:position.y + "px", left:position.x + "px"});
	},
	
	_getViewportDimensions: function() {
		var innerHeight;
		var innerWidth;
		
		if (navigator.appVersion.indexOf('MSIE') > 0) {
			innerHeight = document.body.scrollHeight;
			innerWidth = document.body.clientWidth;
		} else {
			innerHeight = document.body.scrollHeight;
			innerWidth = window.innerWidth;
		}
		return {width: innerWidth, height: innerHeight};
	},
	
	_getPosition: function(e) {
		var dimensions = Element.getDimensions(this.tooltip);
		var viewport = this._getViewportDimensions();
		
		var position_x = Event.pointerX(e);
		var position_y = Event.pointerY(e);
		
		if ((position_x + dimensions.width + this.delta) > (viewport.width - this.delta)) {
			// switch orientation of tooltip when it is too wide
			position_x = position_x - dimensions.width - this.delta;
			if (position_x < 0) position_x = 0;
		} else {
			position_x = position_x + this.delta;
		}
		
		if ((position_y + dimensions.height + this.delta) > (viewport.height - this.delta)) {
			// try to stay in the viewport
			position_y = viewport.height - dimensions.height - this.delta;
			if (position_y < 0) position_y = 0;
		} else {
			position_y = position_y + this.delta;
		}
		return {x: position_x, y: position_y};
	},
		
	initialize: function(indicator) {
		this.indicator = indicator;
		this.indicator.addClassName('active_tooltip');
		
		// remove from content - needed for relative positioned containers
		var tooltipContent = this.indicator.next('.show');
		if (tooltipContent) {
			if (!tooltipContent.empty()) {
				this.tooltip = $('tooltips_container').appendChild(document.createElement('div'));
				this.tooltip.className = 'tooltip';
		
				new Insertion.Bottom(this.tooltip, tooltipContent.innerHTML);
				tooltipContent.remove();
				this.tooltip.hide();
			
				// mouse offset
				this.delta = 10;
			
				Event.observe(this.indicator, 'mouseover', this.mouseover.bindAsEventListener(this));
				Event.observe(this.indicator, 'mouseout', this.mouseout.bindAsEventListener(this));
			}
		}
	}
}

function initJuicyTooltips() {
	// insert new container for tooltips
	new Insertion.Bottom(document.body, '<div id="tooltips_container"></div>');	
	$$('.show_tooltip').each(function(indicator, index) {
		var obj = new JuicyTooltip(indicator);
	});
	setFooter();
}

/* using addDOMLoadEvent? */
if (typeof addDOMLoadEvent != "undefined") {
	addDOMLoadEvent(initJuicyTooltips);
} else {
	Event.observe(window, 'load', initJuicyTooltips, false);
}

var AutoFillForm = Class.create();

AutoFillForm.prototype = {

	fillDescription: function(e) {
		if ((this.input.value == '') || (this.input.value == 'undefined')) {
			this.input.value = this.label.innerHTML;
			if (e) {
				Event.stop(e);
			}
		}
	},
	
	clearField: function() {
		if (this.input.value == this.label.innerHTML)
			this.input.value = '';

	},
	
	initialize: function(autoFillForm) {
		this.label = autoFillForm.down('label');
		this.input = autoFillForm.down('input');

		this.fillDescription();
		
		Event.observe(autoFillForm, 'submit', this.clearField.bindAsEventListener(this));
		Event.observe(this.input, 'focus', this.clearField.bindAsEventListener(this));
		Event.observe(this.input, 'change', this.fillDescription.bindAsEventListener(this));
		Event.observe(this.input, 'blur', this.fillDescription.bindAsEventListener(this));
	}
	
}

function initAutoFillForms() {
	$$('.autofill-form').each(function(elm) {
		var obj = new AutoFillForm(elm);
	});
}

function setFooter() {

    var footer = $('footer-container');
    //var container = $('container');
    var maxHeight = 0;

	if (footer) {
		if ($('sidebar1')) {
			var cols = [$('content'), $('sidebar1'), $('sidebar2')];
		} else {
			var cols = [$('content'), $('sidebar2')];
		}

		for (var i = 0; i < cols.length; i++) {
			if (cols[i]) {
				maxHeight = cols[i].offsetHeight > maxHeight ? cols[i].offsetHeight : maxHeight;
			}
		}

		if (!footer.hasClassName('positioned')) footer.addClassName('positioned');
		footer.style.top = maxHeight + 'px';
		return;
	}
}

var defaultWidth = 200;
var defaultHeight = 200;

function resizeWindowFit(e) {
	Event.stop(e);
	var img = $('detail');
	var new_w  = img.width;
	var new_h  = img.height;
	if ((screen.width > new_w) || (screen.height > new_h)) {

		var left       = (screen.width  - new_w) / 2;
		var top        = (screen.height - new_h) / 2;
	} else {
		var left       = 0;
		var top        = 0;
	}
	window.moveTo(left, top);
	window.resizeBy(new_w - defaultWidth, new_h - defaultHeight);
}

var PopUp = Class.create();

PopUp.prototype = {
	click: function(e) {
		if (!this.popup.closed && this.popup.location) {
			this.popup.location.href = this.anchor.href;
		} else {
			var left       = (screen.width  - defaultWidth) / 2;
			var top        = (screen.height - defaultHeight) / 2;
			this.popup = window.open(this.anchor.href, '_blank', "scrollbars=0, menubar=0, location=0, titlebar=1, resizable=1, width="+this.width+", height="+this.height+", left="+left+", top="+top);
		}
		if (this.popup.focus) {
			this.popup.focus();
		}
		Event.stop(e);
		return !this.popup;
	},

	initialize: function(anchor) {
		this.anchor = anchor;
		this.id = anchor.id;
		this.width = defaultWidth;
		this.height = defaultHeight;
		this.popup = '';
		Event.observe(this.anchor, 'click', this.click.bindAsEventListener(this));
	}
}

function initPopUps() {
	$$('a.popup').each(function(anchor, index) {
		anchor.id = 'popup-' + index;
		var obj = new PopUp(anchor);
	});
}

Event.observe(window, 'load', setFooter, false);
Event.observe(window, 'resize', setFooter, false);
Event.observe(window, 'load', initAutoFillForms, false);
Event.observe(window, 'load', initPopUps, 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 var 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 fixEolasInit() {
//	try { fixEolas(); } catch(e) {};
//}

//Event.observe(window, 'load', fixEolasInit, false);

function initValidation() {
	$$('form.validate').each(
		function(form) {
			new Validation(form, {immediate:true});
		}
	);
}

Validation.add('IsEmpty', '', function(v) {
	return  ((v == null) || (v.length == 0)); // || /^\s+$/.test(v));
});

Validation.addAllThese([
	['required', 'Tato položka je povinná.', function(v) {
				return !Validation.get('IsEmpty').test(v);
			}],
	['validate-number', 'Zadejte číslo.', function(v) {
				return Validation.get('IsEmpty').test(v) || (!isNaN(v) && !/^\s+$/.test(v));
			}],
	['validate-phone', 'Zadejte telefonní číslo s maximální délkou 16 znaků.', function(v) {
				return Validation.get('IsEmpty').test(v) || /^[\+\ \d]{9,16}$/.test(v);
			}],
	['validate-digits', 'Zadávejte pouze číslice.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  !/[^\d]/.test(v);
			}],
	['validate-zip', 'Neplatný formát PSČ.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  /\d{3} ?\d{2}/.test(v);
			}],
	['validate-alpha', 'Zadávejte pouze písmena.', function (v) {
				return Validation.get('IsEmpty').test(v) ||  /^[a-zA-Z]+$/.test(v)
			}],
	['validate-alphanum', 'Zadávejte pouze písmena nebo číslice.', function(v) {
				return Validation.get('IsEmpty').test(v) ||  !/\W/.test(v)
			}],
	['validate-date', 'Zadejte datum.', function(v) {
				var test = new Date(v);
				return Validation.get('IsEmpty').test(v) || !isNaN(test);
			}],
	['validate-email', 'Zadejte validní e-mailovou adresu.', function (v) {
				return Validation.get('IsEmpty').test(v) || /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/.test(v)
			}],
	['validate-url', 'Zadejte validní internetovou adresu.', function (v) {
				return Validation.get('IsEmpty').test(v) || /^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i.test(v)
			}],
	['validate-selection', 'Vyberte jednu z možností.', function(v,elm){
				return elm.options ? elm.selectedIndex > 0 : !Validation.get('IsEmpty').test(v);
			}],
	['validate-one-required', 'Vyberte jednu z možností.', function (v,elm) {
				var p = elm.parentNode;
				var options = p.getElementsByTagName('INPUT');
				return $A(options).any(function(elm) {
					return $F(elm);
				});
			}],
	 ['validate-min3chars', 'Zadejte alespoň 3 znaky.', function (v) {
				return Validation.get('IsEmpty').test(v) || /.{3,}$/.test(v)
			}],
	['validate-password', 'Napište heslo pro kontrolu znovu', {
     			minLength : 6, // value must be at least 6 characters
     			equalToField : 'passwd1' // value is equal to the form element with this ID
			}],
	['validate-birthdate', 'Žádáte-li o členství, musíte vyplnit i rok narození.', function (v) {
     			return $('nMembership').checked || /\d{4,}$/.test(v)
			}]
]);

Event.observe(window, 'load', initValidation, false);

function initRadioChoosers() {
	$$('.radio-chooser').each(function(elm) {
		var obj = new RadioChooser(elm);
	});
}

Event.observe(window, 'load', initRadioChoosers, false);

function initFormKeeper() {
	var obj = new FormKeeper();
}

//Event.observe(window, 'load', initFormKeeper, false);

function initSearchRadios() {
	$$('#search-checkboxes li').each(function(li) {
		var obj = new SearchCheckbox(li);
	});
	return false;
}

Event.observe(window, 'load', initSearchRadios, false);

function initOptionsHighlighters() {
	var obj = new OptionsHighlighter($$('.options-highlighter'), $('more-details'));
}

Event.observe(window, 'load', initOptionsHighlighters, false);
/*
function checkAddToFavourites() {
	var elm = $('add-to-favorites');

	if (elm && ((window.opera && window.print) || (window.sidebar) || (window.external))) {
		if (window.opera && window.print) {
			elm.setAttribute('rel', 'sidebar');
		}
		elm.style.display = 'block';
		Event.observe(elm, 'click', AddToFavorites);
	}
}
function AddToFavorites(e) {
	Event.stop(e);

	var elm = Event.element(e);
	var title = elm.title;
	var url = elm.href;

	if (window.sidebar) {
		window.sidebar.addPanel(title, url, ""); // Mozilla Firefox Bookmark
		return false;
	} else {
		if (window.external) { 
			window.external.AddFavorite(url, title); // IE
			return false;
		} else {
			if(window.opera && window.print) {
				return true; // Opera Hotlist
			}
		}
	}
	return false;
}

Event.observe(window, 'load', checkAddToFavourites, false);
*/

var EarsSwitcher = Class.create();

EarsSwitcher.prototype = {
	change: function(event) {
		var li = Event.findElement(event, 'li');
		if (this.active != li) {
		
			Element.hide(this.active.div);
			Element.removeClassName(this.active, 'active');
			
			this.active = li;
			if (!this.active.div) {
			
				var div = document.createElement('DIV');
				$(div).addClassName('ear');
				this.ears_switcher.appendChild(div);
				this.active.div = div;
				
				var ajax = new Ajax.Updater(
					{ success: this.active.div },
					this.active.down('a').href,
					{method: 'get',
						asynchronous: true,
						onCreate: function() {
							this.indicator.on();
						}.bind(this),
						onComplete: function () {
							this.indicator.off();
						}.bind(this),
						onException: function(ajax, err) {
							this.indicator.off();
						}.bind(this),
						requestHeaders: {
							'Content-type': 'text/html; charset=windows-1250'
						}
					}
				);
			}
			
			Element.show(this.active.div);
			Element.addClassName(this.active, 'active');
			setFooter();
		}
		Event.stop(event);
	},

	initialize: function(div, isAjax) {
		this.ears_switcher = div;
		
		if (isAjax) {
			this.indicator     = new AjaxIndicator(this.ears_switcher);
		}
		
		this.ears          = $$('#' + this.ears_switcher.id + ' ul.switcher li');
		this.divs          = $$('#' + this.ears_switcher.id + ' div.ear');

		this.ears.each(function(li, index) {
			if (isAjax) {
				if (li.hasClassName('active')) {
					this.active = li;
					this.active.div = this.divs[0];
				}
			} else {
				li.div = this.divs[index];
				if (li.hasClassName('active')) {
					this.active = li;
				} else {
					Element.hide(li.div);
				}
			}
			if (!li.hasClassName('inactive')) {
				Event.observe(li, 'click', this.change.bindAsEventListener(this));
			}
		}.bind(this));
		setFooter();
	}
}

// aktivace submit buttonu
function aktivuj(){
document.getElementById('odesli').disabled=false;
document.getElementById('vratZmeny').disabled=false;
}

function initEarsSwitchers() {
	$$('div.ears-switcher').each(function(ears_switcher, index) {
		ears_switcher.id = 'ears-switcher-' + index;
		var obj = new EarsSwitcher(ears_switcher);
	});
/*
	$$('div.ajax-switcher').each(function(ajax_switcher, index) {
		ajax_switcher.id = 'ajax-switcher-' + index;
		var obj = new EarsSwitcher(ajax_switcher, true);
	});
*/
}

Event.observe(window, 'load', initEarsSwitchers, false);
