
var url_img_left_arrow_hover = '/img/img_scroll/img_s_l_arrow_hover.png';
var url_img_right_arrow_hover = '/img/img_scroll/img_s_r_arrow_hover.png';

var stand_left, summ_f=0;
var prev_width1,width1;
var position;
var rese=0;
var monoshirin=0;
var carousel_time=false;

function position_imgs() {
	var width2,temp3,position2,position3;
	var temp3;

	position = $(".img_cont > div.item_scroller:first").position();

	summ_f=0;
	summ_f+=($(".img_cont > div.item_scroller:first").width());

	temp3=parseInt($(".img_c").width()/$(".img_cont > div.item_scroller:first").width());
	if(temp3==1) {
		monoshirin=parseInt( ($(".img_c").width()-$(".img_cont > div.item_scroller:first").width()*temp3) );
	} else {
		monoshirin=parseInt( ($(".img_c").width()-$(".img_cont > div.item_scroller:first").width()*temp3) / (temp3-1) );
	}
	for (i=1;i<n;i++) {
		summ_f+=($(".img_cont > div.item_scroller:eq("+i+")").width());
	}

	if (summ_f<$(".img_c").width()) {
		width2=parseInt( ($(".img_c").width()-summ_f) /(n-1) );
	} else {
		if(temp3==1) {
			width2=parseInt( ($(".img_c").width()-$(".img_cont > div.item_scroller:first").width()*temp3) );
		} else {
			width2=parseInt( ($(".img_c").width()-$(".img_cont > div.item_scroller:first").width()*temp3) / (temp3-1) );
		}
	}

	if (rese==1) {
		$(".img_cont > div.item_scroller").css( {'top' : '0px', 'left': '0px'});
	}

	for (i=0;i<n;i++) {
		temp3=($(".img_cont > div.item_scroller:eq("+i+")").width());
	}

	for (i=1;i<n;i++) {
		position2 = $(".img_cont > div.item_scroller:eq("+i+")").position();
		position3 = $(".img_cont > div.item_scroller:eq("+(i-1)+")").position();
		$(".img_cont > div.item_scroller:eq("+i+")").css( {'top' : position.top-position2.top+"px",
														'left' : ( position3.left-position2.left+$(".img_cont > div.item_scroller:eq("+(i-1)+")").width()+width2 )+"px"});
		temp3=($(".img_cont > div.item_scroller:eq("+i+")").width());
	}
}


$(window).load(function() {

	n = $(".img_cont > div.item_scroller").length;
	
	var width2;

	position_imgs();

	position = $(".img_cont").position();
	stand_left=position.left;
	prev_width1=$(".img_c").width();
	width1=prev_width1;
	rese=1;
	$("#downloading").css ( {'display' : 'none'});

	$(document).mousemove (function() {
		var width2;
		prev_width1=width1;
		width1=$(".img_c").width();
		if (prev_width1!=width1) {
			position_imgs();
			if (summ_f>=$(".img_c").width()) {
				$(".img_cont").css ({'left' : '0px'});
			}
			position = $(".img_cont").position();
			stand_left=position.left;
		}
	});

	$("#img_arrow_l").mouseenter(function() {
		url_img_left_arrow = $("#img_arrow_l img").attr('src');
		$("#img_arrow_l img").attr({'src':url_img_left_arrow_hover});
	});
	$("#img_arrow_l").mouseleave(function() {
		if(typeof(url_img_left_arrow)!='undefined') {
			$("#img_arrow_l img").attr({'src':url_img_left_arrow});
		}
	});
	$("#img_arrow_r").mouseenter(function() {
		url_img_right_arrow = $("#img_arrow_r img").attr('src');
		$("#img_arrow_r img").attr({'src':url_img_right_arrow_hover});
	});
	$("#img_arrow_r").mouseleave(function() {
		if(typeof(url_img_right_arrow)!='undefined') {
			$("#img_arrow_r img").attr({'src':url_img_right_arrow});
		}
	});
});


function toright() {
	if($("#img_arrow_l").css('display')=='none') {
		$("#img_arrow_l").fadeIn();
	}

	//position = $(".img_cont").position();
	position = $(".img_cont").css('left');
	position = parseInt(position.replace("px",""));
	//alert(stand_left);
	var temp2=stand_left-position;
	//alert(position.left);
	var move_carousel = false;
	//if (typeof(summ)!='undefined') alert(summ);
	summ=-parseInt(temp2)-monoshirin;
	//alert(1+'. '+summ);
	for (i=0;i<n;i++) {
		summ+=($(".img_cont > div.item_scroller:eq("+i+")").width()+monoshirin);
//		alert(summ);
		if(summ>$(".img_c").width()) {
			if(i==n-1) {
				$("#img_arrow_r").fadeOut();
			}
			//alert(summ);
			//if ($.browser.msie) {
//				$(".img_cont").css ({ left : -(summ-$(".img_c").width()+temp2) });
			//} else {
				//alert(-(summ-$(".img_c").width()+temp2)+'px');
				$(".img_cont").animate ({ left : -(summ-$(".img_c").width()+temp2)+'px' },380);
			//}
			move_carousel=true;
				break;
		}
	}
	if (!move_carousel) {
			alert(summ);
 		$(".img_cont").animate ({left:0});
		move_carousel = false;
	}
	position = $(".img_cont").css('left');
	//alert(position);
}

function toleft() {
	if($("#img_arrow_r").css('display')=='none') {
		$("#img_arrow_r").fadeIn();
	}

	position = $(".img_cont").position();
	var temp2=stand_left-position.left;

	summ=-parseInt(temp2);
	if (summ<0) {
		for (i=0;i<n;i++) {
			summ+=($(".img_cont > div.item_scroller:eq("+i+")").width()+monoshirin);
			if(summ>=0) {
				if (-(temp2-($(".img_cont > div.item_scroller:eq("+i+")").width()-summ+monoshirin))==0) {
					$("#img_arrow_l").fadeOut();
				}
				$(".img_cont").animate ({ left : -(temp2-($(".img_cont > div.item_scroller:eq("+i+")").width()-summ+monoshirin)) }, 380);
				break;
			} 
		}
	}
}
