//var dbgFlag = false; // magic debugging

document.observe('dom:loaded', function(){ // one and only domload observer, order is important!
	CookieBanner.init();
	popupOutInit();
	detailInit();	
	tabInit();
	cleanerMain();
	reviewInit();
	resizeInit();					// all tabs start here
	carouselInit();
	banInit();
	regInit();
	twoBoxInit();
	VideoInit();
	InterviewInit();
	FotogallInit();
	closeInit();
});

CookieBanner = {
	name: 'ecb',
	timeout: null,
	
	init: function() { 
		if ($('cookiebanner')) if (CookieBanner.getc(CookieBanner.name) != 1) {
			CookieBanner.timeout = setTimeout(function(){ CookieBanner.work(); }, 75000);
		}
	},
	
	work: function() { 
			CookieBanner.setc(CookieBanner.name, 1, 7);
			ytop = document.viewport.getScrollOffsets().top + 200;
			
			$('cookiebanner').show().setStyle({
				'top' : ytop+'px'
			});
			
			$$('#cookiebanner .close').each(function(el) {
				el.observe('click', function(ev) { $('cookiebanner').hide(); });
			});
	},

	getc: function(cn) {
		if (document.cookie.length > 0) {
			ca = document.cookie.indexOf(cn + '=');
			if (ca != -1) {
				ca = ca + cn.length + 1;
				cb = document.cookie.indexOf(';', ca);
				if (cb == -1) cb = document.cookie.length;
				return unescape(document.cookie.substring(ca, cb));
			}
		}
		return '';
	},

	setc: function(cn, cv, ce) {
		var today = new Date();
		today.setTime( today.getTime() );
		if (ce) ce = ce * 1000 * 60 * 60 * 24;
		var ce_date = new Date( today.getTime() + (ce) );
		document.cookie = cn + "=" +escape(cv) +	((ce) ? ";expires=" + ce_date.toGMTString() : "");
	}
};


function dbg(output) {
	if (dbgFlag) if (console) console.log(output);
}

function closeInit() {
	document.observe('click', function(ev) {
		el = ev.element();
		
		if (!el.up('.prototip')) Tips.hideAll();
	});
}


function carouselInit() {
	if ($$("#horizontal_carousel").size()) {
		hCarousel = new UI.Carousel("horizontal_carousel");	
	}
}

function regAjax(field, trigger, response) {
		trigger.observe('blur', function(ev){ 
			new Ajax.Request('services/UserCheck.php', {
				method: 'post',
				parameters: {
					input: field,
					content: trigger.getValue()
				},
				onFailure: function() { alert('Chyba komunikácie, skúste znova!'); },
				onSuccess: function (rq) {
					eval(rq.responseText);
					response.addClassName(flag);
					response.update(text);
				}
			});
		});
}

function regInit() {
	if ($$('.registracia').size()) {
		regAjax('login', $('ajax_login_trigger'), $('ajax_login_response')); 
		regAjax('email', $('ajax_email_trigger'), $('ajax_email_response'));
	//	regAjax('password', $('ajax_password_trigger'), $('ajax_password_response'));
	}
}

function popupOutInit()
{
	Event.observe(document, 'click', function(e){
 		var el = Event.element(e);
 		if (!el.descendantOf('other_menu'))
 		{
			$('other_menu').hide();
 		}
 	}); 
	
	Event.observe($('other'), 'click', function(e){
		 $('other_menu').toggle();
		 e.stop(); 
		 
	}); 
	
}

function showMailCaptcha() {
	$$('#send_mail, #send_captcha').invoke('show');
	$('send_error').hide();
	$('send_captcha').removeClassName('error_captcha').removeClassName('mail_captcha').addClassName('mail_captcha');
	
	y = 170;// top value from css
	if ($$('#error_box_email').size()) {
		if ( $('send_captcha').hasClassName('mail_captcha') )   
		xxx = y + $('error_box_email').getHeight();
		$('send_captcha').setStyle({ 'top': xxx + 'px' });
	} else {
		$('send_captcha').setStyle({ 'top': y + 'px' });
	}
}

