// Webaholix jQuery Gallery

(function($) {

  $.fn.wxGallery = function(options) {
    var opts = $.extend({}, $.fn.gallery_defaults, options); 
    var object_gallery = $(opts.galleryId);
    var object_tabs = $(opts.galleryId + ' ' + opts.tabsClass);
    var object_media = $(opts.galleryId + ' ' + opts.mediaClass);
    var first_media = opts.firstMedia;
    var current_media_pos = first_media;
    var next_media_timer = null;
    var animating = false;
    var media_max = 1;
    
    // First checks
    
    if (object_tabs.children().length == 0) {
      object_gallery.css('display', 'none');
      alert('wxGallery error: tabs are not defined!');      
      return;
    }
    media_max = object_tabs.children().length;
    
    if (object_media.children().length == 0) {
      object_gallery.css('display', 'none');
      alert('wxGallery error: media are not defined!');         
      return;
    }
    
    if (object_tabs.children().length > object_media.children().length) {
      object_gallery.css('display', 'none');
      alert('wxGallery error: there are more tabs like media!');         
      return;
    }
    
    if (object_tabs.children().length < first_media || first_media < 1) { first_media = 1; } 
    
    current_media_pos = first_media;
    
    // Initialise tabs and media
    
    $.each(object_tabs.children(), function(i, tab){
      $(tab).toggleClass(opts.tabSelectedClass, i + 1 == first_media).
      addClass('tab_controller_' + (i + 1));
    })
    
    $.each(object_media.children(), function(i, media){
      $(media).css('position', 'absolute')
                   .css('z-index', 1000+i)
                   .css('display', i + 1 == first_media ? '' : 'none')
                   .addClass('media_' + (i + 1));
    });
    
    // Prepare timeout code and timeout timer
    
    next_media_timer = setTimeout('jQuery("' + opts.galleryId + ' .tab_controller_' + (first_media == media_max ? 1 : first_media + 1) + '").click()', opts.cyclerTime * 1000);
    
    object_media.css('height', opts.containerHeight).css('width', opts.containerWidth);
    
    // Set functionality to tab controlls
    
    object_tabs.children().click(function(){
      for(t=1;t<=object_tabs.children().length;t++) {
        if ($(this).hasClass('tab_controller_' + t) && t != current_media_pos && !animating) {
          
          clearTimeout(next_media_timer);
          
          animating = true;
          
          current_media = $(opts.galleryId + ' .media_' + current_media_pos);
          next_media = $(opts.galleryId + ' .media_' + t);
          current_tab = $(opts.galleryId + ' .tab_controller_' + current_media_pos);
          next_tab = $(opts.galleryId + ' .tab_controller_' + t);
          
          current_tab.fadeTo(opts.fadingSpeed, 0.50);
          current_media.css('z-index', 999 + current_media_pos);
          next_media.css('z-index', 1000000 + t).fadeIn(opts.fadingSpeed, function(){
            current_media.css('display', 'none');
  
            next_media_timer = setTimeout('jQuery("' + opts.galleryId + ' .tab_controller_' + (t == media_max ? 1 : t + 1) + '").click()', opts.cyclerTime * 1000);
            
            current_tab.removeClass(opts.tabSelectedClass);
            next_tab.addClass(opts.tabSelectedClass);
            
            current_tab.fadeTo("fast", 1.0, function(){
              animating = false;
            });
          });
      
          current_media_pos = t;
          
          break;
        }
      }
    });
    
  };
  
  $.fn.gallery_defaults = {
    galleryId: "#gallery", //ID of gallery wraper
    tabsClass: ".tabs", //Class of tabs controll wraper
    mediaClass: ".media", //Class of media wraper
    firstMedia: 1, //Integer value of first media displayed
    tabSelectedClass: "tab_selected",
    cyclerTime: 0.5,
    fadingSpeed: "normal",
    containerWidth: "100%",
    containerHeight: "200px"
  }

})(jQuery);

// Webaholix jQuery Gallery

