$(function() {
    
    // aktualitaates list
    
    var datenr = $('#actual').find('span.date').text();
    
    if(datenr.length === 0) {
		$('#actual').addClass("nodate");
	}
	
	
	/* linkiem - opendocÄ� ievietotajiem pieÅ�Ä·iras klase + tiem, kam jÄ�veras jaunÄ� logÄ� - targets */
	$('a.wysiwyg_link[href^=in_site/tools/download.php?file=]').addClass('fileintext');

	$("a[rel~=external]").attr('target', '_blank');

	/* inputu resets search formÄ� */
	var tmp = '';

	$('.search-field').focus(function() {
		tmp = $(this).attr('value');
		$(this).attr('value', '');
	});
	$('.search-field').blur(function() {
		if (this.value == '') {
			$(this).attr('value', tmp);
		}
	});

	if ($('#items-select').length)
	{
		$('#items-select').change(function()
		{
			var index = $('#items-select').val();
			if (ProdGroups[index])
			{
				window.location.href = ProdGroups[index];
			}
		});
	}

	/* atvÄ“rtai precei - rÄ�dÄ«t/slÄ“pt informÄ�cijas blokus */

	$("#desc a").click(function() {
		$("#item-sort li").removeClass('active');
		$("#desc").addClass('active');
		$(".opendoc").hide();
		$("#item-desc").show();
		return false;
	});

	$("#spec a").click(specClickFunction);
	
	//$(document).ready(specClickFunction);

function specClickFunction() {
    $("#item-sort li").removeClass('active');
		$("#spec").addClass('active');
		$(".opendoc").hide();
		$("#item-spec").show();
		return false;
}

	/* atvÄ“rtai precei, specifikÄ�cijai - bilde/thumbs uz priekÅ�u */

	$(".spec .controls a.fwrd").click(function() {
		$('.spec .image a').each(function() {
			if ($(this).hasClass('active')) {
				if ($(this).next().html() != null)	{
					$(this).next().addClass('active');
					$(this).removeClass('active');
				}
				return false;
			}
		});
		return false;
	});

	/* atvÄ“rtai precei, specifikÄ�cijai - bilde/thumbs uz atpakaÄ¼u */

	$(".spec .controls a.back").click(function() {
		$('.spec .image a').each(function() {
			if ($(this).hasClass('active')) {
				if ($(this).prev().html() != null)	{
					$(this).prev().addClass('active');
					$(this).removeClass('active');
				}
				return false;
			}
		});
		return false;
	});

	/* atvÄ“rtai precei, aprakstam - bilde/thumbs uz priekÅ�u */

	$(".desc .controls a.fwrd").click(function() {
		$('.desc .image a').each(function() {
			if ($(this).hasClass('active')) {
				if ($(this).next().html() != null)	{
					$(this).next().addClass('active');
					$(this).removeClass('active');
				}
				return false;
			}
		});
		return false;
	});

	/* atvÄ“rtai precei, aprakstam - bilde/thumbs uz atpakaÄ¼u */

	$(".desc .controls a.back").click(function() {
		$('.desc .image a').each(function() {
			if ($(this).hasClass('active')) {
				if ($(this).prev().html() != null)	{
					$(this).prev().addClass('active');
					$(this).removeClass('active');
				}
				return false;
			}
		});
		return false;
	});

	/* atvÄ“rta galerija - the magic, the power */

	if ($('.galleries').length > 0 && $('.scroller').length>0) {
		$("<div class='open-image'><h2></h2><a href='#'><img/></a></div>").insertAfter('#scroll-wrap');
		$(".scroller ul").css({marginLeft: '250px'});
		$(".scroller li").eq(0).addClass('active');
		$(".scroller li").eq(1).addClass('s1');
		$('.open-image h2').text($(".scroller a").eq(0).attr('title'));
		$('.open-image img').attr('src', $(".scroller a").eq(0).attr('href'));
		if ($(".scroller li").length>1) {
			$('<div class="controls fwrd"><a href="#" class="fwrd">Uz priekÅ�u</a></div>').insertAfter('.scroller ul');
			$('<div class="controls back"><a href="#" class="back">AtpakaÄ¼</a></div>').insertAfter('.scroller ul').hide();
		}

		function gotohell() {
			if (!$(this).parent().hasClass('active')) {
				var pin = $('.scroller li').index($(this).parent());
				var ml = ((2-pin)*125+'px');
				$(".scroller .back").hide();
				if (pin >= 1) {
					$(".scroller .back").show();
				}
				if (pin >= $('.scroller li').length-1) {
					$(".scroller .fwrd").hide();
				} else {
					$(".scroller .fwrd").show();
				}
				if (!($('.scroller').hasClass('wenttohell'))) {
					$('.open-image h2').text($(this).attr('title'));
					$('.open-image img').attr('src', $(this).attr('href'));
				}
				$(".scroller ul").animate( { marginLeft: ml });
				$(".scroller li").animate( { opacity: '0.5' }, 300).removeClass();
				$(this).parent().animate( { opacity: '1' }, 300).addClass('active').attr('id','active');
				$(".scroller li").eq(pin-1).addClass('s1');
				$(".scroller li").eq(pin+1).addClass('s1');
			}
			return false;
		}

		$(".scroller li a").click(gotohell);

		$(".scroller a.fwrd").click(function() {
			$('.scroller').addClass('wenttohell');
			$(".scroller .active").next().find('a').trigger('click');
			$('.scroller').removeClass('wenttohell');
			return false;
		});

		$(".scroller a.back").click(function() {
			$('.scroller').addClass('wenttohell');
			$(".scroller .active").prev().find('a').trigger('click');
			$('.scroller').removeClass('wenttohell');
			return false;
		});

		$('.open-image a').click(function() {
			var newhref = $(".scroller a[href="+$(this).find('img').attr('src')+"]").parent().next().find('a').attr('href');
			if (newhref != undefined) $('.open-image img').attr('src', newhref);
			$(".scroller a[href="+$(this).find('img').attr('src')+"]").trigger('click');
			return false;
		});
	}

	$('#wrapper').css({
		'backgroundPosition' : '50% '+ ($('#menu').height() + 248) +'px'
	});

	/* atraÅ�anÄ�s vieta */
	$('#location-map a').hover(function() {
		$('#location-popup').toggle();
	}, function() {
		$('#location-popup').toggle();
	});

	$('#location-popup').hover(function() {
		$('#location-popup').toggle();
	}, function() {
		$('#location-popup').toggle();
	});
});
