/**
 *  Sortiment image loader
 */
$(window).load(function(){      
      $('#loader').fadeOut();
      $('.image-scroller .navigation, .image-scroller .scroll-content, .gallery').fadeIn();
      //$('.image-scroller .scroll-content').fadeIn();
});

/**
 * jQuery BackgroundSwitcher
 * @author Franta Toman
 * @copyright © Franta Toman, 2011+
 *
 */
function backgroundSwitcher(){
    var active = $("#backgrounds .item.active");
    if(active.length == 0){        
        active = $("#backgrounds .item:last");        
    }
    
    var next = active.next().length ? active.next() : $('#backgrounds .item:first');    
    
    var siblings = active.siblings();
    var random = Math.floor( Math.random() * siblings.length );
    var next  = $( siblings[ random ] );
    
    active.addClass('last-active');
   
    active.fadeOut('2000').removeClass('active last-active');
    next.fadeIn('2000');
    next.addClass('active');
    
}

/**
 *  jQuery GalleryBox
 *  @author Franta Toman
 *  @copyright © Franta Toman, 2011+
 */
(function($){
    $.fn.extend({
        galleryBox: function(options){
            var defaults = {
                overlayId: 'gb-overlay',
                imageBoxId: 'gb-active-image',
                navigationId: 'gb-navigation',
                thumbsWrapperId: 'gb-thumbs-wrapper',
                thumbsId: 'gb-thumbs',
                navLeftId: 'gb-nav-left',
                navRightId: 'gb-nav-right',
                contentId: 'gb-content',
                srcDiv: '.gallery',
                externFullscreenCall: ''
            }
            
            var options = $.extend(defaults, options);
            
            /** returns height of page */
            var _getPageHeight = function(){
                return $(document).height();
            };
            
            var _close = function(){
                $('#'+options.contentId).fadeOut('fast', function(){                            
                            $('#' + options.overlayId).fadeOut();
                        });             
            };            
            
            var _centerImageBox = function(){
                var imageBox = $('#'+options.imageBoxId);
                var width = imageBox.children('img').width();
                var left = $(document).width()/2 - width/2; 
                //alert(left);
                imageBox.css({left: left + 'px'});
            };
            
            var _loadImage = function(source){
                
                //_removeImage();
                
                $('#'+options.imageBoxId).children('img').fadeOut('fast');                 
                
                var imageObj = new Image();
                imageObj.src = source;

                $(imageObj).load(function(){                

                    var close = $('#'+options.imageBoxId + ' .close');
                    var imageBox = $('#'+options.imageBoxId);

                    imageBox.children('img').remove();
                    imageBox.append(imageObj); 

                    var width = imageBox.children('img').width();
                    var height = imageBox.children('img').height();     
                    var left = $(document).width()/2 - width/2;                    

                    $('#'+options.imageBoxId).animate({width: width+'px', height: height+'px', left: left + 'px'}, function(){
                        $('#'+options.imageBoxId + ' img').fadeIn();
                        $('#'+options.imageBoxId + ' .close').animate( {left: (width/2 - $(close).width() / 2 ) + 'px'} );                    
                    });       

                });                
            };
            
            /**
             *  Initialization plugin
             */
            var initialize = function(){
                
                // creates content of gallery box
                var content = '<div id="'+options.overlayId+'"></div><div id="'+options.contentId+'"><div id="'+options.imageBoxId+'"><a href="#" class="close"></a><div class="loader"></div></div><div id="'+options.navigationId+'"><div id="'+options.navLeftId+'" class="gb-nav"></div><div id="'+options.navRightId+'" class="gb-nav"></div><div id="'+options.thumbsWrapperId+'"><div id="'+ options.thumbsId +'"></div></div></div></div> ';                
                $('body').append(content);  // append gallery box into document                
                $('#'+options.overlayId).height( _getPageHeight() );                        // set overlay over 100% of height
                
                var thumbsWidth = ($(options.srcDiv).children('a').length * 100 );   // thumbs div width
                var thumbs = $(options.srcDiv).children('a').clone();                // clone thumbs
                $('#'+options.thumbsId).width( thumbsWidth ).append(thumbs).children('a').css({opacity: 1});         // append clone of thumbs
                
                //var thumbsWrapperWidth = $('#'+options.thumbsWrapperId).width();     // wrapper
                
                /* callbacks */                
                $(window).resize( function(){
                    $('#'+options.overlayId).height( _getPageHeight() );        // set overlay over 100% of height
                    _centerImageBox();
                });
                
                $('#gb-active-image .close').bind('click', function(){
                    _close();
                });
                
                $('#gb-thumbs a').click(function(){
                    
                    $( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
                    
                    var imgSrc = $(this).attr('href');
                    _loadImage(imgSrc);                    
                    
                    return false;
                });
                
                $('#'+options.navLeftId).bind('click', function(){
                    var th = $('#' + options.thumbsId);
                    var position = parseInt($(th).css('left'));
                    
                    var width = $('#'+options.thumbsWrapperId).width();
                    var shift = width * 0.8;

                    if( position + shift > 0 ){
                        shift = position * -1 ;
                    }
                    
                    if( position < 0 ){
                        $(th).animate({left: '+=' + shift + 'px'});                                            
                    }                   
                    
                });                
                
                $('#'+options.navRightId).bind('click', function(){
                    var th = $('#' + options.thumbsId);
                    var position = th.position().left;
                    
                    var width = $('#'+options.thumbsWrapperId).width();
                    var shift = width * 0.8;
                    
                    //alert( position );
                    //alert( (shift + position) + ' :::: ' + width );
                    
                    if( (position - shift) > $(th).width()*-1 ){
                        $(th).animate({left: '-=' + shift + 'px'});                                            
                    }                    
                });
                
                $(document).bind('keydown', function(event){
                    //alert(event.keyCode);
                    if (event.keyCode == 27) {                        
                       _close();
                    }
                });
                
            };
            
            var _handleFullscreen = function(_ref){
                
                    if( ($("body").children("#" + options.contentId)).length <= 0 ){
                        initialize();
                    }else{
                        $('#' + options.overlayId).show();
                        $('#' + options.contentId).show();                        
                    }
                    
                    var imgSrc = $(_ref).attr('href');
                    _loadImage(imgSrc);              
            }
            
            return this.each(function(){    
                $(this).children("a").bind('click', function(){
                    $( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
                    _handleFullscreen(this);
                    return false;
                });
                
                if(options.externFullscreenCall != ''){
                    $(options.externFullscreenCall).bind('click', function(){
                        
                        var $obj = ($(options.srcDiv).children('a')).get(0);
                        
                        $( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
                        
                        _handleFullscreen($obj);
                        return false;
                    });
                }
                
            });
            
        }
    });
})(jQuery);

/**
 *  jQuery GallerySlider
 *  @author Franta Toman
 *  @copyright © Franta Toman, 2011+
 */
( function($){
    $.fn.extend({
        gallerySlider: function(options){ 
            
            var defaults = {
                contentCls: '.scroll-content',
                contentWrapperCls: '.scroll-content-wrapper',
                contentView: '.scroll-content-view'
            };            
            var options = $.extend(defaults, options);
            
            var _getContent = function(_reference){
                return $(_reference).children(options.contentWrapperCls).children(options.contentView).children(options.contentCls);
            }
            
            var _initialize = function(_reference){
                var $content = $(_getContent(_reference));                
                var width = $content.children('a').length * 156;                
                $content.css({width: width + 'px'});
            };
            
            var _registerCallbacks = function(_reference){
                $(_reference).children('.navigation').children('.right').bind('click', function(){ 
                
                    var left = parseInt($(options.contentCls).stop(false, true).css('left'));
                    
                    if(isNaN(left)){
                      left = 0;
                    }
                 
                    //alert( ($(_reference).width() + 400) * -1 ); 
                    if( parseInt(left) > ($(options.contentCls).width() - 1000) * -1 ){
                        $(options.contentCls).stop().animate({left: '-=500px'});                    
                    }
                    
                });
                
                $(_reference).children('.navigation').children('.left').bind('click', function(){       
                
                    var left = parseInt($(options.contentCls).stop(false, true).css('left'));

                    if( left - 500 < 0 ){
                        $(options.contentCls).stop(false, true).animate({left: '+=500px'}, function(){
                            if( parseInt($(options.contentCls).css('left')) > 0 ){
                                $(options.contentCls).animate({left: '0px'});
                            }
                        });
                    }
                });
            };
            
            return this.each( function(){
                _initialize(this);
                _registerCallbacks(this);              
            });
        }
    });
} )(jQuery);

$().ready(function() {
    
  /* Background changing */
    
  $("#backgrounds .item").not(":last").hide();
  $( function(){setInterval("backgroundSwitcher()", 10000)});
    
  /* Windows */    
  var myClose=function(hash) {hash.w.fadeOut('2000',function(){hash.o.fadeOut('800', function(){hash.o.remove();});});};
  var myShow=function(hash) {hash.w.fadeIn( '2000')}; 
    
  $('#contacts').jqm({trigger: 'a.contacts', onHide: myClose, onShow: myShow});
  $('#about-us').jqm({trigger: 'a.about-us', onHide: myClose, onShow: myShow});
  $('#partners').jqm({trigger: 'a.partners', onHide: myClose, onShow: myShow});
  $('#showroom').jqm({trigger: 'a.showroom', onHide: myClose, onShow: myShow});
  
  //$("a").galleryBox();
  
  // sortiment hover effect
  $('.sortiment-list .sortiment-item, .scroll-content a, .gallery a').hover(function(){
      $('.sortiment-list .sortiment-item, .scroll-content a, .gallery a').not(this).stop().animate({'opacity': '0.5'}, 'fast');
  }, function(){
      $('.sortiment-list .sortiment-item, .scroll-content a, .gallery a').stop().animate({'opacity': '1'}, 'fast');
  });
  
  $(".gallery").galleryBox();
  $(".scroll-content").galleryBox({srcDiv: '.scroll-content', externFullscreenCall: '.navigation .fullscreen'});
  
  $(".image-scroller").gallerySlider();
  
  /* sortiment menu */
  $('.packer').bind('click', function(){
    var $object = $(this).siblings('ul');
    if($object.length > 0){
        $object.slideToggle('fast');
    }
    return false;
  });
  
  /* lightbox */
  $(".popup").lightBox();
  
});



