var hotcms_lang = new Object();

function easy_printf (str, args) {
	if( str==undefined ) return '';
	else if( args==undefined ) return str;
	else{
		var c=1;
		for(a in args){
			var reg = new RegExp('\\%'+c+'\\$s');
			str = str.replace(reg, args[a]);
			c++;
		}
		return str;
	}
}

$(document).ready(function(){

	/*
	$('.menu ul.p1 li').hover(
		function () {
			$(this).find('ul.p2').stop(true, true).slideDown(250);
			$(this).addClass('active');
		},
		function () {
			$(this).find('ul.p2').delay(250).stop(true, true).fadeOut(100);
			$(this).removeClass('active');
		});
	 */
	
	//pobieranie tlumaczen dla js
	$.ajax({
		async : false,
		url: $("base").attr("href")+'tools/js_translate.html',
		global: false,
		type: "POST",
		dataType: "json",
		success: function(data){
			for(i in data)
			{
				hotcms_lang[i] = data[i];
			}
		}
	});
	
	$(window).hashchange(function(){
		onHashChange();
	});
	if(location.hash!=""){
		onHashChange();
	}
	
	pageReady();
});

function pageReady(){

	// cufon
	Cufon.replace('h1, ul#nawigacja a.startNav span.navName');
	//$('#asd3').mbFlipText(true);
	//setTimeout(,3000);
	
	//wczytanie fancybox
	$('.userText a').each(function() {
		var url = $(this).attr("href");
		if(url.match(/.+\.(jpg|jpeg|gif|png)/)) $(this).addClass("fancybox").attr('rel', 'gal');;
	});
	$('.fancybox, .fancy').fancybox({
		'overlayShow'	:	true,
		'titleShow'		:	false,
		'overlayColor'	:	'#000',
		'overlayOpacity'		:	0.9,
		'hideOnOverlayClick'	:	false
	});

	//sprawdzenie wszystkich linkow i jesli sa do wlasnej strony to dodanie hash'a
	var reg_inside_link = new RegExp('^('+$("base").attr("href")+')?([a-z]{2}/|.*\.html)?$');
	$.each($("a"), function() {
		if( reg_inside_link.test($(this).attr("href")) ){
			$(this).click(function() {
				var a = $(this).attr("href");
				var reg_replace = new RegExp('^('+$("base").attr("href")+')?([a-z]{2}/)?(.*\.html)?');
				var a_lang = a.replace(reg_replace, '$2'); //lang with /
				var a_contr = a.replace(reg_replace, '$3');
				a = $("base").attr("href") + ( a_lang=='' ? $("head").attr("lang")+'/' : a_lang ) + '#/' + a_contr;
				if(window.location.href!=a) {
					window.location.href = a;
					
				}
				return false;
			});
		}
	});
	
	opis_scroll();
}

function onHashChange(){

	var locationHash = window.location.hash.replace('#/', '');
	//zmienna mainPage jest deklarowana w top.php
	if( locationHash=='' && mainPage===false ) {
		restartNavigation();
		mainPage = true;
	}
	else if(locationHash!=''){
		$navToOpen = false;
		var linkHashReg = new RegExp('^('+$("base").attr("href")+')?([a-z]{2}/)?(.*)');
		
		$('.majorNav').each(function(){
			var linkHash = $(this).attr('href').replace(linkHashReg,'$3');
			if(linkHash==locationHash){
				$navToOpen = $(this);
			}
		});
		
		if($navToOpen===false){
			
			var reg_aktualnosci = new RegExp('(aktualnosci(/[0-9]+)?.html)|(aktualnosc/.*\.html)');
			var reg_galerie = new RegExp('(galeria(/[0-9]+)?.html)|(galeria/zobacz/.*\.html)');
			
			$('.majorNav').each(function(){
				
				var link = $(this).attr('href');
				var linkHash = link.indexOf("#/")==-1 
					? $(this).attr('href').replace(linkHashReg,'$3') 
					: link.substr(link.indexOf("#/")+2);
				
					/*
				alert( 
					'link nawigacji ='+linkHash
					+'; location strony ='+locationHash
					+'||| link aktualnosc ='+reg_aktualnosc.test(linkHash)
					+'; location aktualnosci ='+reg_aktualnosci.test(locationHash)
					+'||| link aktualnosci ='+reg_aktualnosci.test(linkHash)
					+'; location aktualnosc ='+reg_aktualnosc.test(locationHash)
					+'||| link galeria ='+reg_galeria.test(linkHash)
					+'; location galerie ='+reg_galerie.test(locationHash)
					+'||| link galerie ='+reg_galerie.test(linkHash)
					+'; location galeria ='+reg_galeria.test(locationHash)
				);*/
				
				var result = (reg_aktualnosci.test(linkHash) && reg_aktualnosci.test(locationHash) )
						|| (reg_galerie.test(linkHash) && reg_galerie.test(locationHash) );
			
				if( result ){
					$navToOpen = $(this);
				}
			});
		}
	
		$navToOpen = $navToOpen!==false ? $navToOpen : $('.majorNav:eq(2)');
		if(mainPage) startingNavigation($navToOpen.prev());
		else showContent($navToOpen, false);
		ajaxController(location.href);
		mainPage = false;
	
	}
}

