// JavaScript Document

var zviratkaTransition = 500;
var noScroll = true;
var goRight = false;
var goLeft = false;
var galleryScroll = 0;
var galleryScrollTimeout = 9;
var galleryScrollOffset = 1;
var maxOffsetRight = 100;
var actYear = 2010;
var firstFile;
var imgPath;
var imgLoadedCounter=0;
var jqueryLoaded = false;

var width;
var height;

$("document").ready(function(){
  jqueryLoaded = true;
  $("#galleryMain").load("returnimg.php?file="+firstFile+"&path="+imgPath);
  $("#left").css("background-image","url('grafika/sipky/sipky.png')");
  width = GetWidth();
  height = GetHeight();
  $(".intro img").css("height",(height*0.8)+"px");
  $(".intro img").css("top",(height*0.2+height)+"px");
  $(".intro img").css("opacity","1.0");
  
 // setTimeout("$('.intro img').animate({top:(height*0.2)+'px'},{queue:false,duration:1000});", 2000)
  $("body").mousemove(function(evt){
    $('#kocka2').css("opacity", ( evt.pageX) / ( width));
    $(".intro img").css("left",evt.pageY+"px");    
  });
/*  $('#kocka2').hover(function()     
    {
			$("#kocka2").stop().animate({opacity:'1.0'},{queue:false,duration:1500});
		}, function() {
			$("#kocka2").stop().animate({opacity:'0.0'},{queue:false,duration:1500});
		}); */
  
  if (!noScroll){
     maxOffsetRight = (84 + 15) * fileCount - 300 - 15; // (width + margin) * pocetsouboru - sirkaboxu - marginleft
    $("#right").mouseenter(function(){
      goRight = true; 
      rightScroll();
    });
      
    $(".sipky").mousedown(function(){
      galleryScrollOffset=3;
    });
    
    $(".sipky").mouseup(function(){
      galleryScrollOffset=1;
    });
    
    $("#right").mouseleave(function(){
      goRight = false;
      galleryScrollOffset=1; 
    });
    
    $("#left").mouseenter(function(){
      goLeft = true; 
      leftScroll();
    });
  
    
    $("#left").mouseleave(function(){
      goLeft = false;
      galleryScrollOffset=1;  
    });
  }
  else{
    $("#left").css("background-image","url('grafika/sipky/sipky.png')");
    $("#right").css("background-image","url('grafika/sipky/sipky2.png')");
    //$("#left").css("opacity",0.5);
    //$("#right").css("opacity",0.5);   
  }
  
/*  $(".galleryAnimals").hover(function()     
    {
			$(this).html("<img src='"+galleryDir+"/cmaranice.png'>");
		}, function() {
		  $(this).html("");
		}); */
		
	$(".galleryAnimals").click(function(){
    var file = $(this).attr("id");
    // alert( "url:('"+imgPath+"/"+file+"')");
   // $("#galleryMain").css("background-image","url('"+imgPath+"/"+file+"')");
    $("#galleryMain").load("returnimg.php?file="+file+"&path="+imgPath);
   
  });
  
	$(".zakazniciAnimals").click(function(){
    var file = $(this).attr("id");
    $("#zakazniciMain").load("returnimg.php?file="+file+"&path="+imgPath);
  });
  
  $('.zviratka').hover(function()     
    {
      var id = $(this).attr("id");
			$("#"+id+"_text").stop().animate({opacity:1},{queue:false,duration:zviratkaTransition});
			
		}, function() {
		  var id = $(this).attr("id");
		  $("#"+id+"_text").stop().animate({opacity:0},{queue:false,duration:zviratkaTransition});
		});
		
		$('#actualwork').hover(function()     
    {
      var id = $(this).attr("id");
			$("#actualwork").stop().animate({top:"0px"},{queue:false,duration:zviratkaTransition});
			
		}, function() {
		  var id = $(this).attr("id");
		  $("#actualwork").stop().animate({top:"-180px"},{queue:false,duration:zviratkaTransition});
		});
	
	 $("body.intro").click(function(){
    window.location="omne.php";  
   });

   $("#o-mne").click(function(){
    window.location="omne.php";  
   });
   $("#galerie").click(function(){
    window.location="galerie.php";  
   });
   $("#jak-objednavat").click(function(){
    window.location="jakobjednavat.php";  
   });
   $("#cenik-pred").click(function(){
    window.location="cenik.php";  
   });
   $("#spokojeni-zakaznici").click(function(){
    window.location="kontakt.php";  
   });
   $("#dog_text").click(function(){
    window.location="gallery.php?kat=psi&year="+actYear;  
   });
   $("#cat_text").click(function(){
    window.location="gallery.php?kat=kocky&year="+actYear;  
   });
   $("#horse_text").click(function(){
    window.location="gallery.php?kat=kone&year="+actYear;  
   });
   $("#other_text").click(function(){
    window.location="gallery.php?kat=ostatni&year="+actYear;  
   });
});

function rightScroll(){
  if (goRight){
    galleryScroll-=galleryScrollOffset;
    if (galleryScroll<-maxOffsetRight){
      galleryScroll = -maxOffsetRight;
      //$("#right").css("opacity",0.5);   
      $("#right").css("background-image","url('grafika/sipky/sipky2.png')");
    }
    $("#sliderContent").css('left',galleryScroll+"px");
    setTimeout("rightScroll()",galleryScrollTimeout);
    //$("#left").css("opacity",1.0); 
    $("#left").css("background-image","url('grafika/sipky/sipky-plna.png')");
  }
}

function introLoaded(){
  imgLoadedCounter++;
  if (imgLoadedCounter>=2){
    if (!jqueryLoaded){
      setTimeout("introLoaded()",100);
    }
    else{
      $('.intro img').animate({top:(height*0.2)+'px'},{queue:false,duration:1500});  
    }
  }
}

function leftScroll(){
  if (goLeft){
    galleryScroll +=galleryScrollOffset
    if (galleryScroll>0){
      galleryScroll = 0;
      $("#left").css("background-image","url('grafika/sipky/sipky.png')");
    }
    $("#sliderContent").css('left',galleryScroll+"px");
    setTimeout("leftScroll()",galleryScrollTimeout);
    $("#right").css("background-image","url('grafika/sipky/sipky2-plna.png')");
  }
}


function GetWidth()
{
        var x = 0;
        if (self.innerHeight)
        {
                x = self.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                x = document.documentElement.clientWidth;
        }
        else if (document.body)
        {
                x = document.body.clientWidth;
        }
        return x;
}

function GetHeight()
{
        var y = 0;
        if (self.innerHeight)
        {
                y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                y = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
                y = document.body.clientHeight;
        }
        return y;
}
  
//function used from: http://github.com/peol/jquery.imgloaded/blob/master/ahpi.imgload.js
(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}

				else {
					$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));
                  
