var $ = jQuery;

$(document).ready(function() {	
	

	
	var tipFade = 400;
	var isAnimated = false;
	var mapDiv = '#map-box';
	
	$('.tip').live("click", function(){
		url = $(this).attr('alt');
    	 window.location=url;
		
	});
	
	$('area').mouseover(function(e){	
			
		data = $(this).attr('rel');	
		href = $(this).attr('href');	
		
		if(href != '/#'){
			clsses = 'islink';
		}else{
			clsses = '';
		}	
		clsses = 'islink';
		if(data){
		var eleHover = 'area';
		$('.tip').remove();
		
		data = data.split('-');

		$(mapDiv).append('<div alt="' + href + '" class="tip ' + clsses + ' tip-' + data[3] + '" style="top:' + data[1] + '; left:' + data[2] + ';"><div id="tip-top"></div><div id="tip-content"></div><div id="tip-bottom"></div></div>');
		
		tip = '#tip-'+data[0];
		tipData = $('#tip-'+data[0]).html();
		
		$('#tip-content').html(tipData);
		$('.tip').stop(true, true).fadeIn(tipFade);
		}
		
	});

	
	$('.control').hover(function(e){
		
		if($(this).has('div').length){
		return;
		}	
		tipContent = $(this).attr("title");
		$(this).removeAttr('title');

		if($(this).attr('id') == 'right'){
			cssLeft = '-4px';
			cssClass = 'tip-left';
		}else{			
			cssLeft = '-150px';
			cssClass = 'tip-right';
		}		
		
		$(this).append('<div class="tip '+ cssClass +'" style="top: 28px; left:' + cssLeft + '"><div id="tip-top"></div><div id="tip-content"></div><div id="tip-bottom"></div></div>');
		$('#tip-content').html(tipContent);
	
		$('.tip').stop(true, true).fadeIn(tipFade);
		
	},  function () {
		$(this).attr("title", tipContent);
		$('.tip').remove();	
	});	
		
	

	$(mapDiv + ' .map-image, body').mouseenter(function(){

			$('.tip').remove();		
	});
	
	
	/* Footer */
	var footerHeight = $("#footer").height();
	$("#roller").toggle( function ()
	{
		footerHeight = $("#footer").height();
		$("#footer").css({height:0}).animate({height: footerHeight}, 1000, function (){
			if( $("#roller").hasClass('plus') ){
				$("#roller").removeClass('plus').addClass('minus');
			}else{
				$("#roller").removeClass('minus').addClass('plus');
			}
		});
	},
	function ()
	{
		footerHeight = $("#footer").height();
	    $("#footer").animate({height: '0px'}, 1000,	function(){ 
	    	$("#footer").css({ display: 'none', height: footerHeight  }); 
			if( $("#roller").hasClass('plus') ){
				$("#roller").removeClass('plus').addClass('minus');
			}else{
				$("#roller").removeClass('minus').addClass('plus');
			}
	    });
	});
	
	
	// new menu
	
	hoverEle = '#nav';
	navEle = '#nav .navi';

	navAnim = false;
	height = $(navEle).height()  + 'px';
	$(navEle).append('<li class="last">&nbsp;</li>');
	
	
	
	// start animation for nav
	$(navEle).stop().css({height:0, display:'block'});
	$("#end").css({display: 'block'});
	$(hoverEle).addClass('addtop');
	$("#menu").addClass('mover');
	$(navEle).animate({height: height}, 800, "easeOutBounce", function(){ 		
		$(navEle).animate({height: 0}, 800, "easeOutBounce", function(){ 
			$("#menu").css({'marginTop': '0'}).removeClass('mover');
			$("#nav").removeClass('addtop');		
			$(this).css({display:'none'});
			$("#end").css({display: 'none'}); 
		});	
		navAnim = false; 
	});
	

	
	$(hoverEle).hover(function() {
		$(navEle).stop();
		$(navEle).css({height:0, display:'block'});
		$("#end").css({display: 'block'});
		$(hoverEle).addClass('addtop');
		$("#menu").addClass('mover');
		$(navEle).animate({height: height}, 800, "easeOutBounce", function(){ 
			
			navAnim = false; 
		});
	}, function(){  			
			$(navEle).stop();
			navAnim = true;
			$(navEle).animate({height: 0}, 800, "easeOutBounce", function(){ 
				$("#menu").css({'marginTop': '0'});
				$("#nav").removeClass('addtop');
				$("#menu").removeClass('mover');
				$(this).css({display:'none'});
				$("#end").css({display: 'none'});
				navAnim = false; 
			});			
	});  
	
	mapHover = '#nava .mapshow';
	mapShow = '#nava .map';
	heightMap = $(mapShow).height() + 'px';

	$(mapHover).hover(function() {
		$(mapShow).stop();
		$(mapShow).css({height:0, display:'block'});
		$(mapShow).animate({height: heightMap}, 200);
	}, function(){  
			$(mapShow).stop();		
			$(mapShow).animate({height: 0}, 200, null, function(){ 	
				$(this).css({display:'none'});	 
			});			
	}); 
	
	var hash = window.location.hash || location.hash;
	



	if(document.location.hash.substring(1) != '')
	{
		ile = 300;		
		rowId = '#' + $('.row a[title = ' +document.location.hash.substring(1) +']').parent().parent().attr('id');
		
		selector = $('.row a[title = ' +document.location.hash.substring(1) +']');
		loadPage(selector);


	}

	
	// news ajax
	$("#box-news a, #subpages a").click(function (){
		
		loadPage(this);

		return false;
	});
	
	
	function loadPage(selector){
		 ile = 300;				
		 href = $(selector).attr('href');
		 row = $(selector).parent().parent();
		 rowId = '#' + $(selector).parent().parent().attr('id');
		var divText = $(rowId + ' .news-text' ); 		
	
		
		if($(divText).hasClass('hidden')){	
			$('.news-text' ).addClass('hidden');
			if($(rowId).attr('loaded')){
				$("#box-news .news-text, #subpages .news-text").slideUp(800);	
				$(divText).slideDown(800, function(){ 
					ile = $(rowId).offset().top;
					$('html').animate({scrollTop: ile},'slow');						
				});	
				$(divText).removeClass('hidden');
				return false;
			}	
			$.ajax( {
				url : href,
				type: 'get',
				cache: false,
				beforeSend: function(data) {
					$(rowId  ).prepend('<div class="loader-inline" />');						 
				},
				success : function(data) {		
					
					$(rowId).attr({loaded: true}); 
					$(rowId + ' .loader-inline' ).remove();	
					$(rowId + ' .news-text' ).html(data);
					//$(divText).removeClass('hidden');	
					$("#box-news .news-text,#subpages .news-text").slideUp(800);	
					$(divText).slideDown(800, function(){ 
						ile = $(rowId).offset().top;
						$('html').animate({scrollTop: ile},'slow');						
					});		
					$(divText).removeClass('hidden');							
				},
				error: function(e) {	
					$('.loader-inline').html('[Wystąplił bład]');
				}
			});				
		}else{	
			$(divText).slideUp(800);
			$(divText).addClass('hidden');
		}
	
	}	

	function setPos(){	
		/* set position fixed for controls */
		//var position = $(".box-txt").position();
		
		var position = $(".box-txt").offset();		
		var conWidth = $(".box-txt").width();
		
		leftPos = position.left - 50 - 10;
		rightPos = position.left +  conWidth + 10;			
		$("#left").css({ margin:0, left:leftPos});
		$("#right").css({ margin:0, left:rightPos});				

		$("#nav").css({left:leftPos + 65});	
		$(".box-txt").css({marginTop: 50});
	}	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$(".control").css({ position: 'absolute'});
	}	
		if($(".box-txt").html()){
		
			setPos();
			$(window).bind('resize',function(){	
				setPos();	
			});
		}		
		function setPosMenu(){
			//var positionMap = $("#map-box").position();	
			var positionMap = $("#map-box").offset();
				$("#nav").css({left:positionMap.left + 10});	
				$("body").addClass('bg-blue');
		}	
		if($("#map-box.index").html()){
			setPosMenu();
			$(window).bind('resize',function(){	
				setPosMenu();	
			});
		}


});    	    

