$(function() {
	var URI = location.pathname.split(/\//);


	$.fn.load_by_hash = function() {
		var id = window.location.hash.replace("#", "");
		var IDs = id.split(/\//);	
		
		if(IDs[1]) {
			if($("#detailsLink a").attr("parent") == IDs[1]) {
				var hash = $("#detailsLink a").attr("href").replace("#", "");
				var hIDs = hash.split(/\//);
				var hid = hIDs[1];	
				
				$("#detailsLink a").attr("parent", hid);
				
			}		
		
		
			$("#detailsLink a").attr("href", "#/" + IDs[1] + "/");
			

			
			$("#detailsLink a").trigger("click");
			
			if(IDs[1] == 35) {
				$("#detailsLink span").removeClass("italic");
			}
		}
	}

	
	
	
	function createBottomBlockMargin() {
		var margin = parseInt($("#content").height()) - parseInt($(".blockContent").height()) - 160;
		
		if($.browser.opera || $.browser.msie) {
			margin = margin - 50;
		}	

		if($.browser.webkit) {
			margin = margin - 50;
		}
		
		return margin;
	}


	function createBottomBlockLinksMargin() {
		var margin = parseInt($("#content").height()) - $("#blockLinks").height() - 120;
		if($.browser.opera || $.browser.msie) {
			margin = margin - 50;
		}	
		if($.browser.webkit) {
			margin = margin - 50;
		}		
		return margin;		
	}	
	
	if($(".bottomBlock").length > 0) {
		
		var contentHeight = $("#content").height();
		var blockWrapperHeight = $(".blockWrapper").height();
			
		
		$("#detailsLink").css("visibility", "hidden");

		

		var margin = createBottomBlockMargin();
	  	$(".bottomBlock").animate({
	    	"margin-top": margin
	  	}, 800, function() {
	  		$("#blockLinks").css("margin-top", createBottomBlockLinksMargin());	
	  		
			$(".bottomBlock").css("visibility", "visible");
			$(".bottomLinks").css("visibility", "visible");	 
			$("#detailsLink").css("visibility", "visible"); 
	  	});	

	}
	
	$("#orderBlock a").attr("href", "/order/");


	$('ul#innerGallery').imgbubbles({factor:1.30}) ;
    $('ul#gallery').imgbubbles({factor:1.75}) ;
    
    
	function changeBlockContent(id) {
		$.ajax({
		    url: "/sections/content/" + id + "/",             
		    success: function (data, textStatus) { 		        

		    	$(".blockContent").fadeIn().html(data);
		    	$('ul#gallery').imgbubbles({factor:1.75}) ;
				$("ul#gallery li a").fancybox({
					'transitionIn'			:	'fade',
					'transitionOut'			:	'fade',
					'speedIn'				:	600, 
					'speedOut'				:	200, 
					'overlayShow'			:	true,
					'padding'				:	1,
					'margin'				:	2,
					'hideOnContentClick'	:	true,
					'titlePosition'			:	'over',
					'showCloseButton'		:	false
								
				});
		    	
				if($(".middleWide").length > 0) {
					animateMiddle(".middleWide")
					
					
					var margin = createBottomBlockMargin();
				  	$(".bottomBlock").animate({
				    	"margin-top": margin
				  	}, 800, function() {
				    	// Animation complete.
				  	});						
				} else {
					animateMiddle(".middle")
				}				
		    }
		});		
	}

	function animateMiddle(middleBlock) {
	  	$(middleBlock).animate({
	    	height: $(".blockContent").height()
	  	}, 800, function() {
	    	// Animation complete.
	  	});
	}

	$("#loading").hide();
	$("#loading").bind("ajaxSend", function(){	
		if($(".middleWide").length > 0) {
			$("#loading").css("top", $(".middleWide").height() / 2 - 11);	
		} else {
			$("#loading").css("top", $(".middle").height() / 2 - 11);
		}
	    $(this).show();
	}).bind("ajaxComplete", function(){
	 	$(this).hide();
	});




	$("#detailsLink a").live("click", function(e) {
		
		var hash = $(this).attr("href").replace("#", "");
		var IDs = hash.split(/\//);
		var id = IDs[1];
		
		
		var parent = $(this).attr("parent");
		e.preventDefault();
		
		if($(".middleWide").length > 0) {
			$(".middleWide").height($(".blockContent").height());
		} else {
			$(".middle").height($(".blockContent").height());
		}
		
		$(".blockContent").hide();
		$("#loading").show();
		
		window.location.hash = "#/" + id + "/";
		changeBlockContent(id);
		$("#detailsLink span").toggleClass("italic");
		
		
		$(this).attr("href", "#/" + parent + "/");
		$(this).attr("parent", id);
	});
	

	
	$("#newsPage li a, .gallery-photo a, .feedBack_image a, #feedbacks .feedback_header a, #innerGallery li a, ul#gallery li a").fancybox({
		'transitionIn'			:	'fade',
		'transitionOut'			:	'fade',
		'speedIn'				:	600, 
		'speedOut'				:	200, 
		'overlayShow'			:	true,
		'padding'				:	1,
		'margin'				:	2,
		'hideOnContentClick'	:	true,
		'titlePosition'			:	'over',
		'showCloseButton'		:	false
					
	});


    
    $('#feedbacks ul li div').each(function(){
        $(this).html($(this).find('p:eq(0)').html());  
        
    });
    
    
	$(document).load_by_hash();
    		
});
