$(document).ready(function(){
	//likwidacja outlineow
	$("input[type='submit']").click(function () {
		$(this).blur();
	});
	
	
	function getPath() {
		return location.hash.substr(1);
	}
	
	
	// Podmiana opisu w wyszukiwarce
	var search = $("#search-head");
	if (search.size() > 0) {
		var search_text = search.val();
		
		search
			.focus(function(){
				if ($(this).val() == search_text)
					$(this).val("");
			})
			.blur(function(){
				if ($(this).val() == "")
					$(this).val(search_text);
			});
	}


	function lightbox_map(lat, lon, info_title, info_description) {
		if (lat == undefined || lon == undefined)
			return false;

		if (info_title == undefined || info_description == undefined)
			var ventokienko = "<h3>"+info_title+"</h3><p style='font-size:12px'>"+info_description+"</p>";

		var map = new google.maps.Map(document.getElementById("fancymap"), {
			zoom: 15,
			center: new google.maps.LatLng(lat, lon),
			mapTypeId: google.maps.MapTypeId.ROADMAP
		});
		geocoder = new google.maps.Geocoder();

		var marker = new google.maps.Marker({
			"position": new google.maps.LatLng(lat, lon),
			"map": map,
			"title": info_title
		});

		if (info_title != undefined && info_description != undefined) {}
			var infowindow = new google.maps.InfoWindow({content: ventokienko});
			infowindow.open(map,marker);

			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map,marker);
			});
	}

	$("input[type=file]").filestyle({
		image: "public/images/bg/file-input.png",
		imageheight : 19,
		imagewidth : 87,
		width : 174
	});
	
	$(".light").fancybox({
		'titleShow': false,
		'overlayColor': '#000'
	});
	
	$(".lightmap").fancybox({
		'padding': 20, // optional
		'width': 710, //or whatever you like
		'height': 510,
		'type': 'iframe'
	});
	
	$(".lightmap_js").live("click", function(){
		var a = $(this);
		
		$.fancybox({
			"padding": 20,
			"width": 710,
			"height": 490,
			"content": '<div id="fancymap" style="width:710px; height:461px;"></div>',
			"onComplete": function(event){
				var latlon = a.attr("rel").split("|");
				lightbox_map(latlon[0], latlon[1], latlon[2], latlon[3]);
				return false;
			}
		});
		
		return false;
	});
	
	
	$(".images-list a").fadeTo("fast", .5);
	
	$(".images-list a").hover(function(){
		$(this).fadeTo("fast", 1);
	},function(){
		$(this).fadeTo("fast", .5);
	});
	
	$(".images-list a").click(function(event){
		var div = $("#product-photo");
		var big_picture = $("#product-photo .big");
		var largePath = $(this).attr("href");
		
		div.css({
			"width": big_picture.width(),
			"height": big_picture.height()
		});
		
		big_picture.fadeOut("slow", function() {
			$(this).attr("src", largePath).load(function(){
				$(this).fadeIn("slow", function(){
					div.css({"width": "auto", "height": "auto"});
				});
			});
		});
		return false;
	});	 
	setTimeout(function(){ $(".des").desaturate(); }, 2000);
	
	
	$(".images-list").bind("mouseenter mouseleave", function(event) {
		if (event.type == 'mouseenter')
		{
			//$(".des:not(.color)", this).fadeOut(1000);
			$(".des.color", this).fadeIn(500);
		}
		if (event.type == 'mouseleave')
		{
			//$(".des:not(.color)", this).fadeIn(1000);
			$(".des.color", this).fadeOut(500);
		}
	});

	// Rozwijanie kategorii produktow w lewym menu
	$("a.expand_products_category", "#products-nav").click(function(event){
		event.preventDefault();
		var animation_time = 500;
		
		var t = $(this);
		var category_id = t.attr("rel");
		var action = t.hasClass("on") ? "hide" : "show";
		
		if (action == "show") {
			$(".main-right").load(base_url + lang + "/products/ajax_category/"+ category_id +".html", function(){
				Cufon.refresh();
			});
		}
		
		var products = t.next("ul.sub-products");
		if (action == "show" && products.is(":hidden")) {
			$("ul.sub-products:visible").slideUp(animation_time);
			$(".on", "#products-nav").removeClass("on");
			
			products.slideDown(animation_time, function(){ initializejScrollPane(); });
			t.addClass("on");
			return false;
		}
		else if (action == "hide" && products.is(":visible")) {
			products.slideUp(animation_time, function(){ initializejScrollPane(); });
			t.removeClass("on");
			return false;
		}
		else {
			// Ukrycie pozostalych rozwinietych kategorii
			$("ul.sub-products:visible").slideUp(animation_time);
			$(".on", "#products-nav").removeClass("on");
			
			// Ladowanie produktow Ajax'em
			$.post(base_url + lang + "/products/expand_products.html", { "category_id": category_id },
			function(response){
				var products = $("<ul>").hide().addClass("sub-products");
				
				$("product", response).each(function(){
					var name = $(this).find("name:first").text();
					var link = $(this).find("link:first").text();
					var image = $(this).find("image:first").text();
					
					var li = $("<li>");
					var a = $("<a href=\""+ link +"\" />").appendTo(li);
					$("<span>"+ name +"</span>").appendTo(a);
					
					if (image != "") {
						$('<img src="'+ image +'">').attr("alt", name).appendTo(a);
					}
					
					li.appendTo(products);
				});
				
				// Wstawienie wynikow i rozwiniecie ich
				products.insertAfter(t).slideDown(animation_time, function(){
					initializejScrollPane();
				});
				
				t.addClass("on");
				
				Cufon.refresh();
			},
			"xml");
		}
		
		return false;
	});
});

//rotatorek na glownej
function slideSwitch() {
	var $active = $('.slide.active');
	if ( $active.length == 0 ) $active = $('.slide:last');
	var $next =  $active.next().length ? $active.next()
	: $('.slide:first');
	$active.addClass('last-active');
	$(".last-active").css("visibility","hidden");
	$next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
		$active.css("visibility","visible"); 
	});
}

$(function() {
	setInterval( "slideSwitch()", 5000 );
});
