var apis = [];
$(document).ready(function() {
	var $arPageSize = new Array();
	var $footerHeight;
	var $headerHeight;
	var $containerHeight;
	
	init();
	
	scaleListener();

	$(window).resize(function() {
		scaleListener();
	});
	
	//	http://jscrollpane.kelvinluck.com/index.html#usage
	$('.scroll_block').each(function() {
		apis.push($(this).jScrollPane({
			verticalDragMinHeight: 40,
			verticalDragMaxHeight: 40, 
			horizontalBar: 0, 
			horizontalTrack: 0, 
			horizontalTrackWidth: 0, 
			horizontalDragWidth: 0, 
			animateScroll: true
			//autoReinitialise: true
		}).data().jsp);
	});
});

function scaleListener() {
	$arPageSize = getPageSize();
	$footerHeight = $(".footer").height();
	$headerHeight = $(".header").height();
	
	
	if($arPageSize[3] - $footerHeight - $headerHeight >= 778) {
		$containerHeight = 778;
	} else if($arPageSize[3] - $footerHeight - $headerHeight <= 600) {
		$containerHeight = 600;
	} else {
		$containerHeight = $arPageSize[3] - $footerHeight - $headerHeight;
	}
	
	$(".container").height($containerHeight);
	$(".vertical_all").each(function() {
		$(this).height($(this).parent().height() - 20);
		$(".content_container_inner").each(function() {
			var $contentTopBlockHeight = 0;
			$(this).children().not(".content").each(function() {
				$contentTopBlockHeight += $(this).height();
				$contentTopBlockHeight += parseInt($(this).css("padding-top"));
				$contentTopBlockHeight += parseInt($(this).css("padding-bottom"));
			});
			$(this).find(".content").height($(".content_container_inner").height() - 30 - $contentTopBlockHeight);
		});
	});
	$(".scroll_left, .scroll_right").each(function() {
		$(this).css({
			"top" : ($(this).parent().height() / 2 - $(this).height() / 2)+"px"
		});
	});	
	$(".scale_background_image").height($containerHeight);
	
	$(".vertical_center").each(function() {
		$(this).css({
			"margin-top":(($(this).parent().height() - $(this).height()) / 2)+"px"
		})
	});
	
	// get koef
	var $k = $containerHeight / 778;
	$(".collection_grid").height($containerHeight);

	var $collectionGridWidth = 0;
	$(".collection_grid div.collection_block").each(function() {	
		$(this).width($(this).data("width") * $k);
		$(this).children(".item").each(function() {
			$(this).width($(this).data("width") * $k);
			$(this).height($(this).data("height") * $k);
			
			$(this).find("img").width($(this).width() + 1);
			$(this).find("img").height($(this).height() + 1);
			
			$(this).css({
				top : $(this).data("top") * $k, 
				left : $(this).data("left") * $k
			});
		});
		
		var $trendsAdd = 0;
		var $newCollectionBlockWidth = 0;
		var $mod = 0;
		$(this).children(".trends_item").each(function() {
			$(this).find("img").height($(this).height());
			var $koef =  $(this).height() / parseInt($(this).find("img").attr("height"));
			var $imgWidth = parseInt($(this).find("img").attr("width")) * $koef;
			$(this).find("img").width($imgWidth);
			
			$(this).width(Math.floor($imgWidth));
			$(this).height($(this).data("height") * $k);
			
			$newCollectionBlockWidth += $(this).width() + 1;
			$mod += $imgWidth - Math.floor($imgWidth);
		});
		$trendsAdd = 18;
		if($newCollectionBlockWidth > 0) {
			$(this).width($newCollectionBlockWidth);
		}
		
		$collectionGridWidth += $(this).width() + 1;
	});
	$(".collection_grid").css({left:-$(".collection_grid div.collection_block:first").width()});
	
	$(".ll_content").each(function() {
		var $contentTopBlockHeight = 0;
		$(this).children().not(".content").each(function() {
			$contentTopBlockHeight += $(this).height();
			$contentTopBlockHeight += parseInt($(this).css("padding-top"));
			$contentTopBlockHeight += parseInt($(this).css("padding-bottom"));
		});
		$(this).find(".content").height($(".ll_content").height() - 50 - $contentTopBlockHeight);
	});
	/*$(".collection_grid").width($collectionGridWidth).css({
		"*width":($collectionGridWidth-3)+"px"
	});*/
	
	$('.scroll_block').each(function() {
		apis.push($(this).jScrollPane({
			verticalDragMinHeight: 40,
			verticalDragMaxHeight: 40, 
			horizontalBar: 0, 
			horizontalTrack: 0, 
			horizontalTrackWidth: 0, 
			horizontalDragWidth: 0, 
			animateScroll: true
		}).data().jsp);
	});
	
	$.each(
		apis,
		function(i) {
			this.reinitialise();
		}
	);
	
	$(".scroll_block").find("img").each(function() {
		$(this).load(function() {
			$.each(
				apis,
				function(i) {
					this.reinitialise();
				}
			);
		});
	});
	
	
//	$apiJScrollPane.reinitialise();
	
	$(".home_grid").children(".item").each(function() {
			$(this).width($(this).data("width") * $k);
			$(this).height($(this).data("height") * $k);
			
			$(this).find("img").width($(this).width());
			$(this).find("img").height($(this).height());
			$(this).css({
				top : $(this).data("top") * $k, 
				"right" : $(this).data("right") * $k + "px"
			});
	});
	$(".home_grid .home_01").css({
		width : $(".home_grid").width() - $(".home_grid .home_04").width() - 5, 
		height : $(".home_grid").height()
	});
	$(".home_grid .home_01 img").css({
		"width" : "auto", 
		height : $(".home_grid").height()
	});
	
	moveDetailCollectionItem();
	
	$(".shops_gallery img").each(function() {
		var $koef = $(this).parent().height() / $(this).height();
		$(this)
			.height($(this).parent().height())
			.width($(this).width() * $koef)
			.css({
				"margin-left":($(this).parent().width() / 2 - $(this).width() / 2) + "px"
			});
	});
}

