(function(){
	/* SEARCH */
	var $search = $("#search"),
			$searchDiv = $search.find("div").css({width: "29px"}), 
			$sInputs = $searchDiv.children().hide() ,i=0,wp=29;

	$search.find("p").click(function (){
		if(i%2 == 0) {
			wp=391;
		} else {
			wp=29;
			$sInputs.hide();
		}
		$searchDiv.stop(true,true).animate({width: wp+'px'},1000,function() {
			if(wp==391) {
				$sInputs.fadeIn('slow');
			}
		});
		i++;
	});
	
	/* SLIDER */
	var $slider = $("#slider").find("ul"),
			$next = $("#header").find(".next"),
			$prev = $("#header").find(".prev"),
			$dots = $("#dots").find("li"),
			j=0,
			imgWidth= 948,
			imgs = 4,
			t;
			
	function changePic(ind) {
		if(ind>3) ind=0;
		if(ind<0) ind=3;
		j = ind;
		$slider.stop(true,true).animate({left: -(ind*imgWidth) + 'px'},'fast');
		$dots.find("a").removeClass("active").eq(ind).addClass("active");
		t = setTimeout(function() { changePic(++j); },5000);
	}
	changePic(j);
	$next.click(function() {
		clearTimeout(t);
		changePic(++j);
		return false;
	});
	$prev.click(function() {
		clearTimeout(t);
		changePic(--j);
		return false;
	});
	$dots.click(function() {
		clearTimeout(t);
		changePic($(this).index());
		return false;
	});
})();

/* Footer slider */
(function() {
	var widthLi = 320,
			$list = $("#izdvajamo ul"),
			listLiLength = $list.find('li').length,
			curentLi = 0;
			dir = "right";
	$list.css({ width : listLiLength * widthLi + 'px'});
	function ann() {
		if(curentLi >= (listLiLength-3)	&& dir == "right") {
			dir = "left";
		}
		if(curentLi <= 0 && dir == "left") {
			dir = "right";
		}
		if(dir == "right") {
			curentLi++; 
		} else {
			curentLi--;
		}
		leftPos = curentLi*widthLi;
		$list.stop(true,true).animate({left: -leftPos + "px"}, 5000, function() {
			setTimeout(ann,2000);
		});
		
	}
	ann();	
})();
(function() {
	$("#goTop").click(function() {
		$("body, html").animate({scrollTop: 0},'slow');
	});
})();

(function() {
	/* Gallery */
	
	var active = { width: '400px', height: '300px'},
			$gallery = $(".gallery").prepend('<div class="active">'+$(".gallery li:first a").html()+'</div><div class="galleryleft"></div><div class="galleryright"></div>'),
			$galleryItem = $gallery.find("ul li a"),
			listLength = $galleryItem.length,
			$galleryList = $gallery.find("ul")
															.css({width: listLength*105+'px' })
															.wrap('<div class="list-wrap" />'),
			$galleryActive = $gallery.find('.active img');
			i=0;												
			
															
			$('.galleryleft').click(function() {
				// go left
				i--;
				if(i>listLength-3) {
					i = listLength-4;
				}
				var leftPosition = i*105;
				if(leftPosition <= 0) { leftPosition = 0; i=0;}
				$galleryList.stop(true, false).animate({ left: -leftPosition + 'px'} , 'slow', 'easeOutCirc');
			});
			
			$('.galleryright').click(function() {
				// go right
				i++;
				var leftPosition = i*105;
				if(leftPosition > 105*listLength-340) { i=listLength; leftPosition = 105*listLength-345 }
				$galleryList.stop(true, false).animate({ left: -leftPosition + 'px'} , 'slow', 'easeOutCirc');
			});
			
			$galleryItem.click(function(e) {
				e.preventDefault();	
				$galleryActive.attr("src",$(this).attr("href"));
				return false;
			});
			
	
})();