function showErrorCaptcha() {
	$$('#send_error, #send_captcha').invoke('show');
	$('send_mail').hide();
	$('send_captcha').removeClassName('error_captcha').removeClassName('mail_captcha').addClassName('error_captcha');
	
	y = 327; // top value from css
	if ($$('#error_box_report').size()) {
		if ( $('send_captcha').hasClassName('error_captcha') )   
		xxx = y + $('error_box_report').getHeight();
		$('send_captcha').setStyle({ 'top': xxx + 'px' });
	} else {
		$('send_captcha').setStyle({ 'top': y + 'px' });
	}
}

function detailInit() {
	
	if ($$("#send_captcha").size()) {
		
		$('mail').observe('click', function(){
			showMailCaptcha();
			return false;
		});

		$('error').observe('click', function(){
			showErrorCaptcha();
			return false;
		});

		$$('#send_error .hide, #send_mail .hide').each(function(el){
			
			el.observe('click', function(ev){
				ev.stop();
				$$('#send_mail, #send_error, #send_captcha').invoke('hide').removeClassName('error_captcha').removeClassName('mail_captcha');
				
			})
		});
	}
}


function resizeInit() {
	
	if($('detail') != undefined)
	{
		if($('detail').hasClassName('t-plusplus')) $('resize-plus').addClassName('disabled');
		if(($('detail').hasClassName('t-plusplus') == false) && ($('detail').hasClassName('t-plus') == false)) $('resize-minus').addClassName('disabled');
	
		Event.observe($('resize-plus'), 'click', function(event) { event.stop(); mmResize('plus'); });
		Event.observe($('resize-minus'), 'click', function(event) { event.stop(); mmResize('minus'); });
	}
};
	
function mmResize(what) {
	if(what == 'plus' && ($('resize-plus').hasClassName('disabled') != 'true')) {
		if($('detail').hasClassName('plus')) 
		{
			$('detail').removeClassName('plus');
			$('detail').addClassName('plusplus'); 
			$('resize-plus').addClassName('disabled');
		}
		else 
		{ 
			$('detail').addClassName('plus');
			$('resize-minus').removeClassName('disabled');
		}
	}

	if(what == 'minus' && ($('resize-minus').hasClassName('disabled') != 'true')) {
		if($('detail').hasClassName('plusplus')) 
		{
			$('detail').removeClassName('plusplus'); 
			$('detail').addClassName('plus');
			$('resize-plus').removeClassName('disabled');
		}
		else 
		{ 
			$('detail').removeClassName('plus');
			$('resize-minus').addClassName('disabled');
		}
	}
}


function tabInit() {
//	dbg('tabInit');

	$$('.j-tab').each(function(el){
		if (el.down('.active') != undefined) {
			cn = el.down('.active').down('a').readAttribute('href').split('=').last();
			ok = true;
		} else ok = false;

		new Control.Tabs(el);

		if (ok) Control.Tabs.findByTabId(cn).setActiveTab(cn);  
	});

//	dbg('tabInit');
}


function openPopup(url,name,height,width,opt_show_scrollbars){
	var scrollbar_param=opt_show_scrollbars?",scrollbars=1":"";
	var newwindow=window.open(url,name,'height='+height+',width='+width+scrollbar_param);
	if(newwindow&&!newwindow.opener){newwindow.opener=window;}
	if(window.focus){newwindow.focus()}
	return false;
}

function reviewInit()
{
	if($$('.review h3') != undefined)
	{
		var h3 = $$('.review h3');
		if(h3.length == 2)
		{
			hh31 = h3[0].getDimensions();
			hh32 = h3[1].getDimensions();
			
			if (hh31.height >= hh32.height)
			{
				h3[0].setStyle({height: hh31.height-8+'px'});
				h3[1].setStyle({height: hh31.height-8+'px'});
			}
			else
			{
				h3[0].setStyle({height: hh32.height-8+'px'});
				h3[1].setStyle({height: hh32.height-8+'px'});
			}
		}
	}
}

