/*
 * OptionAccessoires 0.1 (2009-03-11)
 *
 */

$(document).ready(function() {
  var offset = 1;
  var layer_width = $(this).find('div.option_access_layers div.option_layer').width();
  var layer_height = $(this).find('div.option_access_layers div.option_layer').height();
  var layer_count = $('.option_layer').length;
  // remove src attr from images to prevent loading and save value
  
  $('.option_layer:lt(' + (layer_count - 5) + ') img').each(function(){ 
    //$(this).data("img_src", $(this).attr('src')).removeAttr('src');
  });
  
  jQuery.fn.extend({
    wait: function(time, type) {
        time = time || 1000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    },
    
    initializeLayers: function() {
      var scale = 0;
      var left = -20;
      var center = $('body').innerWidth()/2;
      var layer_count = $('div.option_access_layers div.option_layer').length;

      
      $('div.option_access_layers div.option_layer').each(function(i) {
        if( !$(this).hasClass('initialized') ) {
          // first left layer (open)


          if(i === layer_count - 1) {
            $(this).addClass('actualRight');
            image = $(this).find('img');
            // add zoom link
            // thickbox
            if(image.length > 0) {
              $('.option_layers_navigation .right .zoom').each( function(i){
                var big_image = image.attr('src').replace('uploads/pics/','fileadmin/templates/pics/gallery/big_');        

                $(this).attr('href', big_image);
//                $(this).addClass('thickbox');
              });
            }
            $('.option_layers_navigation .right .zoom').addClass('thickbox');
          }

          if(i === layer_count - 2) {
            $(this).animate({left: (center - $(this).width() - (i-1)*offset/2) + 'px'}, {duration:0});  // , top: '+=' + (layer_count/2-i-1)
            image = $(this).find('img');
            // add zoom link
            // thickbox
            if(image.length > 0) {
              $('.option_layers_navigation .left .zoom').each( function(i){
                var big_image = image.attr('src').replace('uploads/pics/','fileadmin/templates/pics/gallery/big_');        
                $(this).attr('href', big_image);
//                $(this).addClass('thickbox');
              });
            }
            $('.option_layers_navigation .left .zoom').addClass('thickbox');
          }
          else
          {
            if($(this).hasClass('left'))
            {
              $(this).css('left', (center + (layer_count/2-i)*offset) + 'px');
              $(this).css('width' ,'0px'); // top: '+=' + ((layer_count/2-i)*offset), 
              $(this).css('z-index',100-i);
              $(this).css('border-width', '0px');
              $(this).css('display', 'none');
              
            }
            else
            {
              $(this).css('left', (center + (layer_count/2-i)*offset) + 'px'); // , top: '+=' + ((layer_count/2-i)*offset);              
            }
            
          }
          $(this).addClass('initialized');
        }
      });
      // thickbox init
      tb_init('.thickbox');

    },
    
    nextLayer: function(direction){
      // load new data (ajax or other layer)
      var image = '';
      $('.option_layer').each(function() { $(this).removeClass('actualRight'); $(this).removeClass('actualLeft'); });
      
      if(direction == 'right') {        
        $(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper:first').find(".left")
            .css('display','block')
            .css('border-width', '1px');

        // scale right side
        $(this).animate({width:0}, {duration: 250, complete:
            function() {
              // scale left side
              old = $(this);
              old.css('border-width','0px');
              $(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper:first').find('.left')                
                .animate({width:layer_width, left: '-=' + layer_width},{duration:250, complete: 
                    function() {
                      // hide old left side
                      // initialize new left side
      	              $(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper:first').find(".left")
                          .css('display','block');
                    }
                });
                         
            }
        });
        
        if($(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper').length > 1)
        {
          $(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper:first').find(".right")
             .addClass('actualRight');
             
        }

        $(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper:first').find(".left")
             .addClass('actualLeft');

        image = $(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper:first').find(".right").find('img');
        // add zoom link
        $('.option_layers_navigation .right .zoom').each( function(i){
          var big_image = image.attr('src').replace('uploads/pics/','fileadmin/templates/pics/gallery/big_');        
          $(this).attr('href', big_image);
          $(this).addClass('thickbox');
        });
        // thickbox
        image = $(this).parents('.option_access_layer_wrapper').prevAll('.option_access_layer_wrapper:first').find(".left").find('img');
        // add zoom link
        $('.option_layers_navigation .left .zoom').each( function(i){
            var big_image = image.attr('src').replace('uploads/pics/','fileadmin/templates/pics/gallery/big_');        
            $(this).attr('href', big_image);
            $(this).addClass('thickbox');
        });

      }
      else
      {
        // scale left side
        $(this)
          .animate({width: 0, left: '+=' + layer_width } , {duration: 250, complete:
            function() {
              // scale right side
              $(this).css('border-width', '0px');
              $(this).parents('.option_access_layer_wrapper').nextAll('.option_access_layer_wrapper:first').find('.right')
                .animate({width: layer_width}, {duration:250, complete:
                    function() {
                    }
                })
                .css('border-width','1px').addClass('actualRight');
//                .click(function(){
//                  $(this).nextLayer('right');
//                });       
            }
        });


        $(this).parents('.option_access_layer_wrapper').nextAll('.option_access_layer_wrapper:first').find(".left")
            .css('display','block')
            .css('border-width', '1px');
       
        if($(this).parents('.option_access_layer_wrapper').nextAll('.option_access_layer_wrapper').length > 1)
        {
          $(this).parents('.option_access_layer_wrapper').nextAll('.option_access_layer_wrapper:first').find(".left")
             .addClass('actualLeft');         
        }     
        
        image = $(this).parents('.option_access_layer_wrapper').nextAll('.option_access_layer_wrapper:first').find(".right").find('img');
        // add zoom link
        $('.option_layers_navigation .right .zoom').each( function(i){
          var big_image = image.attr('src').replace('uploads/pics/','fileadmin/templates/pics/gallery/big_');        
          $(this).attr('href', big_image);
          $(this).addClass('thickbox');
        });
        // thickbox
        image = $(this).parents('.option_access_layer_wrapper').nextAll('.option_access_layer_wrapper:first').find(".left").find('img');
        // add zoom link
        $('.option_layers_navigation .left .zoom').each( function(i){
            var big_image = image.attr('src').replace('uploads/pics/','fileadmin/templates/pics/gallery/big_');        
            $(this).attr('href', big_image);
            $(this).addClass('thickbox');
        });
                            
      }

      // thickbox init
      //tb_init('.thickbox');      
    }    
  // fn.extend
  });

$('.option_access_layers_wrapper').initializeLayers();

// Navigation
//=================

$('.option_layer').click(function() {
  if($(this).hasClass('actualLeft')) {
      $('.actualLeft').nextLayer('left');
  }
  else if($(this).hasClass('actualRight'))
  {  
      $('.actualRight').nextLayer('right');
  }
});

// page right
$('.option_layers_navigation .right .page')
  .click(function() {
    if($('.actualRight').length>0) {
      $('.actualRight').nextLayer('right');
    }
  })
  .hover(
      function(){ 
	$(this).addClass("ui-state-hover"); 
      },
      function(){ 
	$(this).removeClass("ui-state-hover"); 
      }
  );

// zoom right
$('.option_layers_navigation .right .zoom')
  .hover(
      function(){ 
	$(this).addClass("ui-state-hover"); 
      },
      function(){ 
	$(this).removeClass("ui-state-hover"); 
      }
  );

// page left
$('.option_layers_navigation .left .page')
  .click(function() {
    if($('.actualLeft').length>0) {
      $('.actualLeft').nextLayer('left');
    }
  })
  .hover(
      function(){ 
	$(this).addClass("ui-state-hover"); 
      },
      function(){ 
	$(this).removeClass("ui-state-hover"); 
      }
  );

// zoom left
$('.option_layers_navigation .left .zoom')
  .hover(
      function(){ 
	$(this).addClass("ui-state-hover"); 
      },
      function(){ 
	$(this).removeClass("ui-state-hover"); 
      }
  );

});