// JavaScript Document

$(document).ready(function(){
	
	/* homepage banners of sections 
	$(".title_banner").hover(function(){
		$(this).find(".hover").fadeTo("fast", 1);
	}, function(){
		$(this).find(".hover").fadeTo("fast", 0);
	});*/
	
	/* compare button and table */
	$(".compare_btn").click(function(){
		if (!$(this).hasClass("cprGoDisabled")) { 	// if some items are selected
			window.location = appPath + "/" + lang + "/compare.aspx";
		}
	});
	
	$(".compare_table tr:not(.row2)").hover(function(){
		$(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	});
	
	/* gallery news_article */
	$(".newsGalleryThumb a").click(function(){
		$(".newsGalleryThumb a").removeClass("selected");
		$(this).addClass("selected");
		var imgLink = $(this).attr("href");
		var imgAlt = $(this).find("img").attr("alt");
		$(".newsGallery img").replaceWith('<img src="'+imgLink+'" alt="'+imgAlt+'" />');
		$(".newsGallery a").attr("href", imgLink);
		$(".picture_text").text(imgAlt);
		return false;
	});
	
	/* gallery article_zoom */
	$(".gallery a.thumb").click(function(){
		var imgLink = $(this).attr("href");
		var imgAlt = $(this).find("img").attr("alt");
		$(".gallery_box img").replaceWith('<img src="'+imgLink+'" />');
		$(".galleryPictureText").text(imgAlt);
		return false;
	});
	
	/* gallery article_colours */
	$(".colour_icons a").click(function(){
		var imgLink = $(this).attr("href");
		var imgAlt = $(this).find("img").attr("alt");
		$(".colourBigPicture").replaceWith('<img class="colourBigPicture" src="'+imgLink+'" alt="'+imgAlt+'" />');
		$(".colour span").text(imgAlt);
		return false;
	});


	/* gallery article_zoom */
	var bigImageLink = $(".zoomPicture .imageLink").text();
	var middleImage = $(".zoomPicture img").get(0);
	if (middleImage) {
	MojoMagnify.makeMagnifiable(middleImage, bigImageLink);	
	$(".thumbZoom a").click(function(){
		var middleImageLink = $(this).attr("href");
		var imgAlt = $(this).find("img").attr("alt");
		var bigImageLink = $(this).find(".imageLink").text();
		$(".zoomPicture").empty();
		$(".zoomPicture").append('<a href="#" onclick="return false;"><img src="'+middleImageLink+'" alt="'+imgAlt+'" /></a>');
		var middleImage = $(".zoomPicture img").get(0);
		MojoMagnify.makeMagnifiable(middleImage, bigImageLink);
		return false;
	});
	}
	
	/* news */
	var pagination = '<a href="#" class="lastPage" title="Last Page"></a> <a href="#" class="nextPage" title="Next Page"></a> <div class="pageNum"></div> <a href="#" class="prevPage" title="Prev Page"></a> <a href="#" class="firstPage" title="First Page"></a>';
	if($(".newsItem").size()) {
		var itemsOnPage = 5;
		$(".pagination_news").append(pagination);
		var itemsCount = $(".newsItem").size();
		var pageCount = Math.ceil(itemsCount / itemsOnPage);
		var pageNum = 0;
		showNews();
		$(".pagination_news .firstPage").click(function(){
			pageNum = 0;
			showNews();
			return false;
		});
		$(".pagination_news .prevPage").click(function(){
			if (pageNum > 0) {
				pageNum--;
				showNews();
			}
			return false;
		});
		$(".pagination_news .nextPage").click(function(){
			if (pageNum < pageCount-1) {
				pageNum++;
				showNews();
			}
			return false;
		});
		$(".pagination_news .lastPage").click(function(){
			pageNum = pageCount-1;
			showNews();
			return false;
		});
	};
	function showNews() {
		$(".newsItem").each(function(index, element){
			var fromNum = pageNum*itemsOnPage;
			var toNum = fromNum+itemsOnPage-1;
			if (index >= fromNum && index <= toNum ){
				$(element).show();
			} else {
				$(element).hide();
			};
		});
		$(".pagination_news .pageNum").text(pageNum+1+'/'+pageCount);
	};
	
	if($(".shortNewsItem").size()) {
		var itemsOnPageShort = 8;
		$(".pagination_shortnews").append(pagination);
		var itemsCountShort = $(".shortNewsItem").size();
		var pageCountShort = Math.ceil(itemsCountShort / itemsOnPageShort);
		var pageNumShort = 0;
		showShortNews();
		$(".pagination_shortnews .firstPage").click(function(){
			pageNumShort = 0;
			showShortNews();
			return false;
		});
		$(".pagination_shortnews .prevPage").click(function(){
			if (pageNumShort > 0) {
				pageNumShort--;
				showShortNews();
			}
			return false;
		});
		$(".pagination_shortnews .nextPage").click(function(){
			if (pageNumShort < pageCountShort-1) {
				pageNumShort++;
				showShortNews();
			}
			return false;
		});
		$(".pagination_shortnews .lastPage").click(function(){
			pageNumShort = pageCountShort-1;
			showShortNews();
			return false;
		});
	};
	function showShortNews() {
		$(".shortNewsItem").each(function(index, element){
			var fromNumShort = pageNumShort*itemsOnPageShort;
			var toNumShort = fromNumShort+itemsOnPageShort-1;
			if (index >= fromNumShort && index <= toNumShort ){
				$(element).show();
			} else {
				$(element).hide();
			};
		});
		$(".pagination_shortnews .pageNum").text(pageNumShort+1+'/'+pageCountShort);
	};
	
	
});

