$(function(){
	var LoadMsg = 'Please Wait ...';
	var AjaxPath = '/page/';
	$('#top ul li a').click(function(){
		if(!$(this).hasClass("current")) {
			var _Href = $(this).attr('href').replace("/", "/page/");
			if (_Href.match(/portfolio/i) == null){
			  do{ var randImage = bgImages[Math.ceil(Math.random()*(bgImages.length-1))]; }while( randImage == currImage )
        currImage = randImage;
        $('#box2').BgImageTransition( currImage );
      }
      else {
        gallery_from_href(_Href);
        $('#box2').BgImageTransition(gallery[gallery_id][photo_id]);
        currImage==gallery[gallery_id][photo_id];
      }
      if(jQuery.BgImageTransitions.gallery!=undefined) delete jQuery.BgImageTransitions.gallery;
			$('#top ul li a').removeClass("current");
			$(this).addClass("current");
			$('#loading').fadeIn('slow',function(){
				$.ajax({
					type:	'GET',
					url:	_Href,
					dataType:	'html',
					timeout:	5000,
					success: function(d,s){
							$('#loading').fadeOut('fast',function(){
								$('#box3').fadeOut('medium',function(){
										$(this).html(d).fadeIn('medium');
										ajax_href_loader();
										ajax_gallery();
										window.location='#'+_Href;
									});
								});
							},
					error: function(o,s,e){
								window.location=_Href;
							}
				});
			});
		}
		return false;
	});
});

function ajax_href_loader()
{
	var LoadMsg = 'Please Wait ...';
	var AjaxPath = '/page/';
	$('#text a').click(function(){
		if(!$(this).hasClass("current")) {
			var _Href = $(this).attr('href').replace("/", "/page/");
			if (_Href.match(/portfolio/i) != null) {
        gallery_from_href(_Href);
        $('#box2').BgImageTransition(gallery[gallery_id][photo_id]);
        currImage==gallery[gallery_id][photo_id];
        }
			$('#text ul li a').removeClass("current");
			$(this).addClass("current");
			$('#loading').fadeIn('slow',function(){
				$.ajax({
					type:	'GET',
					url:	_Href,
					dataType:	'html',
					timeout:	5000,
					success: function(d,s){
							$('#loading').fadeOut('fast',function(){
								$('#box3').fadeOut('medium',function(){
										$(this).html(d).fadeIn('medium');
										ajax_href_loader();
										ajax_gallery();
										window.location='#'+_Href;
									});
								});
							},
					error: function(o,s,e){
								window.location=_Href;
							}
				});
			});
		}
		return false;
	});
}

function ajax_gallery()
{
  pic1= new Image(); 
  pic2= new Image();
  $("#gallery_left a").attr("href", "#"+$("#gallery_left a").attr('href'));
  $("#gallery_right a").attr("href", "#"+$("#gallery_right a").attr('href'));
  var LoadMsg='Please Wait...';
  $('#gallery_left a').click(function(){
     $('#loading').fadeIn('slow',function(){
          if(photo_id_back<=0){
            $('#gallery_left').fadeOut('fast',function(){
              $('#text').fadeIn('fast');
            });
          }
          photo_id_back--;
          photo_id_next--;
          photo_id--;
          $('#box2').BgImageTransition(gallery[gallery_id][photo_id]);
          $("#gallery_left a").attr("href", "#/portfolio/"+gallery_id+"/photo/"+photo_id_back);
          $("#gallery_right a").attr("href", "#t/portfolio/"+gallery_id+"/photo/"+photo_id_next);
          currImage==gallery[gallery_id][photo_id];
          $(pic1).load(function () {}).error(function () {}).attr('src', gallery[gallery_id][photo_id_back]);
          $("#licznik").html(photo_id_next + " / " + gallery[gallery_id]['count']); 
          if(photo_id_next<=gallery[gallery_id]['count']-1){
              $('#gallery_right').fadeIn('fast');
          }
      });
  });
  $('#gallery_right a').click(function(){
      $('#loading').fadeIn('slow',function(){
            if(photo_id_next>=gallery[gallery_id]['count']-1){
              $('#gallery_right').fadeOut('fast');
            }
            photo_id_back++;
            photo_id_next++;
            photo_id++;
            $('#box2').BgImageTransition(gallery[gallery_id][photo_id]);
            $("#gallery_left a").attr("href", "#/portfolio/"+gallery_id+"/photo/"+photo_id_back);
            $("#gallery_right a").attr("href", "#/portfolio/"+gallery_id+"/photo/"+photo_id_next);
            currImage==gallery[gallery_id][photo_id];
            $(pic2).load(function () {}).error(function () {}).attr('src', gallery[gallery_id][photo_id_next]);
            $("#licznik").html(photo_id_next + " / " + gallery[gallery_id]['count']); 
            if(photo_id_back>=0){
              $('#text').fadeOut('fast',function(){
                $('#gallery_left').fadeIn();
              });
            }
    });
  });
}

function gallery_from_href(href) {
    href=replace_all("/", "|", href);
    var hrefarray = href.split(/\|/);
		for (var i = 0; i < hrefarray.length; i++) {
	       		if(hrefarray[i]=='portfolio') gallery_id=hrefarray[i+1];
         		else if(hrefarray[i]=='photo') {
            			photo_id=parseInt(hrefarray[i+1]);
            			photo_id_next=(photo_id+1);
            			photo_id_back=(photo_id-1);
          		}
        	}
    if(gallery_id==0) gallery_id=1;
}

function replace_all(what, whot, text){
    while(text!=(text=text.replace(what, whot)));
    return text;
}

$(function(){
if(location.hash!="") window.location=(location.hash).substring(1).replace("/page/", "/");
});