var base = '/';

if (document.location.hash && document.location.hash.substr(1,1)=='!') {
	document.location=document.location.hash.substr(2);
} else {
	var lsth = '';
	function hashCheck() {
		var thsh = document.location.hash.substr(1);
		if (lsth !== thsh && lsth.substr(0,1)=='!') {lsth = thsh;hashBrowse(lsth.substr(1));}
	}
	setInterval(hashCheck, 100);
}

$(window).load(function() {
	$('input,textarea').live('blur',function(){$(this).val($(this).val().trim())});
	$('input[maxlength]').live('keyup',function(){if($(this).val().length==parseInt($(this).attr("maxlength"))){$(this).next('input').focus();}});
	$('#search_q').live('focus',function(){if($(this).val()==translate.SEARCH){$(this).val('');}});
	$('#search_q').live('blur',function(){if($(this).val()==''){$(this).val(translate.SEARCH);}});
	$('#search_q').live('keypress',function(e){if(e.which==13){e.preventDefault();siteSearch();return false;}});
	$("header h1 a, header li:not('.lan') a").live('click',function(){hashBrowse($(this).attr('href'),false);return false;});
	$('footer nav a').live('click',function(){hashBrowse($(this).attr('href'),'#content');return false;});
	$('#logos a').live('click',function(){hashBrowse($(this).attr('href'),'#logos');return false;});
	$('.ellip p.more a').live('click',function(){$(this).parent().hide();$(this).parent().siblings('p').fadeIn();return false;});
	pageLayout();
});

function calendar(y,m,f) {
	$('#'+(f?'':'big-')+'calendar').load(base+'scripts/calendar/?a=c&f='+f+'&y='+y+'&m='+m);
}

function calendarEvents(y,m,d) {
	if ($('#calendar_'+d+' .popup').length) {
		$('#calendar_'+d+' .popup').fadeOut(function(){$(this).remove()});
	} else {
		$('#calendar .popup').fadeOut(function(){$(this).remove()});
		$.getScript(base+'scripts/calendar/?a=e&y='+y+'&m='+m+'&d='+d);
	}
}

function formKeyEnter(e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return false;
	}
	return (keycode == 13);
}

function hashBrowse(q,s) {
	if (typeof html5 != "undefined" && html5 == false) {
		document.location = q;
	} else {
		h = document.domain;
		if(q.indexOf(h)!=-1)q=q.substr(q.indexOf(h)+h.length);
		$('#content').load(q+(q.indexOf('?')!=-1?'&':'?')+'ajax', function(){pageLayout();_gaq.push(['_trackPageview', q]);if(s!==false){scrollToElement(s);}});
		location.hash = '!'+q;
		lsth = location.hash.substr(1);
	}
}
function hashRefresh() {
	hashBrowse(hashReturn(),false);
}
function hashReturn() {
	return ((document.location.hash&&document.location.hash.substr(1,1)=='!')?location.hash.substr(2):window.location.pathname);	
}

function imageBrowse(e,b,a) {
	switch (a) {
		case '+1':
			$('#carousel'+b+' div ul').animate({ left: parseInt($('#carousel'+b+' div ul').css('left'))-360},{duration:'slow',easing:'swing',complete:function(){if(parseInt($('#carousel'+b+' div ul').css('left'))+$('#carousel'+b+' div ul').width()<=0){$('#carousel'+b+' div ul').css('left','360px');imageBrowse(e,b,a);}}});
			break;
		case '-1':
			$('#carousel'+b+' div ul').animate({ left: parseInt($('#carousel'+b+' div ul').css('left'))+360},{duration:'slow',easing:'swing',complete:function(){if(parseInt($('#carousel'+b+' div ul').css('left'))>=360){$('#carousel'+b+' div ul').css('left',(-360*Math.ceil($('#carousel'+b+' div ul').width()/360))+'px');imageBrowse(e,b,a);}}});
			break;
		default:
			$('#image'+b).load(base+'scripts/images/?i='+a,function(){$('#image'+b+' img').fadeIn();});
			$('#carousel'+b+' li a').removeClass('selected');
			$(e).addClass('selected');
			break;
	}
}

function pageLayout() {
	$('.ellip p').not(':first-child,.more').hide();
	$('.ellip p:first-child').show();
	$('.ellip').each(function(){with($(this).find('p.more')){if($(this).find("p:not('.more')").length>1){show();}else{hide();}}});
}

function scrollToElement(e) {
	$('html,body').animate({ scrollTop: $(e).offset().top},{duration:'slow',easing:'swing'});
}

function siteSearch(q) {
	if ($('#search_q').val()!=''&&$('#search_q').val()!=translate.SEARCH) {
		_gaq.push(['_trackPageview', window.location.pathname+'?q='+encodeURIComponent($('#search_q').val())]);
		$('#search').submit();
	}
}