function ajaxController(url) {
	url = url.replace($("base").attr("href"), ''); //wstepne usuniecie domeny
	url = /^[a-z]{2}\/(.*\.html)?/.test(url) ? url : $("head").attr("lang")+url; //sprawdzenie czy url posiada jezyk
	url = $("base").attr("href")+url;
	url = url.replace('#/', '');
	
	setTimeout( function(){
		$.ajax({
			url: url,
			type: "POST",
			dataType: "json",
			success: function(data) {
				if(data) {
					//ukrywanie obiektów i ładowanie do nich nowego HTML
					$('#content').fadeOut(500, function(){
						$('#content').html(data.content);
						$('#content').fadeIn(500, function(){
							pageReady();
						});
					});
					var metaTitleArgs = new Array( $("base").attr("href"), data.title==undefined||data.title.length==0 ? '' : data.title+' - ');
					document.title = easy_printf(hotcms_lang['metaTitle'], metaTitleArgs);
				}
				else{
					ajaxMsg(hotcms_lang['ajaxPageLoadingError']);
					setTimeout("window.location.href='"+url+"';", 3000);
				}
			},
			error: function() {
				ajaxMsg(hotcms_lang['ajaxPageLoadingError']);
				setTimeout("window.location.href='"+url+"';", 3000);
			}
		});
	}, 400);
}

function ajaxMsg(str, delay) {
	
	setTimeout(function() {
		$.fancybox('<div id="ajaxMsg">'+str+'</div>');
	}, typeof(delay) !== "undefined" ? 0 : 250);
	
}

function startingNavigation(wsk){

	//ustawienie id dla wlasciwego kontentu, aby funkcja ajaxControler wywolana na hashChange miala gdzie wstawic tresc
	$newCont = $(wsk).parent().next().children('.contentText');
	$newCont.attr('id','content');

	$('a.startNav').hover(function() {
		$(this).css({'background-position': 'top left'});
	}, function() {
		$(this).css({'background-position': 'top left'});
	});
	
	$('.navBox').each(function(){

		$(this).children('.startNav').find('.navName').hide();
		
		$(this).children('.startNav').animate({
		    //width: '55px',
			width: '100px',
		    opacity: 0
		  }, 
		  1000, 
		  //"swing", 
		  function() {
			  $(this).hide();
			   $(this).next().show();
		});
		
		$(this).animate({
		    marginLeft: '1px',
		    marginRight: '1px'
		  }, 
		  1000,
		  function(){
			  if( $(this).children('.startNav').attr('id') == $(wsk).attr('id') ){
				  showContent( $(wsk).next(), true );
			  }
		  }
		);
		
	});
}

function restartNavigation(){

	$aktCon = $('#content');
	
	//zmniejszanie boxa z trescia
	$aktCon.removeAttr('id');
	$aktCon.parent().animate({
	    width: '0'
	  }, 
	  500,
	  function() {
		$(this).hide();
		$aktCont.html('');
					
		$('.navBox').each(function(){
			$(this).children('.majorNav').hide();//fadeOut(500);
			$(this).children('.startNav').show().animate({
				width: '205px',
			    opacity: 1
			  }, 
			  500, 
			  function() {
				$(this).hover(function() {
					$(this).css({'background-position': 'top right'});
				}, function() {
					$(this).css({'background-position': 'top left'});
				});
				$(this).children('.navName').show();
			});
			
			$(this).animate({
			    marginLeft: '10px',
			    marginRight: '10px'
			  }, 
			  500
			);
		});
	});
}

function showContent(wsk, firstOpen){

	$aktCont = $('#content');
	$newCont = $(wsk).parent().next().children('.contentText');
	
	if( !$newCont.is('#content') )// nie nacisnieto na ten sam przycisk
	{
		$aktCont.removeAttr('id');
		$aktCont.parent().animate({
		    width: '0'
		  }, 
		  500,
		  function() {
			  $(this).hide();
			  $aktCont.html('');
		  }
		);
	}
	
	if( !$newCont.is('#content') || firstOpen )// nie nacisnieto na ten sam przycisk lub otwarcie pierwszy raz dowolnej zakladki w intro
	{
		$newCont.attr('id','content');
		$newCont.parent().show()
		.animate({
		    width: '700px'
		  }, 
		  500
		);
	}
}

function opis_scroll() {

	$(".sliderContent").css('width', '630px');
	var opis_container = $(".sliderContainer").height();
	var opis_height = $(".sliderContent").height();
	var value = opis_height - opis_container;

   if(value > 0) {
       $("#vertical-slider").hide();
       $("#vertical-slider").slider({
           orientation: "vertical",
           value: value,
           min: 0,
           max: value,
           step: 1,
           slide: function(event, ui) {
    	   		$(".sliderContent").css("top", "-"+(value-ui.value)+"px");
           }
       }).css("margin", "auto").css("height", opis_container);
       $("#vertical-slider").show();
   }
   else{
	   $(".sliderContent").css('width', 'auto');
   }
   

}

