var method = "easeOutQuart";
var speed = 1500;
var actpage = 0, actpane = 1;
var cardir = 0;
var timer, btimer;
var npage;
var bgcolor;
var clicked = false;
var myWidth = 0, myHeight = 0;
getSize();
var dw = myWidth;
var myAddClass = 'brown';

function checkLoaded(){
  if(!bgimg.complete){
    timer = setTimeout("checkLoaded()",200);
  }else{
    (npage<actpage) ? slideBack() : slideForth();
    if(npage==0 && $('.pane').size()>1 ){btimer = setInterval("turnCarousel(0)",5000);}
    actpage=npage;
  }
  return false;
}

function getSize() {
  if(typeof(window.innerWidth)=='number') {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  }else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  }else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
}

function slideForth() {
  var leftpos = $('.bgwrap').eq(0).css('left');
  leftpos = leftpos.substr(0,leftpos.length-2);
  $('body').css('background','#'+bgcolor);
  $('#bgwrap').append('<div class="bgwrap"></div>');
  $('.bgwrap:gt(1)').remove();
  var newleft = 1*leftpos+dw;
  $('.bgwrap').eq(1).css({'background':'url('+bgpic+') no-repeat center top','left':newleft+'px'}).animate({'opacity':0}, 10);
  $('.bgwrap').eq(0).animate({'left': leftpos-dw+'px','opacity':0}, {duration: speed, easing: method, complete: function(){$(this).remove();} });
  $('.bgwrap').eq(1).animate({'left': 0, 'opacity': 1}, {duration: speed, easing: method, complete: function(){
      if(actpage!=0){
        $('.menu ul li').eq(0).addClass('active');
        $('.prod'+actpage).fadeIn(250);
        $('.usefullLinks').addClass(myAddClass);
        $('.usefullLinks').fadeIn(250);
      }else{
        if( $('.pane').size() > 1 ){ $('.cctrl').fadeIn(50); }
      }
      $('.preload').hide();
    } 
  });
}

function slideBack() {
  var leftpos = $('.bgwrap').eq(0).css('left');
  leftpos = leftpos.substr(0,leftpos.length-2);
  $('body').css('background','#'+bgcolor);
  $('#bgwrap').prepend('<div class="bgwrap"></div>');
  $('.bgwrap:gt(1)').remove();
  $('.bgwrap').eq(0).css({'background':'url('+bgpic+') no-repeat center top','left':leftpos-dw+'px'}).animate({'opacity':0}, 10);
  $('.bgwrap').eq(1).animate({'left': 1*leftpos+dw+'px','opacity':0}, {duration: speed, easing: method, complete: function(){$(this).remove();} });
  $('.bgwrap').eq(0).animate({'left': 0, 'opacity': 1}, {duration: speed, easing: method, complete: function(){
      if(actpage==0){
        $('.menu ul li').removeClass('active');
        $('.navig ul li').removeClass('active');
        if( $('.pane').size() > 1 ){ $('.cctrl').fadeIn(50); }
        $('.hp-banners').fadeIn(500,function(){
          $('#hp_video').fadeIn(500);
        });
      }else{
        $('.usefullLinks').addClass(myAddClass);
        $('.usefullLinks').fadeIn(250);
        $('.prod'+actpage).fadeIn(250);
      }
      $('.preload').hide();
    } 
  });
}

function initCarousel() {
  if( $('.pane').size() > 1 ){
    $('.pane:gt(0)').hide();
    $('.cctrl').show();
    btimer = setInterval("turnCarousel(0)",5000);
  }else{
    $('.cctrl').hide();
  }
}

function turnCarousel() {
  if(cardir==0){
    actpane = 1 * actpane + 1;
    if(actpane > $('.pane').size() ){ actpane = 1; }
    if(!isIE6 && !isIE7){
      $('.pane:visible').animate({'left':'-762px','opacity':0},{duration: 750, easing: method, complete: function(){$(this).hide();} });
      $('#pane'+actpane).css('left','762px').show().animate({'opacity':'0'},10);
      $('#pane'+actpane).animate({'left':'0','opacity':1},{duration: 750, easing: method, complete: function(){$(this).show();$('.cctrl').fadeIn(50);} });
    }else{
      $('.pane:visible').animate({'left':'-762px'},{duration: 750, easing: method, complete: function(){$(this).hide();} });
      $('#pane'+actpane).css('left','762px').show();
      $('#pane'+actpane).animate({'left':'0'},{duration: 750, easing: method, complete: function(){$(this).show();$('.cctrl').fadeIn(50);} });
    }
  }else{
    cardir=0;
    actpane = actpane - 1;
    if(actpane<1){ actpane=$('.pane').size(); }
    if(!isIE6 && !isIE7){
      $('.pane:visible').animate({'left':'762px','opacity':0},{duration: 750, easing: method, complete: function(){$(this).hide();} });
      $('#pane'+actpane).css('left','-762px').show().animate({'opacity':'0'},10);
      $('#pane'+actpane).animate({'left':'0','opacity':1},{duration: 750, easing: method, complete: function(){$(this).show();$('.cctrl').fadeIn(50);} });
    }else{
      $('.pane:visible').animate({'left':'762px'},{duration: 750, easing: method, complete: function(){$(this).hide();} });
      $('#pane'+actpane).css('left','-762px').show();
      $('#pane'+actpane).animate({'left':'0'},{duration: 750, easing: method, complete: function(){$(this).show();$('.cctrl').fadeIn(50);} });
    }
  }
}

