var h = window.location.hash;
var regexp = /^#!(.*)/gi	
if( regexp.test(h) ) {
	window.location.href =  h.replace(regexp,"$1");
}
	
	Cufon.replace(".cufon",{fontFamily:"klavika",hover:true});
	var bg = 0;
	var bg_images = new Array();
	
	function YatengaInitBg() {
		var ile = 4;
		var loaded = 0;
		for(i=1;i<=ile;i++) {
			bg_images[i] = new Image();
			$(bg_images[i]).attr("src","images/bg/"+i+".jpg").css("opacity",0).prependTo("#bg_img").load(function() {
				$(this).data('yatenga',{width: $(this).width(),height : $(this).height()});
				if(++loaded==ile) {
					YatengaBg();
				}
			});
		}
	}
	
	function YatengaBg() {
		var newbg = parseInt($("#bg").val());
		if(bg!=newbg) {
			if(bg!=0) {
				$(bg_images[bg]).fadeTo(800,0);
			}
			$(bg_images[newbg]).fadeTo(800,1.0);
			bg = newbg;
		}
		YatengaBgSize();
		
	}
	function YatengaHeight() {
		if($("#intro").length) {
			if( $(window).height() < 350) {
				$("html,.height_adj").css("height",350);
			} else {
				$("html,.height_adj").css("height","100%");
			}
		} else {
			var t = 136;
			var b = 72;
			var wh = $(window).height();
			var ch = $("#center").height();
			if(wh < ch+b+t) {
				$("#center").css("top",t);
				$("html,.height_adj").css("height",ch+b+t);
			} else {
				nt = (wh - t - b - ch) / 2 + t;
				$("#center").css("top",nt);
				$("html,.height_adj").css("height","100%");
			}
		}
	}
	
	function YatengaBgSize() {
		YatengaHeight();
		$("#bg_img img").each(function() {
			if( $(this).data('yatenga') ) {
				var w = $(this).data().yatenga.width;
				var h = $(this).data().yatenga.height;
				var ww = $(document).width();
				var wh = $(document).height();
				var nw = ww;
				var nh = wh;
				var nh,th;
				var nt = 0;
				var nl = 0;
								
				tw = Math.floor(w * wh / h) ;
				th = Math.floor(h * ww / w) ;
				 // if calculated width is ok, we set it as target width
				if (tw < ww) {
					nh = th;
				} else {
					nw = tw;
				}
				var nt = (Math.floor( (wh - nh)/2));
				var nl = (Math.floor( (ww - nw)/2));
				
				$(this).css({"width":nw,"height":nh,"top":nt,"left":nl});
			}
		});
	}
	function YatengaScrollInit() {
		$(".scrollable").scrollContent({arrows:false});
		
		if( $(".scrollable_gal").length ) {
			if( $(".scrollable_gal_ref").children().length > 4 ) {
				$(".scrollable_gal_ref").css("overflow","visible");
				
				var h = $(".scrollable_gal").height();
				//console.log(h);
				$(".scrollable_gal_wrap").css("height",h);
				$(".scrollable_gal").scrollContent({arrows:false});
			} else if ($(".scrollable_gal").height() >= 300 )  {
			
			
				$(".scrollable_gal_wrap").css("height",300);
				$(".scrollable_gal").scrollContent({arrows:false});
			}
			YatengaBgSize();
		}
	}
	$(document).ready(function() {
		YatengaInitBg();	
		$("a.galeria").fancybox({
			padding: 1,
			overlayColor: "#000"
		});
	});
	$(window).resize(function() {
		YatengaBgSize();
	});
	$(window).load(function() {
		YatengaScrollInit();
	});
	
	$("a[rel=ajax]").live("click",function() {
		var u = $(this).attr("href");
		window.location.hash = '!'+u;
		$("#center").animate({left: -1*$("#center").width()},"slow",function() {
			$.ajax({
				url: u,
				data: {ajax:1},
				success: function(data) {
					$('#center').html(data);
					Cufon.refresh();
					$("a.galeria").fancybox({
						padding: 1
					});
					YatengaScrollInit();
					YatengaHeight();
					$("#center").animate({left: 0},"slow",function() {
						setTimeout(function() {YatengaBg();},70);	
					});
				}
			});
		});
		return false;
	});
	$("#menu_left").live("mouseenter",function() {
		$(this).addClass("hover");
		var w = 205;
		if( $(".menu_left_width").length) {
			w = $(".menu_left_width").outerWidth() - 32;
		}
		$("#menu_left_hover").css("width",w);
		$(this).stop().animate({width:(32+w)},"slow");
	});
	$("#menu_left").live("mouseleave",function() {
		$(this).removeClass("hover");
		$(this).stop().animate({width:(32)},"slow");
	});
	
	$("#idea_prezentacja a").live("click",function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: '',
				'width'		: 448,
				'height'		: 252,
				'href'			: 'flash/player.swf?v=YATENGA_film_promocyjny_smallest.flv&img=flash/video.jpg',
				'type'			: 'swf',
				'swf'			: {
					 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

		return false;
	});