/*
(function($) {

  jQuery.fn.wxGallery = function(options) {
    var opts = jQuery.extend({}, jQuery.fn.gallery_defaults, options); 
    var object_gallery = jQuery(opts.galleryId);
    var object_tabs = jQuery(opts.galleryId + ' ' + opts.tabsClass);
    var object_media = jQuery(opts.galleryId + ' ' + opts.mediaClass);
    var first_media = opts.firstMedia;
    var current_media_pos = first_media;
    var next_media_timer = null;
    var animating = false;
    var media_max = 1;
    
    // First checks
    
    if (object_tabs.children().length == 0) {
      object_gallery.css('display', 'none');
      alert('wxGallery error: tabs are not defined!');      
      return;
    }
    media_max = object_tabs.children().length;
    
    if (object_media.children().length == 0) {
      object_gallery.css('display', 'none');
      alert('wxGallery error: media are not defined!');         
      return;
    }
    
    if (object_tabs.children().length > object_media.children().length) {
      object_gallery.css('display', 'none');
      alert('wxGallery error: there are more tabs like media!');         
      return;
    }
    
    if (object_tabs.children().length < first_media || first_media < 1) { first_media = 1; } 
    
    current_media_pos = first_media;
    
    // Initialise tabs and media
    
    jQuery.each(object_tabs.children(), function(i, tab){
      jQuery(tab).toggleClass(opts.tabSelectedClass, i + 1 == first_media).
      addClass('tab_controller_' + (i + 1));
    })
    
    jQuery.each(object_media.children(), function(i, media){
      jQuery(media).css('position', 'absolute')
                   .css('z-index', 1000+i)
                   .css('display', i + 1 == first_media ? '' : 'none')
                   .addClass('media_' + (i + 1));
    });
    
    // Prepare timeout code and timeout timer
    
    next_media_timer = setTimeout('jQuery("' + opts.galleryId + ' .tab_controller_' + (first_media == media_max ? 1 : first_media + 1) + '").click()', opts.cyclerTime * 1000);
    
    object_media.css('height', opts.containerHeight).css('width', opts.containerWidth);
    
    // Set functionality to tab controlls
    
    object_tabs.children().click(function(){
      for(t=1;t<=object_tabs.children().length;t++) {
        if (jQuery(this).hasClass('tab_controller_' + t) && t != current_media_pos && !animating) {
          
          clearTimeout(next_media_timer);
          
          animating = true;
          
          current_media = jQuery(opts.galleryId + ' .media_' + current_media_pos);
          next_media = jQuery(opts.galleryId + ' .media_' + t);
          current_tab = jQuery(opts.galleryId + ' .tab_controller_' + current_media_pos);
          next_tab = jQuery(opts.galleryId + ' .tab_controller_' + t);
          
          current_tab.fadeTo(opts.fadingSpeed, 0.50);
          current_media.css('z-index', 999 + current_media_pos);
          next_media.css('z-index', 1000000 + t).fadeIn(opts.fadingSpeed, function(){
            current_media.css('display', 'none');
  
            next_media_timer = setTimeout('jQuery("' + opts.galleryId + ' .tab_controller_' + (t == media_max ? 1 : t + 1) + '").click()', opts.cyclerTime * 1000);
            
            current_tab.removeClass(opts.tabSelectedClass);
            next_tab.addClass(opts.tabSelectedClass);
            
            current_tab.fadeTo("fast", 1.0, function(){
              animating = false;
            });
          });
      
          current_media_pos = t;
          
          break;
        }
      }
    });
    
  };
  
  jQuery.fn.gallery_defaults = {
    galleryId: "#gallery", //ID of gallery wraper
    tabsClass: ".tabs", //Class of tabs controll wraper
    mediaClass: ".media", //Class of media wraper
    firstMedia: 1, //Integer value of first media displayed
    tabSelectedClass: "tab_selected",
    cyclerTime: 0.5,
    fadingSpeed: "normal",
    containerWidth: "100%",
    containerHeight: "200px"
  }

})(jQuery);
*/
