$(document).ready(init);

var currentPreviewsPos = 0;
var previewStepBy = 440;

function init() {
	$(".highlight").hover(hlCatItem, hlCatItem);
	
	$("#series-scroller a.left, #series-scroller a.right").click(function(){
		if ( $(this).hasClass("right") ) {				
			if ((-1 * currentPreviewsPos) < ($("#series-holder").attr('rel') * previewStepBy - 2*previewStepBy)) {
				currentPreviewsPos -= previewStepBy;
			}
		}
		else if ( $(this).hasClass("left") ) {
			if (currentPreviewsPos < 0) {
				currentPreviewsPos += previewStepBy;
			}
		}
		
		$("#series-holder").animate({marginLeft: currentPreviewsPos+"px"}, 500);		
		return false;
	})
	
	// big frames
	var frame_image_count = 0;
	$(".frame-images img").each(function(){
		frame_image_count++;
		var src = $(this).attr("src");
		$(this).attr("src", "/images/frames/big.gif");
		$(this).css({
			'background-image': "url("+src+")"
		});
	});
	$(".frame-images").css('width', (235*frame_image_count));
	// /big frames
	
	$(".pager a").click(function(){
		var page = $(this).attr("rel");
		
		setCurrentPage(page);
	});
	
	if ( location.href.indexOf('#page-') > 0 ) {
		setCurrentPage(location.href.substr(location.href.indexOf('#page-')+6));
		return;
	}
	else
		setCurrentPage();
	
	$("a.show-products").click(function(){
		var products_holder = $("#"+$(this).attr("rel"));
		products_holder.slideDown();
		var buf = location.href.substr(location.href.indexOf('#page-')+6).split('-');
		var product_id = buf[0];
		var current_page = buf[1];
		
		setCurrentPage(product_id+"-"+current_page);
	});
	
	/*if ( location.href.indexOf('#page-') > 0 ) {
		var product_id = location.href.substr(location.href.indexOf('#page-')+6).split('-')[0];
		$("a.show-products[rel='products-"+product_id+"']").click();
	}*/
}

function setCurrentPage( page ) {	
	try {
		if ( !page ) {
			page = $(".page-item:first").attr("id").replace('page-item-','');
		}
			
		var current_page = page;
			
		var container_id = current_page.toString().split('-')[0];
		var current_page = current_page.toString().split('-')[1];
		
		if ( current_page == 'all' ) {
			/*var all_pages_holder = $("#all-pages-holder-"+container_id);
			$(all_pages_holder).html($("#pages-holder-"+container_id+" table").remove());
			loadPageImages(all_pages_holder);
			$("#pages-scroller-"+container_id).css('height',0);
			
			$("#pager-"+container_id+" a").removeClass("current");
			$("#pager-"+container_id+" a[rel='"+page+"']").addClass("current");*/
			var pages_holder = $("#pages-holder-"+container_id);
			pages_holder.data("style", pages_holder.attr("style")).removeAttr("style").css("width","auto");
			$("#pages-scroller-"+container_id).css({"overflow":"auto", "height":"auto"});
			loadPageImages(pages_holder);
			
			$("#pager-"+container_id+" a").removeClass("current");
			$("#pager-"+container_id+" a[rel='"+page+"']").addClass("current");
			return;
		}
		else if ( $("#pages-holder-"+container_id).data("style") ) /*if ( $("#all-pages-holder-"+container_id+" table").length ) {
			$("#pages-holder-"+container_id).html($("#all-pages-holder-"+container_id+" table").remove());
		}*/ {
			var pages_holder = $("#pages-holder-"+container_id);
			pages_holder.attr("style", pages_holder.data("style"));
			$("#pages-scroller-"+container_id).css({"overflow":"hidden", "height":"auto"});
		}
		
		var current_page_holder = $("#page-item-"+container_id+"-"+current_page);
		$("#pages-scroller-"+container_id).animate({height: (20+current_page_holder.height())+'px'}, 500, function(){ /*alert($("#pages-scroller").css('height'))*/ });
		
		$("#pager-"+container_id+" a").removeClass("current");
		$("#pager-"+container_id+" a:eq("+(current_page-1)+")").addClass("current");
		
		var scroll_step_speed = 500;
		//var page_segments = Math.abs(prev_page-current_page);
		//if ( page_segments > 2 ) {
			$("#pages-holder-"+container_id).css({marginLeft: -1*938*(current_page-1) + "px"});
			loadPageImages(current_page_holder);
		/*}
		else {
			$("#pages-holder-"+container_id).animate({
				marginLeft: "-" + 938*(current_page-1) + "px"
			}, page_segments*scroll_step_speed, "linear", function() { alert($("#pages-holder-"+container_id).css('margin-left')) });
		}*/
	}
	catch(e) {
	}
}

function hlCatItem() {
	var hli = $("#"+$(this).attr("rel")).attr("src");
	$("#"+$(this).attr("rel")).attr("src", $("img", $(this)).attr("src"));
	$("img", $(this)).attr("src", hli);
}

function loadPageImages( current_page_holder ) {
	if ( current_page_holder.hasClass("loaded") ) {
		return;
	}
	//alert($("img.icon", current_page_holder).length);
	$("img.icon", current_page_holder).each(function(){
		var img = $(this);
		var buf = new Image();		
		buf.onload = function() {
			img.css('background-image','url("'+buf.src+'")');	
		};
		buf.src = img.attr("rel");
	});
	current_page_holder.addClass("loaded");
}