/* 
 * Js fun for Cabinet website
 */

var slideinterval;
var inArea = false;

function initSlideSwitch(){
   // events
   $('#panel-images').mouseover(function(){
      inArea = true;
   });
   $('.button-show-image').mouseover(function(){
      inArea = true
      });
   $('#panel-images').mouseleave(function(){
      inArea = false;
   });
   
   $('.button-show-image').click(function(e){
      e.preventDefault();
      $('.button-show-image').removeClass('active');
      $(this).addClass('active');
      
      var selImageClass = $(this).attr('href').split('#')[1];
      
      var $active = $('#panel-images .slide-image.active');
      if($active.length != 0){
         $active.animate({
            opacity: 0.0
         }, 800, function() {
            $active.removeClass('active');
            var $selected = $('#panel-images .'+selImageClass);
            $selected.addClass('active').animate({
               opacity: 1.0
            }, 800);
         });
      } else {
         var $selected = $('#panel-images .'+selImageClass);
         $selected.addClass('active').animate({
            opacity: 1.0
         }, 800);
      }
   });
   
   $('.slide-image').click(function(e){
      var href = $(this).attr('href');
      // pokud odkaz odkazuje na tuto stránku nebo je admin
      if(href.indexOf(window.location.host) == -1 || isAdmin == true){
         return;
      }
      e.preventDefault();
      var pathArray = href.split( '/' );
      var urlkey = href.substr(href.substr(0, href.length-1).lastIndexOf('/') + 1).slice(0, -1);
      var prLink = window.location.protocol+'//'+window.location.host+"/"+pathArray[3]+"/#project-"+urlkey;
      window.location = window.location.protocol+'//'+window.location.host+"/"+pathArray[3]+"/#project-"+urlkey;
   });
   
   // zkrytí všech a zobrazení prvního
   $('#panel-images .slide-image').css({
      opacity: 0.0
   });
   $('#panel-images .slide-image:first').css({
      opacity: 1.0
   }).addClass('active');
   $('.button-show-image:first').addClass('active');
   slideinterval = setInterval( "slideSwitch()", 6000 );
}

function slideSwitch() {
   if(inArea == true){
      return;
   }
   var $active = $('.button-show-image.active');
   var $next = $active.next();
   if ( $next.length == 0 ) {
      $next = $('#panel-images-buttons .button-show-image:first');
   }
   $next.click();
}

function initNewsScroll(){
   var boxW = $('.news-post').width();
   var $scrollArea = $('#news-list-wrapper');
   var scrollW = $('.news-post').length * boxW;
   var viewW = $('#news-list-wrapper').width();
   
   $('#news-list').width(scrollW+46);
   $('#news-list-wrapper').css('overflow', 'hidden');

   // check anchor

   $('#news-button-n').hide();
   if($('.news-post').length <=3 ){
      $('#news-button-o').hide();
   }

   var scrollQueue = {
      position : 0,
      numItems : $('.news-post').length,
      scroll : function(newest){
         $('#news-button-n').show();
         $('#news-button-o').show();
         
         var hrefPos = 3;
         
         if(newest == true){
            var xmax = this.numItems-(this.position+3);
            if(xmax == 0){ 
               return; 
            } else if(xmax >= 3){
               this.position = this.position+3;
            } else {
               this.position = this.position+xmax;
            }
         } else {
            var xmin = this.position;
            if(xmin == 0){
               return; 
            } else if(xmin >= 3){
               this.position = this.position-3;
            } else {
               this.position = this.position-xmin;
            }
         }
         
         if(this.position+3 >= this.numItems){
            $('#news-button-o').hide();
         }
         
         if(this.position == 0){
            $('#news-button-n').hide();
         }
         
         // change buttons anchors
         $('#news-button-o').attr('href',
            $('#news-button-o').attr('href').toString()
            .replace(/(#pos-)[0-9]+/, "$1"+(this.position).toString())
            );
         $('#news-button-n').attr('href',
            $('#news-button-n').attr('href').toString()
            .replace(/(#pos-)[0-9]+/, "$1"+(this.position).toString())
            );
         $scrollArea.animate({
            scrollLeft: this.position*boxW
            }, {
            duration: 500
         } );
      }
   };
   
   $('#news-button-o').click(function(e){
      e.preventDefault();      
      scrollQueue.scroll(true);
   });

   $('#news-button-n').click(function(e){
      e.preventDefault();
      scrollQueue.scroll(false);
   });
}

$(document).ready(function() {
   $('#main-menu ul a').append($('<span></span>').addClass('decorator').css({
      opacity : 0
   }));
   $('#main-menu ul a').hover(function(){
      $('.decorator', this).animate({
         opacity: 1
      }, 

      {
         duration: 200, 
         queue: false
      });
   }, function(){
      $('.decorator', this).animate({
         opacity: 0
      }, 

      {
         duration: 400, 
         queue: false
      });
   });
   
   $('#page-cnt a, #footer-content a').hover(function(){
      $(this).animate({
         color: '#ff6400'
      }, {
         duration: 400, 
         queue: false
      });
   }, function(){
      $(this).animate({
         color: '#5b5a5a'
      }, {
         duration: 400, 
         queue: false
      });
   });
});

function initSection(){
   $('.section-projects').hide();
   // event zobrazení sekce
   $('.show-section-projects').click(function(){
      $('.section-projects').slideUp(500);
      $('.project-placeholder').slideUp(500).html(null);
      $('.show-section-projects-box').slideDown(500);
      var $link = $(this);
      $link.parent('div').slideUp(200, function(){
         var ids = $link.attr('href').match(/#([a-z-]+)/gi);
         if(ids.length > 0){
            $(ids[0]).slideDown(800);
         }
      })
   });
   // event zobrazení projektu
   $('.project a').click(function(e){
      if(isAdmin){
         return;
      }
      e.preventDefault();
      var href = $(this).attr("href");
      
      var pathArray = href.split( '/' );
      var urlkey = href.substr(href.substr(0, href.length-1).lastIndexOf('/') + 1).slice(0, -1);
      window.location = window.location.protocol+'//'+window.location.host+"/"+pathArray[3]+"/#project-"+urlkey;
            
      var plholder = $(this).parents('article').find('.project-placeholder');
      plholder.slideUp(500, function(){
         $(this).load(href, function(){
            $(this).slideDown(500);
            $('html, body').scrollTop($(this).offset().top-40);
            initGalery();
         });
      });
   });
         
   function initGalery(){
      $("a.view-image").attr('rel', 'gallery').fancybox({
         titlePosition	: 'inside', 
         padding		: 0,
         overlayOpacity : 0.9, 
         overlayColor : '#373535',
         onComplete : function(){
            $('#fancybox-close').text(closeText);
         }
      });
   }
         
   // kontrola jestli se nemá zobrazit
   var str = window.location.href.match(/#([a-z-]+)/gi);
   if(str != null && str.length > 0){
      // je zadána sekce
      str = str.toString().substr(1);
      var $sectionBox = $('#'+str).parents('article');
            
      if(str.indexOf('project') == 0){ // first is project
         $sectionBox.find('.project-placeholder').load($('a', '#'+str).attr('href'), function(){
            initGalery();
            $(this).show();
            $('html, body').scrollTop($(this).offset().top-40);
         });
      }
      $sectionBox.find('.show-section-projects-box').hide();
      $sectionBox.find('.section-projects').show();
   }
}
