var history_steps=2;
$(function(){
	$('.over_left').mouseenter(function(){
		var i = (window.location.hash).substring(2);
		mouseCar(i, nr_pic);
	}).mouseleave(function(){
		clearInterval(mouseCarST);
	});
	$('.over_right').mouseenter(function(){
		
		var i = parseInt((window.location.hash).substring(2));
		if(i+2 < nr_pic){
			i = i + 2;
		}
		else{
			i = nr_pic;
		}
		mouseCar(i, nr_pic);
	}).mouseleave(function(){
		clearInterval(mouseCarST);
	});
	
	$('.pap_carousel').click(function(e){
		var i = (window.location.hash).substring(2);
		var x = e.pageX - $('.pap_carousel').offset().left;
		var tmpX = 0;
		while(tmpX + $('#x'+i).width() < x){
			tmpX+=$('#x'+i).width();
			if(!$('#x'+(++i)))
				return;
		}
		window.location.href=$('#x'+i).parent().attr('href');
	});
	
	var lat = $('.pap_main_poza img').width();
	if(!lat){
		lat=381;
	}
	var index = (window.location.hash).substring(2);
	if(!index){
		index = 0;
	}
	if ($('div.pap_main_buttons').length){
		window.location.hash = "p"+index;
	}
	var animate = -1*index*lat;
	$('.pap_carousel ul').animate({"left": animate+"px"});
	
	//detalii rochii
	$('#t1').addClass("active_thumb");
	var id = "";
	$('.thumb_images li').live('click', function(){
		id = $(this).attr("id").substr(1);
		width_pic = $('#b'+id).width();
		$("#b"+id).css("margin-left", parseInt(-1*width_pic/2));
		$('.thumb_images li').removeClass("active_thumb");
		$("div.pic img:visible").fadeOut("slow");
		$("#b"+id).fadeIn("slow");
		$('#t'+id).addClass("active_thumb");
	});
	$('div.pic').mousemove(function(e){
		var my=e.pageY-$(this).offset().top;
		var ratio=my/$(this).height();
		var img=$('img:visible', this);
		img.css('top', -1*ratio*(img.height()-$(this).height()));
		img.animate('top', '-1*ratio*(img.height()-$(this).height())', 'linear');
	});
});

$(window).load(function() {
var thumb_width = $('.thumb_images li img').width();
	$('.thumb_images li img').css('margin-left', -1*thumb_width/2);
});

var mouseCarST=false;
function mouseCar(obiect, nr_poze){
	var lat = $('.pap_main_poza img').width();
	var index_curent_pic = obiect;
	var index = (window.location.hash).substring(2);
	//alert(index_curent_pic+"-"+index);
	var diff = index_curent_pic - index;
	if(diff == 0){
		mouseCarST=setInterval(function(){
			car('right', nr_poze);
		}, 1000);
	}
	else if(diff == 2){

		mouseCarST=setInterval(function(){
			car('left', nr_poze);
		}, 1000);
	}

}
var iHasNews=false;
function car(side, nr_poze){
	var i = (window.location.hash).substring(2);
	var lat1 = $('#x'+i).width();
	var lat2 = $('#x'+(i-1)).width();
	var index = (window.location.hash).substring(2);
	var shift_limit = '';
	if(iHasNews){
		shift_limit = nr_poze-2;
	}
	else{
		shift_limit = nr_poze-3;
	}
	if(location.href.match(/portfolio-campanie/)){
		shift_limit = nr_poze-1;
	}
	if((index == 0 && side=="right") || (index >= shift_limit && side=="left")){
		return false;
	}
	else{
		// scos eventul de mouseover de pe toate pozele
		if(side == "left"){
			$('.pap_carousel ul').animate({"left": "-="+lat1+"px"});
			window.location.hash = "p"+index++;
		}
		else if(side == "right"){
			$('.pap_carousel ul').animate({"left": "+="+lat2+"px"});
			window.location.hash = "p"+index--;
		}

		window.location.hash = "p"+index;
		history_steps++;
	}
}

var	centerSplash={
	runs: 0,
	repeater: false,
	init: function(){
		centerSplash.repeater=setInterval(centerSplash.go, 500);
	},
	go: function(){
		centerSplash.runs++;
		if($('#b1').width()>200){
			// se centraza
			var width_pic = $('#b1').width();
			$('#b1').css('margin-left', parseInt(-1*width_pic/2));
			$('#b1').show();
			clearInterval(centerSplash.repeater);
		}
		else if(centerSplash.runs>20)
			clearInterval(centerSplash.repeater);
	}
}

