$(document).ready(function(){
	var path = document.location.pathname;		
	if ( path.charAt(path.length-1) != '/' ) path = path+'/';
	var epos = document.location.pathname.indexOf('/',1) == -1 ? document.location.pathname.length : document.location.pathname.indexOf('/',1)-1;
	var section = document.location.pathname.substr(1, epos );
	
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
	// FONT -------------------------------------------------------------------
	
	
	
	var imgBorder = '/images/content/img.png';
	$("img.img-js").each(function(){
		var src = $(this).attr('src');
		$(this).attr("src", imgBorder).css({"background-image" : "url(" + src + ")"});
	}
	);
	
	var iconBorder = '/images/content/icon.png';
	$("img.icon").each(function(){
		var src = $(this).attr('src');
		$(this).attr("src", iconBorder).css({"background-image" : "url(" + src + ")"});
	}
	);
	
	$("#scroll-block").scrollTop(0);
	$("#up-scroll").click(function(){
		$("#scroll-block").stop();
		$("#scroll-block").animate({scrollTop : '-=75px'}, 400);
		return false;
	});
	$("#down-scroll").click(function(){
		$("#scroll-block").stop();
		$("#scroll-block").animate({scrollTop : '+=75px'}, 400);
		return false;
	});
	$("#scroll-block a").click(function(){
		$("#big-img").attr("href", $(this).attr("href"));
		$("#big-img img").css("background-image", "url(/files/image/products/big_" + $(this).attr("rel") + ")");
		$("#scroll-block img.active").attr("src", "/images/content/small_frame.png").removeClass("active");
		$(this).find("img").attr("src", "/images/content/active_frame.png").addClass("active");
		var num = $("#scroll-block a").index(this);
		if (num > 0 && num < ($("#scroll-block a").length -1)) {
			//alert(1);
			$("#scroll-block").stop();
			$("#scroll-block").animate({scrollTop : (75*(num-1)) + 'px'}, 400);
		}
		return false;
	});
	
	
	
	// BANNERS ----------------------------------------------------------------------
	$("#new-banners a.item1").hover(function(){
		$("#new-banners .sep1 span").addClass("right");
	}, function() {
		$("#new-banners .sep1 span").removeClass("right");
	});
	$("#new-banners a.item2").hover(function(){
		$("#new-banners .sep1 span").addClass("left");
		$("#new-banners .sep2 span").addClass("right");
	}, function() {
		$("#new-banners .sep1 span").removeClass("left");
		$("#new-banners .sep2 span").removeClass("right");
	});
	$("#new-banners a.item3").hover(function(){
		$("#new-banners .sep2 span").addClass("left");
		$("#new-banners .sep3 span").addClass("right");
	}, function() {
		$("#new-banners .sep2 span").removeClass("left");
		$("#new-banners .sep3 span").removeClass("right");
	});
	$("#new-banners a.item4").hover(function(){
		$("#new-banners .sep3 span").addClass("left");
	}, function() {
		$("#new-banners .sep3 span").removeClass("left");
	});
	
	
	// PARTNERS ---------------------------------------------------------------------
	function activate_partners(p) {
		$("#block-" + $("#partners-buttons a.active").attr("rel")).hide();
		$("#block-" + p).show();
		$("#partners-buttons a").removeClass("active");
		$("#partners-buttons a[rel='" + p + "']").addClass("active");
	}
	activate_partners("p1");
	$("#partners-buttons a").click(function(){
		activate_partners($(this).attr("rel"));
		return false;
	});
	$(".scroll-line").scrollLeft(0);
	$(".next-part").click(function() {
		var scroll = parseInt( $(this).attr("title") ) * 311;
		var scroll_width = $(".scroll-width-" + $(this).attr("rel")).width() - 311;
		//console.log (scroll + " " + scroll_width)
		$("#scroll-" + $(this).attr("rel")).stop();
		if (scroll < scroll_width) {
			$("#scroll-" + $(this).attr("rel")).animate({
				scrollLeft: scroll + 'px'
			}, 400);
			$(this).attr("title", parseInt( $(this).attr("title") ) + 1);
		}
		else {
			$("#scroll-" + $(this).attr("rel")).animate({
				scrollLeft: '0px'
			}, 400);
			$(this).attr("title", 1);
		}
		return false;
	});
	
	
	
	// PROD --------------------------------------------------------------------------
	var prod_height = 221;
	var prod_total = $("#tmp-prod div.cat-block").length;
	var prod_width = $("#prod-width").width();
	var count_cols = Math.floor((prod_width + 41) / 238);
	var count_rows = Math.ceil(prod_total / count_cols);
	var count_page = Math.ceil(count_rows / 4);
	$("#frame-prod").width(count_cols * 238 - 41);
	$("#frame-scroll-prod").width(count_page * count_cols * 238 + 1);
	if (count_page == 1) {
		$("#frame-prod").height(count_rows * prod_height - 41);
		$("#frame-scroll-prod").height(count_rows * prod_height);
	}
	else {
		$("#frame-prod").height(4 * prod_height - 41);
		$("#frame-scroll-prod").height(4 * prod_height);
	}
	
	for (var i = 0; i < count_page; i++) {
		$("#frame-scroll-prod").append("<div class='page_prod' id='page-prod-" + i + "'></div>");
		if (count_page == 1) {
			var j_count = prod_total;
		}
		else {
			var j_count = 4 * count_cols;
		}
		for (var j = 0; j < j_count; j++) {
			$("#page-prod-" + i).append($("#tmp-prod div.cat-block:eq(0)"));
		//$(".page_prod").css("width", count_cols*238);
		}
		$("#page-prod-" + i).width(count_cols * 238);
		
	}
	$("#frame-prod").fadeIn(1000);
	if (count_page > 1) {
		for (var i = 1; i <= count_page; i++) {
			$("#pager-prod").append("<a href='#page" + i + "' " + ((i == 1) ? "class='active'" : "") + " rel='" + i + "'>" + i + "</a>");
		}
		$("#pager-prod").show();
		$("#prod-width").css("min-height", ($("#left").height() - 70) + "px");
	}
	else {
		$("#prod-width").css("min-height", ($("#left").height() - 40) + "px");
	}
	//alert($("#tmp-prod div.cat-block").length);
	$("#pager-prod a").click(function(){
		var scroll_width = count_cols * 238 * (parseInt($(this).attr("rel")) - 1);
		$("#pager-prod a.active").removeClass("active");
		$(this).addClass("active");
		$("#frame-prod").animate({
			scrollLeft: scroll_width + 'px'
		}, 400);
		$("html").animate({
			scrollTop: '250px'
		}, 400);
	});

	
	// GALLERY ------------------------------------------------
	var gheight = 0;
	var gcount = $("#tmp-gallery .gallery-block").length;
	$("#tmp-gallery .gallery-block").each(function() {
		//alert($(this).height());
		if($(this).height() > gheight)
			gheight = $(this).height();
	});
	$("#tmp-gallery").hide();

	var gcols = Math.floor( ($("#gallery-frame").width() + 15)/231 );
	var gpage = Math.ceil( gcount/(gcols*2) );
	
	$("#gallery-frame").height(gheight*2 + 18);
	$("#gallery-frame").width(gcols * 231 - 15);
	$("#gallery-scroll-frame").height(gheight*2 + 18);
	$("#gallery-scroll-frame").width( gpage * gcols * 231 + 15);
	
	for (var i = 0; i < gpage; i++) {
		$("#gallery-scroll-frame").append("<div class='page_gal' id='page-gal-" + i +"'></div>");
		
		for (var j = 0; j < 2*gcols; j++) {
			$("#page-gal-" + i).append($("#tmp-gallery div.gallery-block:eq(0)"));
		}
		$("#page-gal-" + i).width(gcols*231);
		$("#page-gal-" + i).height(gheight*2);
	}
	$("#gallery-frame .gallery-block").height(gheight);
	$("#gallery-frame").fadeIn(1000);
	if(gpage > 1) {
		for(var i = 1; i <= gpage; i++) {
			$("#pager-gal").append("<a href='#page" + i +"' " + ((i == 1) ? "class='active'" : "") + " rel='" + i +"'>" + i +"</a>");
		}
		$("#pager-gal").show();
	}
	$("#pager-gal a").click(function() {
		var scroll_width = gcols*231 * (parseInt($(this).attr("rel")) - 1);
		$("#pager-gal a.active").removeClass("active");
		$(this).addClass("active");
		$("#gallery-frame").animate({scrollLeft : scroll_width + 'px'}, 400);
		$("html").animate({scrollTop : '250px'}, 400);
	});
	
});