$(document).ready(function() {
	
	// custom fade animation
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};
	
	$('#logo').flashembed({
		src:			'/logo.swf',
		version:		[10,0],
		width:			'125',
		height:			'50',
		wmode:			'transparent',
		w3c:			true
	});
	
	$('#pradinis').flashembed({
		src:			'/gym.swf',
		version:		[10,0],
		width:			'956',
		height:			'412',
		wmode:			'transparent',
		w3c:			true
	});

	
	// sulyginam sarasa / atsizvelgt i eilutes???
	var subs = $('#sarasas .sub');
	if(subs.length > 0) {
		var max_height = 0;
		subs.each(function() {
			var aukstis = $(this).height();
			if(aukstis > max_height) max_height = aukstis;
		});
		subs.height(max_height);
	}
	
	// FANCYBOX
	$('a.group').fancybox({
		'zoomSpeedIn'			: 600,
		'zoomSpeedOut'			: 500,
		'easingIn'				: 'easeOutBack',
		'easingOut'				: 'easeInBack'
	});
	
	// uzsiprenumeruojam
	$('#pren1').click(function() {
		prenumeruoti(true);
		return false;
	})
	
	// atsisakom prenumeratos
	$('#pren2').click(function() {
		prenumeruoti(false);
		return false;
	})
	
	// balsuojam
	$('#survey1').click(function() {
		var forma = $('#forma_apklausa');
		var klaida = $('#klaida_apklausa');
		if($('input[name="apklausa"]:checked').length > 0) {
			forma.hide();
			klaida.hide();
			$('#rezultatai_apklausa').load('/ajax/vote.tpl', forma.serialize());
		} else {
			$('#klaida_apklausa').text('Pasirinkite atsakymą');
		}
		
		return false;
	})
	
	// ziurim apklausos rezultatus
	$('#survey2').click(function() {
		var forma = $('#forma_apklausa');
		var klaida = $('#klaida_apklausa');
		forma.hide();
		klaida.hide();
		$('#rezultatai_apklausa').load('/ajax/forma_apklausa_rezultatai.tpl');
		return false;
	})
	
	// akcijos popup
	$('#akcija, #akcija_close').click(function() {
		$('#akcija_popup').fadeToggle();
		return false;
	})
	
	// klausimas popup
	$('#klausimams a, #klausimas_close').not('#klausimas_submit').click(function() {
		$('#forma_klausimas').fadeToggle();
		return false;
	});
	
	$('#klausimas_submit').click(function() {
		process_klausimas();
		return false;
	})
	
	// apvalus kampai
	$('#meniu ul.sub').corner();
	$('#meniu div.sub_multi').corner();
	
	$('#meniu li').hover(
			function() {
				$('ul.sub', this).css('display', 'block');
				$('.sub_multi', this).css('display', 'block').css('height', 'auto');
				$('span', this).css('display', 'block');
			},
			function() {
				$('ul.sub', this).css('display', 'none');
				$('.sub_multi', this).css('display', 'none');
				$('span', this).css('display', 'none');
			}
	);
	
	$('.sub_multi').siblings('a').click(function() {
		return false;
	});
	
	// treniruokliai
	$('#tabs').tabs('#panes > div.pane');
	
	$('#panes a').hover(
		function() {
			var reiksme = $(this).attr('rel');
			var virsus = $(this).css('top');
			$(this).parent().next().html(reiksme);
			$(this).parent().next().css({'padding-top':parseInt(virsus) + 'px'});
		},
		function() {
			$(this).parent().next().html('');
		}
	);
	
	// h1 
	$('h1').append('<a href="/">grįžti atgal</a>');
	$('h1').live('click', function() {
		history.back();
		return false;
	});
	
	// uzpildom formas
	uzpildytiForma('forma_prenumerata');
	uzpildytiForma('forma_klausimas');


});

/*
	FUNKCIJOS -------------------------------------------------------------------------
*/