function init() {
	if($(".collection_grid div.collection_block").length == 1) {
		$(".collection_grid div.collection_block").clone().appendTo($(".collection_grid"));
	}
	$(".collection_grid div.collection_block:last").clone().prependTo($(".collection_grid"));
	$(".collection_grid div.collection_block").each(function() {
		//$(this).width($width);
		$(this).data("width", $(this).width());
		$(this).children(".item").each(function() {
			$(this).data("width", $(this).width());
			$(this).data("height", $(this).height());
			$(this).find("img").data("width", $(this).width());
			$(this).find("img").data("height", $(this).height());
			$(this).data("top", $(this).position().top);
			$(this).data("left",$(this).position().left);
		});

		$(this).children(".trends_item img").each(function() {
			$(this).load(function() {
				$(this).parents(".trends_item").data("width", $(this).width());
				$(this).parents(".trends_item").data("height", $(this).height());
				$(this).data("width", $(this).width());
				$(this).data("height", $(this).height());
			});
		});
		if($(this).parent().hasClass("trends")) {
			var $width = 0;
			$(this).children(".trends_item").each(function() {
				$width += Math.ceil($(this).width()) + 4;
			});
			$(this).width($width);
			$(this).data("width", $(this).width());
		}
	});
	$(".collection_grid").css({left:-$(".collection_grid div.collection_block:first").width()});
	$(".scroll_left, .scroll_right, .shops_scroll_left, .shops_scroll_right").each(function() {
		$(this).css({
			"top" : ($(this).parent().height() / 2 - $(this).height() / 2)+"px"
		});
	});
	
	$(".content_container_inner").each(function() {
		var $contentTopBlockHeight = 0;
		$(this).children().not(".content").each(function() {
			$contentTopBlockHeight += $(this).height();
			$contentTopBlockHeight += parseInt($(this).css("padding-top"));
			$contentTopBlockHeight += parseInt($(this).css("padding-bottom"));
		});
		$(this).find(".content").height($(".content_container_inner").height() - 30 - $contentTopBlockHeight);
	});
	
	$(".ll_content").each(function() {
		var $contentTopBlockHeight = 0;
		$(this).children().not(".content").each(function() {
			$contentTopBlockHeight += $(this).height();
			$contentTopBlockHeight += parseInt($(this).css("padding-top"));
			$contentTopBlockHeight += parseInt($(this).css("padding-bottom"));
		});
		$(this).find(".content").height($(".ll_content").height() - 50 - $contentTopBlockHeight);
	});
	
	$(".home_grid").children(".item").each(function() {
		$(this).data("width", $(this).width());
		$(this).data("height", $(this).height());
		$(this).find("img").data("width", $(this).width());
		$(this).find("img").data("height", $(this).height());
		$(this).data("top", $(this).position().top);
		$(this).data("right",parseInt($(this).css("right")));
	});
}

function loadImage(imageSrc) {
	var newHtml;
	$(".container").append('<img src="/i/loading.gif" class="loading" />');
	$("img.loading").css({
		"margin-top":($(".container").height() / 2 - $("img.loading").height() / 2)+"px"
	});
	myImage = new Image();
	myImage.src = imageSrc;
	myImage.onload = function() {
		newhtml = '<img src="'+myImage.src+'" class="scale_background_image" />';
		$("img.loading").replaceWith(newhtml);
		var timeout;
		timeout = setTimeout(function(){
			scaleListener();
		}, 100);
	};
}

function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};
