$(document).ready(function() {
	$('a.button').each(function(){
		$(this).html('<span><b>'+$(this).html()+'</b></span>');
	});
	$('input[type=text]:not(.noinputstyle), input[type=password]:not(.noinputstyle)').addClass('input-text');
	$('input[type=submit]:not(.noinputstyle), input[type=reset]:not(.noinputstyle)').addClass('input-button');
	$('input[type=checkbox]:not(.noinputstyle)').addClass('input-checkbox');
	$('select:not(.noinputstyle)').addClass('input-select');

	function changeImg(element) {
		var newimage = $(element).attr('thumb');
		var fullimage = $(element).attr('href');
		var fullimagerel = 'galerija';
		$('#mainfoto img').attr('src','/UserFiles/images/'+newimage);
		$('#mainfoto a').attr('href',fullimage);
		$('#mainfoto a').attr('rel',fullimagerel);
		$('#foto a.active').attr('rel',fullimagerel);
		$('#foto a.active').removeClass('active');
		$(element).addClass('active');
		$(element).removeAttr('rel');
	}
	
	$('#foto a:last').css({'margin-right':'0'}).addClass('last');
	$('#foto a:first').addClass('first').addClass('active');
	$('#foto a').live('click',function(e){
		e.preventDefault();
		changeImg($(this));
	});
	if ($('#foto a').size() > 0) {
		changeImg($('#foto a:first'));
	}
	$('.arrow-right a').live('click',function(e){
		e.preventDefault();
		if ($('#foto a.active').hasClass('last') != true) {
			changeImg($('#foto a.active').next());
		}
	});
	$('.arrow-left a').live('click',function(e){
		e.preventDefault();
		if ($('#foto a.active').hasClass('first') != true) {
			changeImg($('#foto a.active').prev());
		}
	});
	$('#catalog tbody tr:odd').addClass('odd');
	
	$('#currency').change(function() {
  		$.cookie('currency', $('#currency option:selected').attr('value'), { expires: 30, path: '/'});
		$(location).attr('href',$(location).attr('href'));
	});
	
	$("a.ajax").live('click',function(e){
		e.preventDefault();
		var link = $(this).attr('href');
		var id = $(this).attr('id');
		id = id.substr(2);
		link = link.substring(0,link.length-1) + $('input[name=kiekis-'+id+']').attr('value');
		$.fancybox({
			'overlayShow' : true,
			'hideOnContentClick' : false,
			'titleShow' : false,
			'transitionIn' : 'none', 
			'transitionOut' : 'none',
			'scrolling' : 'no',
			'href' : link,
			'onCleanup' : updateCart
		});
	});
	
	function updateCart() {
		$('#krepselis').load('/cart/view.tpl');
	}
	$('#subscribe-submit').click(function(){
		$('#subscribe-form').submit();
	});
	
	$("#mainfoto a, a.fancybox").fancybox({
		'titleShow':false,
		'transitionIn':'elastic',
		'transitionOut':'elastic'
	});
	$("a.gallery").fancybox({
		'titlePosition'	: 'over',
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'fade'
	});
	
	$('.redirect-to-id').click(function(e){
		e.preventDefault();
		$(location).attr('href',$(this).attr('id'));
	});
	
	$('.radiodiv').click(function(){
		$(this).find('input[type=radio]').attr("checked",true);
	});
	
	$('#sveikinimo-txt').addClass('hidden');
	
	$('input[name=atvirukas]').change(function(){
		if ($(this).attr('value') > 0) {
			$('#sveikinimo-txt').removeClass('hidden');
		} else {
			$('#sveikinimo-txt').addClass('hidden')
		}
	});
	
	$('.meniu1 .inline-meniu li:last').addClass('last');
	$('#findus > a').click(function(e){
		var position = $('#findus').position();
		$('#findus div').css({'top':(position.top+23), 'left':position.left});
		$('#findus div').show();
		e.preventDefault();
	});
	$("div#findus").hoverIntent({
		interval: 50,
		over: function(){},
		out: function(){
			$('#findus div').hide();
		}}); 
	
	$('#catalog td').each(function(){
		$(this).find('span:not(:first)').hide();
	});
	$('select[name=selectas]').change(function() {
		var id = $(this).find('option:selected').attr('value');
		$(this).parent().parent().find('span').hide();
		$('span.p-'+id).show();
	});
	
	$('#meniu li.pasleptas ul').hide();
	$('#meniu li.pasleptas a[href=#show]').live('click',function(e){
		e.preventDefault();
		$('#meniu > li:not(.pasleptas) ul').slideUp(200,function(){
			$(this).parent().addClass('pasleptas');
		});
		$(this).parent().find('ul').slideDown(200,function(){
			$(this).parent().removeClass('pasleptas');
		});
	});
});
