$(document).ready(function(){
  var ie = /*@cc_on!@*/0;
  var products = 19;
  var items = 19;
  var item = 0;
  var pos = new Array();
  pos = [
    [0,583,0,472,0,532,'prirodni','#fbfffe','Přírodní'],
    [620,557,475,450,523,510,'citron','#fefef6','Citron'],
    [650,579,494,470,455,530,'pomeranc','#fffcf7','Pomeranč'],
    [575,557,475,450,483,510,'citrusy','#fafff4','Citrus Mix'],
    [640,578,500,470,553,530,'svestka','#f6f9fe','Švestka'],
    [640,578,510,470,0,530,'visen','#fff7f7','Višeň'],
    [460,578,0,470,0,530,'pomelo','#f6fcff','Pomelo'],
    [640,578,510,470,523,530,'citron-neperliva','#f9fdff','Citron neperlivá'],
    [640,578,490,470,553,530,'svestka-neperliva','#f9fdff','Švestka neperlivá'],
    [500,578,0,470,523,530,'prolinie-citron','#fffef7','ProLinie citron'],
    [610,578,510,470,455,530,'prolinie-pomeranc','#fffcf7','ProLinie pomeranč'],
    [460,578,0,470,0,530,'prolinie-grep','#fff8f7','ProLinie grep'],
    [500,578,0,470,513,530,'prolinie-limetka','#f9fdff','ProLinie limetka'],
    [600,578,480,470,0,530,'prolinie-aktiv','#fffcfe','ProLinie aktiv'],
    [460,578,0,470,0,530,'prolinie-rybiz','#f9fbff','ProLinie černý rybíz'],
    [610,578,0,470,0,530,'prolinie-pomelo','#f9fbff','ProLinie pomelo'],
    [660,578,490,470,0,530,'prolinie-ostruzina','#f9fbff','ProLinie ostružina'],
    [470,578,0,470,0,530,'mineral-limeta','#fbfef8','Mineral Drink limeta'],
    [680,578,520,470,465,530,'mineral-multivitamin','#fffcf7','Mineral Drink multivitamín']
  ];
  var hash = window.location.hash.substr(1);
  var pathname = window.location.pathname;
  var pathfield = pathname.split('/');
  var cesta = pathfield[pathfield.length-1];

  if (hash=='') {
    $('#site').css({'visibility': 'hidden'});
    $('#loader').css({'height': $(window).height()+'px', 'display': 'block'});
    $('body').css({'overflow': 'hidden'});
    $('#loader').waitForImages(function() {
      for (i = 0; i < items; i++) {
        $('#loadingImg').append('<img src="images/head/'+ pos[i][6] +'.png" alt="" />');
        $('#loadingImg').append('<img src="images/bg/'+pos[i][6]+'-repeat.jpg" alt="" />');
        $('#loadingImg').append('<img src="images/bg/'+pos[i][6]+'.jpg" alt="" />');
        $('#loadingImg').append('<img src="images/prod/'+pos[i][6]+'-1.png" alt="" />');
        $('#loadingImg').append('<img src="images/prod/'+pos[i][6]+'-2.png" alt="" />');
        $('#loadingImg').append('<img src="images/prod/'+pos[i][6]+'-4.png" alt="" />');
        $('#loadingImg').append('<img src="images/prod/'+pos[i][6]+'-6.png" alt="" />');
      }
      $('#loadingImg').waitForImages(function() {
          $('#site').css({'visibility': 'visible'});
          $('#loader').delay(1900).fadeOut('slow');
          $('body').delay(1900).css({'overflow': 'auto'});
          }, function(loaded, count) {
          	progress = Math.floor(loaded/count*100);
          	// $('.loadingLahev').animate({ 'height': loaded*3+'px' },10);
          	$('.loadingLahev').animate({ 'height': progress+'%' },10);
          	$('.loadingProgress').text(progress+'%');
      });
    });
  }

  if (cesta) {
    $('.control').find('a').each(function()
		{
			var href = $(this).attr('href');

			if ( cesta == href )
			{
        item = $(this).parent().attr('class').substr(1)-1;
        carousel(item);
			}
		});
    window.location.hash = cesta;
		window.location.pathname = '';
  }
  
  if ($(window).height()<723) {
    $('#content').css({
                  'position': 'relative',
                  'bottom': 0
                  });
    $('#main').css({
                  'height': '720px'
                  });
  }

  if (hash)
	{
		$('.control').find('a').each(function()
		{
			var href = $(this).attr('href');

			if ( hash == href )
			{
        item = $(this).parent().attr('class').substr(1)-1;
        carousel(item);
			}
		});
	} else {
    $('.head').hide();
    for (i = 1; i < 7; i++) {
        $(".o"+i).animate({'left': '2000px'},1);
        if ((i==4) || (i==1)) {
          $(".o"+i).animate({'left': '-2000px'},1);
        }
    }

    $('.item').prepend('<img class="loading" src="images/loading.gif" alt="" />');
    $('.head').waitForImages(function() {
        $('.loading').hide().remove();
        if(ie == true) {
          $('.head').show();
        } else {
          $('.head').fadeIn();
        }
        for (i = 1; i < 7; i++) {
            $(".o"+i).show();
            move = 650+i*20;
            $(".o"+i).delay(move).animate({"left": + pos[item][i-1] +"px"}, 1200, 'easeOutExpo', function()
  		    {
      		    $('.o1').plaxify({"xRange":280,"yRange":0});
              $('.o2').plaxify({"xRange":130,"yRange":0});
              $('.o3').plaxify({"xRange":220,"yRange":0});
              $('.o4').plaxify({"xRange":150,"yRange":0});
              $('.o5').plaxify({"xRange":100,"yRange":0});
              $('.o6').plaxify({"xRange":50,"yRange":0});
          });
        }
        $('.items').mouseenter(function() {
          $.plax.enable();
        });
        $('.items').mouseleave(function() {
          $.plax.disable();
        });
    });
  }

  $("#menuBlock").hoverIntent(
    function () {
      $('#menu').animate({
        top: '-120px'
      }, 600, function() {
      });
    },
    function () {
      $('#menu').animate({
        top: '0px'
      }, 600, function() {
      });
    }
  );

  $(".textMenu a").hoverIntent(
    function () {
      if(ie == true) {
        $("span", this).show();
      } else {
        $("span", this).fadeIn();
      }
    },
    function () {
      if(ie == true) {
        $("span", this).hide();
      } else {
        $("span", this).fadeOut();
      }
    }
  );
  
  $(".download a").hoverIntent(
    function () {
      if(ie == true) {
        $("span", this).show();
      } else {
        $("span", this).fadeIn();
      }
    },
    function () {
      if(ie == true) {
        $("span", this).hide();
      } else {
        $("span", this).fadeOut();
      }
    }
  );

  $('#footer li a').click(function(){

    var toLoad = $(this).attr('href') + '.php';
    var slide = $(this).attr('rel');

    if (($(window).height()-55)<slide) {
      slide= 60-($(window).height()-55);
      $('body').css({'overflow': 'hidden'});
    } else {
      slide= 60-slide;
      $('body').css({'overflow': 'auto'});
    }

    $('#subBlock').css({'height': ($(window).height()-55)+'px'});
    $('#subBlock').animate({top: '60px'}, 400);
    $('#footer li a').removeClass('active');

    $('#subBlock').load(toLoad + ' .toLoad', function(){
      Cufon.refresh();
      $('#subBlock').animate({
        top: slide + 'px'
      }, 400);

      $(".close").click(
        function () {
          $('#subBlock').animate({
            top: '60px'
          }, 400);
        $('#footer li a').removeClass('active');
        $('body').css({'overflow': 'auto'});
        return false;
        }
      );
      
      if (toLoad == 'historie.php') {
        $(function()
          {
          	$('.scroll-pane').jScrollPane();
          });
        }

      if (toLoad == 'reklamy.php') {
        $(function()
          {
          	$('.scroll-pane').jScrollPane();
          });
          
          $('.spotBlock').click(function () {
              $('iframe', this).show();
              return false;
          });
        }


    });
    $(this).addClass('active');
    return false;
  });


    function animace(path)
  		    {
            $('.items').hide().load(path + '.php .item', function()
        		{
              $('.head').hide();
              for (i = 1; i < 7; i++) {
                  $(".o"+i).css({'left': '2000px'});
                  if ((i==4) || (i==1)) {
                    $(".o"+i).css({'left': '-2000px'});
                  }
              }

              $('.item').prepend('<img class="loading" src="images/loading.gif" alt="" />');
              $('.head').waitForImages(function() {
                  $('.loading').hide().remove();
                  if(ie == true) {
                    $('.head').show();
                  } else {
                    $('.head').fadeIn();
                  }
                  for (i = 1; i < 7; i++) {
                      $(".o"+i).show();
                      move = 650+i*20;
                      $(".o"+i).delay(move).animate({"left": + pos[item][i-1] +"px"}, 1200, 'easeOutExpo', function()
              		    {
                		    $('.o1').plaxify({"xRange":280,"yRange":0});
                        $('.o2').plaxify({"xRange":130,"yRange":0});
                        $('.o3').plaxify({"xRange":220,"yRange":0});
                        $('.o4').plaxify({"xRange":150,"yRange":0});
                        $('.o5').plaxify({"xRange":100,"yRange":0});
                        $('.o6').plaxify({"xRange":50,"yRange":0});
                      });
                  }
                  $('.items').mouseenter(function() {
                    $.plax.enable();
                  });
                  $('.items').mouseleave(function() {
                    $.plax.disable();
                  });
              });

              $('#bg').css({ 'background-image': 'url(images/bg/'+pos[item][6]+'-repeat.jpg)' });
              $('.bgIn').css({ 'background-image': 'url(images/bg/'+pos[item][6]+'.jpg)' });
              $('body').css({ 'background': pos[item][7] });
              Cufon.refresh();
              if(ie == true) {
                $(this).show();
              } else {
                $(this).fadeIn('normal');
              }

              if (item == 0) path = 'prirodni';
				      window.location.hash = path;
        		});
      		};




    function carousel(item) {
        var path = pos[item][6];
        if (item == 0) path = 'index';

        $('div.control li').removeClass('active');
        $(this).addClass('active');

        for (i = 1; i < 7; i++) {
            if(ie == true) {
              $(".o"+i).delay(600-100*i).hide();
            } else {
              $(".o"+i).delay(600-100*i).fadeOut();
            }
        }
        
        if(ie == true) {
          $('.item').delay(500).hide(0, function()
  		    {
            animace(path);
      		});
        } else {
          $('.item').delay(500).fadeOut('normal', function()
  		    {
            animace(path);
      		});
        }

        if (item + 1 > products ) {
            item = -1;
        }
        document.title = 'Poděbradka | ' + pos[item][8];
    };

    $('div.control li').click(function () {
        item = $(this).attr('class').substr(1)-1;
        carousel(item);

        return false;
    });


    $('#left').click(function () {
        if (item == 0) {
            item = (products - 1);
        } else {
            item = item - 1;
        };
        carousel(item);

        return false;
    });

    $('#right').click(function () {
        if (item == (items-1)) {
            item = 0;
        } else {
            item = item + 1;
        };
        carousel(item);

        return false;
    		});

    $(window).jkey('left, right',function(key){
    	var direction;
    	if(key == 'left'){
        if (item == 0) {
            item = (products - 1);
        } else {
            item = item - 1;
        };
        carousel(item);
    	}
    	else{
        if (item == (items-1)) {
            item = 0;
        } else {
            item = item + 1;
        };
        carousel(item);
    	}
    });

    $(window).bind('resize',function(){
         window.location.href = window.location.href;
    });

});