function twoBoxInit()
{
	if($$('.twobox h3') != undefined)
	{
		var h3 = $$('.twobox h3');
		if(h3.length == 2)
		{	
			var h3 = $$('.twobox h3');
			hh31 = h3[0].getDimensions();
			hh32 = h3[1].getDimensions();
			
			if (hh31.height >= hh32.height)
			{
				h3[0].setStyle({height: hh31.height-8+'px'});
				h3[1].setStyle({height: hh31.height-8+'px'});
			}
			else
			{
				h3[0].setStyle({height: hh32.height-8+'px'});
				h3[1].setStyle({height: hh32.height-8+'px'});
			}
		}
	}
	if($$('.twobox img') != undefined)
	{
		var Imgs = $$('.twobox img');
		if(Imgs.length == 2)
		{
			off1 = Imgs[0].offsetTop;
			off2 = Imgs[1].offsetTop;
			
			if (off1 > off2)
			{
				Imgs[1].setStyle({margin: off1-off2+'px 0 0 0'});
			}
			else if (off1 < off2)
			{
				Imgs[0].setStyle({margin: off2-off1+'px 0 0 0'});
			}
		}
	}
}
function InterviewInit()
{
	if($$('.interview img') != undefined)
	{
		var Imgs = $$('.interview img');
		if(Imgs.length == 2)
		{
			off1 = Imgs[0].offsetTop;
			off2 = Imgs[1].offsetTop;
			
			if (off1 > off2)
			{
				Imgs[1].setStyle({margin: off1-off2+'px 0 0 0'});
			}
			else if (off1 < off2)
			{
				Imgs[0].setStyle({margin: off2-off1+'px 0 0 0'});
			}
		}
	}
}
function VideoInit()
{
	if($$('.video_articles .v') != undefined)
	{
		var Imgs = $$('.video_articles .v');
		if(Imgs.length == 2)
		{
			off1 = Imgs[0].offsetTop;
			off2 = Imgs[1].offsetTop;
			
			if (off1 > off2)
			{
				Imgs[1].setStyle({margin: off1-off2+'px 0 0 0'});
			}
			else if (off1 < off2)
			{
				Imgs[0].setStyle({margin: off2-off1+'px 0 0 0'});
			}
		}
	}
}
function FotogallInit()
{
	if($$('.fotogall img') != undefined)
	{
		var Imgs = $$('.fotogall img');
		if(Imgs.length == 2)
		{
			off1 = Imgs[0].offsetTop;
			off2 = Imgs[1].offsetTop;
			
			if (off1 > off2)
			{
				Imgs[1].setStyle({margin: off1-off2+'px 0 0 0'});
			}
			else if (off1 < off2)
			{
				Imgs[0].setStyle({margin: off2-off1+'px 0 0 0'});
			}
		}
	}
}

//
//	Specialny js pre archiv pre menenie action formu pri zmene sekcie
//
var FormSectionChanger = Class.create({
	selector: null,
	value: null,
	form: null,
	initialize: function() {
		this.selector = $('sectionSelect');
		this.form = $('searchForm')
		this.value = this.selector.value;
		this.selector.observe('change', this.changeFormAction.bind(this));
	},
	changeFormAction: function(event) {
		var value = this.selector.value;
		this.form.action = this.form.action.replace('/' + this.value + '/', '/' + value + '/');
		this.value = value;
	}
});

function banInit() {
	$$(".ban").each(function(el) {
		if (el.getHeight() > 20) {
			el.insert({ bottom: '<small>Reklama</small>' });
		}
	});
}

function registerNewsletter() {
	info = $('newsletter_registration_info');
	
	new Ajax.Request('index.php?eID=register_email_to_newsletter', {
		method: 'post',
		parameters: {language: $('newsletter_language').value, name: $('newsletter_name').value, organization: $('newsletter_organization').value, email: $('newsletter_email').value},
		
		onSuccess: function(transport)
		{
			info.update(transport.responseText);
			info.style.display = '';
		},
		
		onFailure: function()
		{
			alert('Na serveri sa vyskytla chyba. Skúste neskôr prosím.');
		}
	});
	return false;
}

function sizeAudioPlayer(height,videoId) {
	$$('#video-box-'+videoId + ' embed').each(function(e) {
		e.morph('height:' + height + 'px');
	});
}
function resizeCalculator(height, calcId) {
	$$('#calc-' + calcId + ' embed').each(function(e) {
		e.height = height;
	});
}

function cleanerDoset(element, value) {
	element.value = value;
}

function cleanerReset(element) {
  if (element.value == '') {
    cleanerDoset(element, element.attributes.getNamedItem("title").value);
  }
}

function cleanerMain() {
	$$('input.input_cleaner').each( function(element) {
		element.observe('focus', function(event) {

			if (element.value == element.attributes.getNamedItem("title").value) {
				cleanerDoset(event.element(), '');
			}
			event.element().activate();
		});
		
		element.observe('blur', function(event) {
			cleanerReset(event.element())
		});

		cleanerReset(element, true);
	});
}
