$(function() {
	if ( $('#index').length && !(jQuery.browser.msie==true && jQuery.browser.version.substr(0,1)<7) && !(jQuery.browser.opera==true && jQuery.browser.version<9.2) ) {
		for ( e=1; e<7; e++ ) {
			fade(('e'+e),((Math.floor(Math.random()*(30-7+1))+7)*100));
			$('#e'+e).mouseover( function() { $('#'+this.id).stop(); $('#'+this.id).css({opacity:"1.0"}); });
			$('#e'+e).mouseout( function() { fade(this.id,1500); });
		}
	}

	if ( $('#subscribe').length ) {
		$('#subscribe>span>a').click(function() {
			$('#subscribe>span').html('<form onsubmit="return subscribe()">El.pašto adresas: <input type="text" id="address" /> <input type="submit" id="submit" value="&raquo; prenumeruoti" /> <img src="img/loading.gif" /></form>');
			$('#subscribe #address').focus();
		});
	}

	if ( $('#content>p#addr').length ) { $('#content>p#addr>br').after('<a href="ma'+'ilto:'+String.fromCharCode(101,107,111,108,111,103,105,106,97,64)+'zaliazalia.'+'lt'+'">'+String.fromCharCode(101,107,111,108,111,103,105,106,97,64)+'zaliazalia.'+'lt'+'</a>'); }

	if ( $('#content a#map').length ) {
		$('#content a#map').click(function() {
			$('body').click(function() { if ($('#mapfull').is(':visible')) { $('#mapfull').hide(); } });
			$('#mapfull').show(); return false;
		});
	}
	
	// paginate content
	if ( ($('#content #news').length && $('#content #news>.page').length>1) || ($('#content.products>#list').length && $('#content.products>#list>ul').length>3) ) {
		if ( $('#content.products').length ) {
			var pages = Math.ceil($('#content.products>#list>ul').length/3);
		} else {
			var pages = $('#content .page').length;
		}
		var dashes = '';
		while ( dashes.length<200 ) { dashes+='_ '; }

		showPage = function(num,init) {
			if ( Number(num)<1 || Number(num)>pages ) { return false; }
			
			//scroll
			if ( $('#content.products').length ) {
				if (init) { $('#content.products>#list').css({'marginLeft':(num-1)*-588}); }
				else { $('#content.products>#list').animate({'marginLeft':(num-1)*-588},800); }
			} else {
				if (init) { $('#content>div').css({'marginLeft':(num-1)*-720}); }
				else { $('#content>div').animate({'marginLeft':(num-1)*-720},800); }
			}
			//nav
			var links='';
			for ( var p=0; p<pages; p++ ) {
				if (p+1==num) { links+= String(p+1); }
				else { links+= '<a href="#'+String(p+1)+'">'+String(p+1)+'</a>'; }
				if (p<pages-1) { links+=' _ '; }
			}
	  		$('#content>.paginate').html('<span>'+dashes+links+'</span>');
			$('#content>.paginate a').click(function(){
				var hash = $(this).attr('href').split('#');
				location.hash = hash[1];
				showPage(hash[1]);
				return false;
			});
		}
		$('#content').append('<div class="paginate"></div>');
		if ( Number(location.hash.substr(1))>0 && Number(location.hash.substr(1))<=pages ) {
			showPage(Number(location.hash.substr(1)),'init');
		} else { showPage(1,'init'); }
	}
	
	if ( $('#content.products>#menu>li.group').length ) {
		var unfolded=false;
		$('#content.products>#menu>li.group').each(function(i){
			if ( $(this).find('ul>li').not($('li:has(a)')).length && !unfolded ) {
				unfolded=true;
				$(this).find('>a').addClass('open');
			} else {
				$(this).find('>ul').css({display:"none"});
			}
			$(this).find('>a').click(function() {
				if ( !$(this).is('.open') || ($(this).is('.open') && $('#content.products>#menu>li.nongroup').length) ) {
					$('#content.products>#menu>li.group>a.open').parent().find('>ul').slideUp(300);
					if ( $(this).is('.open') && $('#content.products>#menu>li.nongroup').length ) {
						$('#content.products>#menu>li.group>a').removeClass('open');
					} else {
						$('#content.products>#menu>li.group>a').removeClass('open');
						$(this).addClass('open');
						$(this).parent().find('>ul').slideDown(300);
					}
				}
				$(this).blur();
				return false;
			});

		});
	}

	if ( $('#content.products>#list').length ) {
		$('#content.products>#list>ul>li').each(function(i){
			$(this).find('img').wrap('<a href="'+ $(this).find('a').attr('href') +'"></a>');
		});
	}
	
	try { var pageTracker = _gat._getTracker("UA-1283498-15"); pageTracker._trackPageview(); } catch(err) {}
});

function fade(element,msec) {
	$('#'+element).fadeTo(msec, 0.3, function() {
		$('#'+this.id).fadeTo(1500, 0.8, function() {
			fade(this.id,1500);
		});
	});
}

function subscribe() {
	var regex = /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i;
	if (!regex.test( $('#subscribe #address').attr("value") )) {
		alert("Prašome patikrinti nurodytą adresą.")
		$('#subscribe #address').focus();
		return false;
	}
	$('#subscribe>span>form>img').css({visibility:"visible"});
	$.post("/subscribe.php", { addr: $('#subscribe #address').attr("value") },
		function(data) {
			$('#subscribe>span').html('Dėkojame, naujienų prenumeratos užsakymas perduotas.');
		}
	);
	return false;
}