// img popup
function activatecmsShowImagePopup(url,title,width,height){
	var narsykle = window.opera || ( document.layers && !navigator.mimeTypes['*'] ) || navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ) || ( window.ScriptEngine && ScriptEngine() == 'JScript' && navigator.platform == 'Win32' && window.ActiveXObject && !navigator.__ice_version );
	var left=window.screen.width/2-width/2;
	var top=window.screen.height/2-height/2;
	var langas = window.open( ( narsykle ? '' : 'about:blank' ), 'nuotraukos', "height="+(height+2)+",width="+width+",left="+left+",top="+top+',location=0,menubar=0,scrollbars=0,status=0,toolbar=0' );
	html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
	html+='<html xmlns="http://www.w3.org/1999/xhtml" lang="lt" xml:lang="lt">';
	html+='<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />';
	html+='<title>'+title+'<\/title>';
	html+='<style type="text/css">body{margin:0;padding:0;border:0;}<\/style>';
	html+='<\/head>';
	html+='<body>';
	html+='<a href="#" onclick="window.close()"><img src="'+unescape(url)+'" border="0"/><\/a>';
	html+='<\/body>';
	html+='<\/html>';
	
	langas.document.open();
	langas.document.write(html);
	langas.document.close();
	langas.focus();
	return false;
}

function uzpildytiForma(id) {
	values = gautiPradinesReiksmes();
	$('#'+ id + ' :input').each(function(i, el) {
		
		var ell = $(el);
		var pr= (values[ell.attr('name')] == undefined) ? '' : values[ell.attr('name')];

		if(ell.val() == '') 	ell.val(pr);

		ell.blur(function () {
			if(ell.val() == '') 	ell.val(pr);
		});
		
		ell.focus(function () {
			if(ell.val() == pr) 	ell.val('');
		});
	});
}

function valytiForma(id) {
	values = gautiPradinesReiksmes();
	$('#'+ id + ' :input').each(function(i, el) {
		
		var ell = $(el);
		var pr= (values[ell.attr('name')] == undefined) ? '' : values[ell.attr('name')];
		ell.val(pr);
		
	});
	return false;
}

// tikrinam el pasta
function check_email(str) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	(filter.test(str)) ? testresults=true : testresults=false;
	return (testresults)
}

// prenumeruoti
function prenumeruoti(pren) {
	values = gautiPradinesReiksmes();
	mess = gautiKlaidas();
	forma = $('#forma_prenumerata');
	klaida = $('#klaida_prenumerata');
	geri = true;
	
	el = $('#prenumerata_elpastas');
	if(check_email($.trim(el.val())) == false) {
		klaida.text(mess[el.attr('id')]);
		el.focus();
		geri = false;
		return false;
	}
	
	if (geri) {
		forma.hide();
		klaida.html('<img src="/images/load.gif" alt="loader" />');
		if(pren) {
			klaida.load('/ajax/prenumerata1.tpl',forma.serialize());
		} else {
			klaida.load('/ajax/prenumerata2.tpl',forma.serialize());
		}
	}
}

function loadRate(id) {
	$('#rate_'+id).load('/ajax/rate.tpl',{id:id});
	return false;
}

function addRate(id, rate) {
	if(rate==1) {
		$('#rate_'+id).load('/ajax/rate.tpl',{'id':id,'plius':1});
	} else {
		$('#rate_'+id).load('/ajax/rate.tpl',{'id':id,'minus':1});
	}
	return false;
}

// uzklausa
function process_uzklausa() {
	forma = $('#forma_uzklausa');
	klaida = $('#klaida_uzklausa');
	geri = true;

	forma.children(":input").each(function(i, e) {
		el = $(e);
			if($.trim(el.val()) == '') {
				klaida.text('Užpildykite visus laukelius');
				el.focus();
				geri = false;
				return false;
			} else if(el.attr('id') == "uzklausa_elpastas" && check_email(el.val()) == false) {
				klaida.text('Netinkamas el. paštas');
				el.focus();
				geri = false;
				return false;
			}
	});

	if (geri) forma.submit();
	return false;
}

// uzklausa
function process_klausimas() {
	var forma = $('#forma_klausimas');
	var values = gautiPradinesReiksmes();
	geri = true;

	forma.children(":input").each(function(i, e) {
		el = $(e);
			if($.trim(el.val()) == '' || $.trim(el.val()) == values[el.attr('id')]) {
				//klaida.text('Užpildykite visus laukelius');
				el.focus();
				geri = false;
				return false;
			}
	});

	if (geri) forma.submit();
	return false;
}

function showFoto(id) {
	$('.foto span').hide();
	$('#foto_'+id).show();
}

