$(document).ready(function(){

	setCookie("treeview",0,-5) // usuniecie cookie - wyzerowanie menu

	init();

	// Obsluga pierwszego wejscia na strone, gdy nie ma jeszcze zmiany hasha
	// znajduje id wpisu na podstawie tego co jest po "#!" w adresie i rozwija odpowiednią podstronę
	if (location.hash.search("#!") != -1)
		var id = znajdz_id_wpisu(location.hash.substring(2)); // adres postaci #!nazwa
	else
		var id = znajdz_id_wpisu(location.hash.substring(1)); // adres postaci #numer
	rozwin_menu(id);
	pobierz(id);
	
	// Obsługa zmiany podstrony na podstawie zmiany hasha
	$(window).hashchange( function(){
		$("#loading").show();
		// znajduje id wpisu na podstawie tego co jest po "#!" w adresie i rozwija odpowiednią podstronę
		if (location.hash.search("#!") != -1){
			_gaq.push(['_trackPageview','/'+location.hash.substring(2)]);
			id = znajdz_id_wpisu(location.hash.substring(2));			
		}
		else{
			id = znajdz_id_wpisu(location.hash.substring(1));
			_gaq.push(['_trackPageview','/'+location.hash.substring(1)]);
		}
			
		rozwin_menu(id)
		pobierz(id);
	});
	
	// Pokazywanie / ukrywanie panelu informacyjnego kotla
	$("#przelacz_panel").click(function(event) {
		$("#panel_info").toggle('slow');
	});	
	
	
	//Promocje
	$(".promocja_tresc").hide(); //Hide all content
	$("ul.promocje_menu li:first").addClass("active").show(); //Activate first tab
	$(".promocja_tresc:first").show(); //Show first tab content

	//Promocje - kliknięcie na tabie
	$("ul.promocje_menu li").click(function() {
		$("ul.promocje_menu li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".promocja_tresc").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(500); //Fade in the active ID content
		return false;
	});
	
	// Pokazywanie promocji
	$("#pokaz_promocje").click(function(event) {
		pobierz_promocje();
		$("#promocje").show();
		return false;
	});	
	
	// Ukrywanie promocji
	$("#ukryj_promocje").click(function(event) {
		$("#promocje").hide();
	});	
	

	// pobierz obrazki - tła
	$.getJSON('obsluga.php', {akcja: 'pobierz_obrazki_promo'}, function(data){

		if (data.promo.length == 0){    // brak promocji - wstawiamy domyslne tlo

			$('#promo1').css('background-image','url(\"./files/Obrazy/promo/tlo_domyslne.jpg\")');
			document.getElementById('slidetabs').innerHTML = "\n<a href=\"#\" id=\"minipromo1\"></a>\n";
			document.getElementById('promo1').setAttribute('onclick', 'pobierz(-1);');
		
		}

		else
		{
			var slidetabs = "\n";
			for (var i=1; i<=data.promo.length; i++){
				var nazwa = data.promo[i-1].zdjecia_baner;
				pozycja1 = nazwa.indexOf("src=\"",0); 
				pozycja1 = pozycja1 + 5; // pierwszy " po src
				pozycja2 = nazwa.indexOf("\"",pozycja1); // kolejny "
				nazwa = nazwa.substring(pozycja1, pozycja2).replace(/..\/files/g, "./files");

				slidetabs += "<a href=\"#\" id=\"minipromo"+i+"\"></a>\n"
			
				$('#minipromo'+i).html("<img src=\"" + nazwa + "\" alt=\"\" title=\"\" width=\"130\" onclick=\"\$(\'.slidetabs\').data(\'slideshow\').stop(); $('#panel_info').hide();\">" );
				$('#promo'+i).css('background-image','url(\"' + nazwa + '\")');
				//$('#promo'+i).attr('onclick', '$(".slidetabs").data("slideshow").stop();pobierz('+data.promo[i-1].id+');');				
				document.getElementById('promo'+i).setAttribute('onclick', 'pobierz('+data.promo[i-1].id+');');
			}		
			document.getElementById('slidetabs').innerHTML = slidetabs;
		}
		
		$(".slidetabs").tabs(".images > div", {
			// enable "cross-fading" effect
			effect: 'fade',
			fadeOutSpeed: "slow",

			// start from the beginning after the last tab
			rotate: true
			
		// use the slideshow plugin. It accepts its own configuration
		}).slideshow({autoplay:true, interval: 5000, autopause: true});
		
		$("#minipromo1").click(function() {
		alert(1)
		});	

		
	}
	);
	$(".slidetabs").data("slideshow").play();
	
});

// Facebook
$(function(){
 
	$("#facebook").hover(
		function () {
			$("#facebook").animate({left: "0px"}, 300 );
			$(this).addClass("zamknij");
		},
		function () {
			$("#facebook").animate({left: "-310px"}, 1000 );
			$(this).removeClass("zamknij");
		}
	);
});

         jQuery.fn.forceNumeric = function () {

             return this.each(function () {
                 $(this).keydown(function (e) {
                     var key = e.which || e.keyCode;

                     if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
                     // numbers   
                         key >= 48 && key <= 57 ||
                     // Numeric keypad
                         key >= 96 && key <= 105 ||
                     // comma, period and minus, . on keypad
                        //key == 190 || key == 188 || key == 109 || key == 110 ||
						key == 190 || key == 110 ||
                     // Backspace and Tab and Enter
                        key == 8 || key == 9 || key == 13 ||
                     // Home and End
                        key == 35 || key == 36 ||
                     // left and right arrows
                        key == 37 || key == 39 ||
                     // Del and Ins
                        key == 46 || key == 45)
                         return true;

                     return false;
                 });
             });
         }
		 
		 
function init(){
	
	// Wywołania AJAX sysnchroniczne, bez uzycia cache
	$.ajaxSetup({async: false, cache: true});	
		
	//Treeview
	$("#red").treeview({
		animated: "fast",
		collapsed: true,
		unique: true,
		persist: "cookie",
		toggle: function(){
			window.console && console.log("%o was toggled", this);
		}
	});

	
	// Tabs
	$("#flowtabs").tabs("#flowpanes > div");	
	$("#flowtabs2").tabs("#flowpanes2 > div");	

	$("#t9").click(function() {
		$("#flowpanes").hide('slow');
		$("#t9").hide();
	});	
	$("#t1").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});	
	$("#t2").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});	
	$("#t3").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});		
	$("#t4").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});	
	$("#t5").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});	
	$("#t6").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});		
	$("#t7").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});		
	$("#t8").click(function() {
		$("#flowpanes").show('slow');
		$("#t9").show();
	});	
	

	// Karuzela
	$("#thumbnails").scrollable();
	
	
	
//overlay - kalkulator


	// if the function argument is given to overlay,
	// it is assumed to be the onBeforeLoad event listener
	$("a[rel]").overlay({

		 // mask: 'white',
		 effect: 'apple',

		onBeforeLoad: function() {

			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");

			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}

	});


	
	
	
}

