/* 
 Funkcje: podstawHtml(),zoom(),galeria(),zaladujStrone()
 Last edit by M.Bujak 2010-11-21
*/

function podstawHtml(content){
    
    $("div#wrapper").append("<div id=\"forFadeIE\">");
    $("div#changeBox").html(content);

    if ($.browser.msie && parseInt($.browser.version) == 7){
        $(".center").each(function(index){
            //centrowanie dla ie7
    	    var wysokosc = $(this).css('height')
    	    $("div",this).wrapInner("<table style=\"width:100%; height:"+wysokosc+"\"><tr><td style=\"vertical-align:middle\">");
    	    $("#forCenter").wrapInner("<table style=\"width:100%; height:100%\"><tr><td style=\"vertical-align:middle\">");
    	});
    }else{
        //hack na safari, które wymaga innej nazwy mapy dla kazdej postrony
        var currentTime = new Date();
    	$("img#navPrevNext").attr({usemap:"#r"+currentTime.getTime()});
    	$("map#MapaImg").attr({name:"r"+currentTime.getTime()});
    }
	
    Cufon.replace(".cf");

    $(".change").hide();
    $(".css-scrollbar").scrollbar();
    
    $("div.item:odd").addClass("odd");
	$("div.galleryBox.main ul:odd").addClass("odd");
	$("div.galleryBox.main ul:even").addClass("even");

    galeria();
    //zaladujStrone();
    anchoruj();
	//do głownej galerii	
	$("div#itemFull.galeria ul a").hover(
	    function(){
	        var ktory = $(this).attr("rel");
	        $("."+ktory).fadeIn("slow");
	    },
	    function(){
	        var ktory = $(this).attr("rel");
    	    $("."+ktory).hide();  
	    }
	);
	if($("div#rotator img").length)
	{
		$("div#rotator img").hide();
		if($("div#rotator img").length == 1)
		{
			$("div#rotator img:eq(0)").show();
		}
		else
		{
			var ranNum= Math.floor(Math.random()*$("div#rotator img").length)	;
			$("div#rotator img:eq("+ranNum+")").show();
		}
		/*if(parseInt(Math.random()*100)%2 == 0){
			$("div#rotator img:eq(1)").hide();
		}else{
			
		}*/
	}
    $("a#navZoom,a#imgZoom").click(function () {
	    ///console.log($(this));
         var strona = $(this).attr("href"); 
         var obrazek = parseInt($(this).attr("rel")) - 1;
         $("#loader").show();
         $.get(strona,function(out){
             $("a#navZoom").unbind('click');
             podstawHtml(out);
             zoom(obrazek);
             $("#loader").hide();
         });
 	    return false; 
	});
	$("div#aside div.kwadrat").removeClass("fl");
	$("div#forFadeIE").fadeTo(1300, 0, function() {
        $("div#forFadeIE").remove();
     });
}
function zaladujStrone(){
    /*$("a.zaladuj,.zaladuj a").live("click", function () {
        var strona = "home.html";
        if ($(this).attr("href") == "" || typeof ($(this).attr("href")) == "undefined") {
            //console.log($(this).attr("rel"));
            strona = $(this).attr("rel");
        }
        else {
            strona = $(this).attr("href");
            //console.log($(this).attr("href"));   
        }
        if (strona.indexOf('?') != -1)
            strona += '&ajx=body';
        else
            strona += '?ajx=body';
        $("#loader").show();
        $.get(strona, function (out) {
            $("a.zaladuj,.zaladuj a").unbind('click');
            podstawHtml(out);
            $("#loader").hide();
        });
        return false;
    });*/
}

function anchoruj() {
    $a = $('a.zaladuj,.zaladuj a,area.zaladuj');
    $a.each(function () {
        var href = $(this).attr('href');
        if (href.indexOf('#') == -1) {
            if (doc_root.length == 0 || href.indexOf(doc_root) == 0) {
                var newhref = href.substr(doc_root.length);
                $(this).attr('href', '#' + newhref);
            }
        }
    });
}

var currentAnchor = '';

function monitoranchor() {

    if (currentAnchor != document.location.hash) {
        currentAnchor = document.location.hash;
        var strona = doc_root + currentAnchor.substr(1);
        if (strona.indexOf('?') != -1)
            strona += '&ajx=body';
        else
            strona += '?ajx=body';
        
        naviguj(strona);
    }
}

function naviguj(strona) {
    $("#loader").show();
    $.get(strona, function (out) {
        //$("a.zaladuj,.zaladuj a").unbind('click');
        podstawHtml(out);
        $("#loader").hide();
    });
}

$(document).ready(function () {
    anchoruj();
	podstawHtml($('div#changeBox').html());
    setInterval(function () { monitoranchor(); }, 1000);
});
