/*
 * Name: Interface script for "Eggo" site
 * Author: Shmakov Sergey (sh-pro@yandex.ru)
 */
$(document).ready(function(){

	if ($("#gallery-port").length!=0) {
		
		// Prepare images list in the view-port (set left-position)
		var gil_left = 0;
		$("#g-image-list .pos").each(function() {
			$(this).css("left",gil_left+"px");
			gil_left = gil_left + 105;
		});		
		var gil_width = gil_left - 19;
		
		var gilLeft = function() {
			var regExpObj = new RegExp(/\D\D/);
			var gil_left_real = $("#g-image-list").css("left").replace(regExpObj,"");
			return parseInt(gil_left_real);
		}
		
		// Function for control buttons "active-status" checking and setting them class if it needly
		var actCheck = function() {
			var vp_width = 296;
			gil_left_real = gilLeft();
			if ($("#g-image-list").css("left")=="105px") $("#g-control-left").removeClass("active");
			if ($("#g-image-list").css("left")==((vp_width-gil_width-105)+"px")) $("#g-control-right").removeClass("active");
			if (gil_left_real>(vp_width-gil_width-105) && vp_width<gil_width) $("#g-control-right").addClass("active");
			if (gil_left_real<105) $("#g-control-left").addClass("active");
			
			if ($("#g-image-list .pos.active").length!=0) $("#g-image-list .pos.active").removeClass("active");
			
			$("#g-image-list .pos").each(function() {
				if ($(this).css("left")==((105-gil_left_real)+"px")) {
					$(this).addClass("active");
					$("#g-main-image img").attr("src",$(this).find("img").attr("src"));
				}
			});		
		}
		
		// Go to next
		$("#g-control-right").click(function() {
			if ($(this).hasClass("active")) {
				var new_left = gilLeft()-105;
				$("#g-image-list").animate({left: new_left},200,"",actCheck);
			}
		});
		
		// Go to previous
		$("#g-control-left").click(function() {
			if ($(this).hasClass("active")) {
				var new_left = gilLeft()+105;
				$("#g-image-list").animate({left: new_left},200,"",actCheck);
			}
		});
		
		// Go to pos
		$("#g-image-list .pos").click(function() {
			if (!$(this).hasClass("active")) {
				if ($(this).next().hasClass("active")) {
					var new_left = gilLeft()+105;
					$("#g-image-list").animate({left: new_left},200,"",actCheck);
				}
				else {
					var new_left = gilLeft()-105;
					$("#g-image-list").animate({left: new_left},200,"",actCheck);
				}
			}
		});
		
		actCheck();
		
	}
	
	if ($("#inner-content .img").length!=0) {
		// Automatic corner setup
		$("#inner-content .img").each(function() {
			$(this).css("width",($(this).find("img").get(0).clientWidth)+"px").css("height",($(this).find("img").get(0).clientHeight)+"px")
			var tags = "<div class='tl'><div class='png'></div></div><div class='tr'><div class='png'></div></div><div class='bl'><div class='png'></div></div><div class='br'><div class='png'></div></div>";
			$(tags).appendTo(this);
		});
	}
	
	if ($("#inner-content table").length!=0) {
		$("#inner-content table tr td:last-child, #inner-content table tr th:last-child").css("border-right","0");
		$("#inner-content table tr:last-child td, #inner-content table tr:last-child th").css("border-bottom","0");
	}
	
	if ($("#page-selector").length!=0) {
		$("#page-selector .prev, #page-selector .next").each(function() {
			if (!$(this).hasClass("active")) $(this).attr("title","");
		});
		$("#page-selector .prev, #page-selector .next").click(function() {
			if (!$(this).hasClass("active")) return false;
		});
		$(".project-list-pos").each(function(){
			if ($(this).find(".project-list-img").length==0) $(this).find(".project-list-desc").css("margin","0");
		});
	}
	if ($("#catalogue-list").length!=0) {
		$(".project-list-pos").each(function(){
			if ($(this).find(".project-list-img").length==0) $(this).find(".project-list-desc").css("margin","0");
		});
	}
	if ($("#catalogue-list-2 .pos").length!=0) {
	/*	var max_catList = 0;
		$("#catalogue-list-2 .pos").each(function(){
			if ($(this).get(0).offsetHeight>$(this).get(0).clientHeight) var this_obj = $(this).get(0).offsetHeight; else var this_obj = $(this).get(0).clientHeight;
var this_obj = $(this).get(0).clientHeight;
			if(this_obj>max_catList)max_catList=this_obj;
		});
		alert(max_catList);
		$("#catalogue-list-2 .pos").css("height",max_catList+"px");*/
		//$(".stul .pos:nth-child(5n+5)").next().addClass("clear");
	}
	
});
