Common = {
		
	init: function() {
	
		$("a").focus(function() {
			$(this).blur();
		});
	}
}

InputHiddenValues = {
		
	init: function() {
		
		$("input.hiddenValue").each(function(i) {
			var value = $(this).val();
			$(this).attr("hiddenValue", value);
		});
		
		$("input.hiddenValue").focus(function() {
			if ($(this).val() == $(this).attr("hiddenValue"))
				$(this).val("");
		});
		
		$("input.hiddenValue").blur(function() {
			if ($(this).val() == "")
				$(this).val($(this).attr("hiddenValue"));
		});
		
		$("textarea.hiddenValue").each(function(i) {
			var value = $(this).text();
			$(this).attr("hiddenValue", value);
		});
		
		$("textarea.hiddenValue").focus(function() {
			if ($(this).text() == $(this).attr("hiddenValue"))
				$(this).text("");
		});
		
		$("textarea.hiddenValue").blur(function() {
			if ($(this).text() == "")
				$(this).text($(this).attr("hiddenValue"));
		});
	}
};

Slider = {

	sliderSection: "",
	currentPage: 0,
	perPage: 400,

	init: function(sliderSection, currentPage) {
		if ($("#itemsWrapper").length == 0)
			return false;
		Slider.currentPage = currentPage;
		Slider.sliderSection = sliderSection;
		Slider.perPage = 400;
		containerWidth = $("#listing").width()/4;
		itemsWidth = $("#itemsWrapper").width();
		pages = Math.floor(itemsWidth / Slider.perPage);
		Slider.setPage();

		$(".navLeft").click(function() {
			if (Slider.currentPage > 0) {
				Slider.currentPage--;
				$("#itemsWrapper").animate({"left": "+=" + Slider.perPage + "px"}, "", "", function() {Slider.evaluatePosition();});				
			}
		});
		
		$(".navRight").click(function() {
			if (Slider.currentPage < pages - 1) {
				Slider.currentPage++;
				$("#itemsWrapper").animate({"left": "-=" + Slider.perPage + "px"}, "", "", function() {Slider.evaluatePosition();});
			}
		});
	},
	
	evaluatePosition: function() {
		containerWidth = $("#listing").width();
		
		//left
		if ($("#itemsWrapper").position().left < 0) $(".navLeft").removeClass("hidden");
		else $(".navLeft").addClass("hidden");
		
		//right
		if ($("#itemsWrapper").position().left + $("#itemsWrapper").width() < containerWidth) $(".navRight").addClass("hidden");
		else $(".navRight").removeClass("hidden");

		// save position
		$.get("/scripts/setSliderPage.php?section=" + Slider.sliderSection + "&page=" + Slider.currentPage + "&rand=" + Math.random());
		
		return true;
	},
	
	setPage: function() {
		$("#itemsWrapper").css("left", "-" + Slider.currentPage*Slider.perPage + "px");
		Slider.evaluatePosition();
	}
};

Lamps = {
	
	init: function() {
		$(".productOptionImages a").click(function() {
			title = $(this).find("img").attr("title");
			rel = $(this).attr("rel");
			questionLinkRel = $("#questionLink").attr("rel");
			/**$("#productView div.image img").attr("src", rel).attr("title", title).attr("alt", title);**/
			$("#productView div.image img").attr({"src": rel, "title": title, "alt": title});
			$("#questionLink").attr("href", questionLinkRel + "?subject=Zapytanie o produkt " + title);
		});
	}
}


$(document).ready(function() {
	Common.init();
	InputHiddenValues.init();
	Lamps.init();
	$('.identity').innerfade({speed: 'slow', timeout: 5000, type: 'random', containerheight: '198px'});
});