// ############################ DOC.READY ##############################################################################

$(function(){
  $(function(){
    $("#lifestyle").fancybox({
      //'width' : '562px',
      //'height' : '461px',
      'autoScale' : false,
      'transitionIn' : 'none',
      'transitionOut' : 'none',
      'type' : 'iframe'
    });
  });

  $('.navig ul li a').click(function(){
    $(this).blur();
    $('.navig ul li').removeClass('active');
    $(this).parent().addClass('active');
    $('.cctrl').hide();
    $('#hp_video').hide();
    $('.hp-banners').fadeOut(250);
    $('a.a-prev, a.a-next').fadeOut(250);
    var val = 1*1+$('.navig ul li a').index(this);
    if(actpage != val){
      $('.preload').show();
      $('.prod-info').fadeOut(50);
      $('.usefullLinks').fadeOut(50, function() {
        $('.usefullLinks').removeClass('brown').removeClass('blue').removeClass('black');
      });
      
      myBgColor = $(this).attr('rel');
      myAddClass = 'brown';
      if(myBgColor == '140500' || myBgColor == '000001' || myBgColor == '040000' || myBgColor == '200401') {
        myAddClass = 'brown';
      } else if(myBgColor == '000') {
        myAddClass = 'black';
      } else if(myBgColor == '286287') {
        myAddClass = 'blue';
      }

      clearInterval(btimer);
      bgcolor = $(this).attr('rel');
      npage = val;
      bgpic = "images/0"+npage+".jpg";
      bgimg = new Image();
      bgimg.src = bgpic;
      checkLoaded(myAddClass);
    }
    return false;
  });
  
  $('.logo').click(function(){
    $(this).blur();
    if(actpage != 0){
      $('.preload').show();
      $('.prod-info').fadeOut(150);
      clearInterval(btimer);
      bgcolor = $(this).attr('rel');
      npage = 0;
      bgpic = "images/00.jpg";
      bgimg = new Image();
      bgimg.src = bgpic;
      checkLoaded();

      $('.usefullLinks').fadeOut(50, function() {
        $('.usefullLinks').removeClass('brown').removeClass('blue').removeClass('black');
      });
    }
    return false;
  });

  $('.menu ul li a').click(function(){
    $(this).blur();
    //return false;
  });

  $('a.a-prev').click(function(){
    $(this).blur();
    clicked = true;
    $('.cctrl').hide();
    clearInterval(btimer);
    cardir=-1;
    turnCarousel();
    //btimer = setInterval("turnCarousel()",5000);
    return false;
  });

  $('a.a-next').click(function(){
    $(this).blur();
    clicked = true;
    $('.cctrl').hide();
    clearInterval(btimer);
    turnCarousel();
    //btimer = setInterval("turnCarousel()",5000);
    return false;
  });
  
  $('.hp-banners').hover(function(){
    if( $('.pane').size() > 1 ){
      clearInterval(btimer);
    }
  },function(){
    if( $('.pane').size() > 1 && clicked==false){
      btimer = setInterval("turnCarousel()",5000);
    }
  });

  $(window).resize(function(){
    getSize();
    if(myWidth<980){
      myWidth=980;
      if(myHeight<740){myHeight=740;}
    }
    if(myWidth>980){
      if(myHeight<770){myHeight=770;myWidth=myWidth-20;}
    }
    //if(myHeight<770){myHeight=770;}
    $('#bgwrap, .bgwrap').css(({'height':myHeight+'px','width':myWidth+'px'}));
    return false;
  });

  $('.dodatek').click(function(){
    $('.dodatek').fadeOut(50);
    return false;
  });

  $('.e-vice').click(function(){
    $(this).blur();
    var val = $(this).attr('href').substring(1);
    $('#'+val).fadeIn(150);
    return false;
  });

  $('.prod-info').hide();

  if(isIE6==true){
    $('.hp-banners *').pngFix();
  }

  if( $('.pane').size() > 1 ){initCarousel();} else { $('.cctrl').hide(); }

  getSize();
  if(myWidth<980){
    myWidth=980;
    if(myHeight<740){myHeight=740;}
  }
  if(myWidth>980){
    if(myHeight<770){myHeight=770;myWidth=myWidth-20;}
  }
  //if(myHeight<770){myHeight=770;}
  $('#bgwrap, .bgwrap').css(({'height':myHeight+'px','width':myWidth+'px'}));

});













