// Slide Set Builder Plugin
// Author:	Chris Wolf
// BBNC: 	6.20
// Created:	11/16/2010

/* TO DO
1. add vertical scrolling option for auto qty
*/

(function($){
 $.fn.SlideSet = function(options) {
	 
  var defaults = {
	 tabs: 'thumb',   // 'thumb' uses image; 'tab' uses text; 'browse' uses left and right arrows; 'none' does not build tabs
	 prev: '&laquo;', // text for 'previous' button
	 next: '&raquo;',  // text for 'next' button
	 display: 'tabs',
	 tabParams: {
	  effect: "fade",
	  fadeOutSpeed: "slow",
	  rotate: true
     },
	 slideshowParams: {
	  autoplay: false,
	  clickable: false
     },
	 scrollParams: {
	  vertical: false
	 },
	 autoscroll: {
	  autoplay: false 
	 },
	 showTabNav: true,
	 tabNavPos: 'top',
	 tabQty: 'auto'
  };
  var options = $.extend(defaults, options);
  
  var tab = options.tabs;
  var prev = options.prev;
  var next = options.next;
  var selector = $(this).selector;
      var getPane = selector.split(' ');
  var pane = getPane[getPane.length-1].replace('.','');
  var tallest = 0;
  var widest = 0;
  var display = options.display;
  var tabParams = options.tabParams;
  var slideshowParams = options.slideshowParams;
  var nav = options.tabNavPos;
  var num = options.tabQty;
  var scrollParams = options.scrollParams;
  var autoScroll = options.autoscroll;
   
 $(this).each(function(){

  if (isEditView) {
   var admPane = $(this).parent().parent();
   $(this).removeClass(pane).addClass('ogPane');
   admPane.addClass(pane);
   if ($(admPane).prev(selector).length == 0) {
    $(admPane).addClass('first');	
   }
  } else {

   if ($(this).prev(selector).length == 0) {
    $(this).addClass('first');	
   }
  }
 });
  
 $(selector+'.first').each(function(i){
      i = i+1;
	  $(this).addClass(pane+'-set-'+i);
 	  $(this).nextAll(selector).addClass(pane+'-set-'+i);
	  $('.'+pane+'-set-'+i).wrapAll('<div class="'+pane+'Set '+display+'"/>');
 });
  
// $('.'+pane+'Nav').each(function(){$(this).remove();});
// $('.'+pane+'s').each(function(){$(this).remove();});
  
	
  return $('.'+pane+'Set').each(function(i,thisSet) {
	  
   var thisID = i;
	       	
   $(this).prepend('<div class="'+pane+'Nav" /><div class="'+pane+'s" />');
   $('.'+pane+'Nav',thisSet).append('<span class="navItems" />');
 
   $('.'+pane,thisSet).each(function(i){
	i = i+1;
	var specialClass = $(this).attr('class').replace(pane,'');
    if (isEditView) {
		var thisPane = $('.ogPane',this);
	} else {
		var thisPane = $(this);
	}	  

	if (tab == 'thumb') {
		var tabImg = $('img:first',thisPane).attr('src');
		var tabContent = '<img src="'+tabImg+'"/>';
	} else if ((tab == 'tab') && ($('.'+tab,thisPane).length > 0)) {
		var tabTxt = $('.'+tab,thisPane).text();
		var tabContent = tabTxt;
		$('.'+tab,thisPane).remove();
	} else {
		var tabContent = i;
	}
	
    if ((isEditView) && (display == 'slider')) {
	 $(this).appendTo('.'+pane+'s',thisSet).wrap('<div class="item item-'+i+' '+specialClass+'"><div class="gutter"/></div>');
	} else {
     $('.'+pane+'s',thisSet).append('<div class="item item-'+i+' '+specialClass+'"><div class="gutter"/></div>');
	}
	
    if (($(thisPane).children('tr').length > 0) || ($(thisPane).children('tbody').length > 0)) {
     $('td',thisPane).each(function(){
 	  var tdClass = $(this).attr('class');
	  var tdHTML = $(this).html();
	  $('.item-'+i+' .gutter',thisSet).append('<div class="'+tdClass+'">'+tdHTML+'</div>');
	 });
	} else {
	 $(thisPane).children().each(function(){
	  $('.item-'+i+' .gutter',thisSet).append(this); 
	 });
	};
		   
    $(thisPane).remove(); 	
	
	if (display != 'slider') {
     if (isEditView) {
  	  $(this).appendTo('.navItems:eq('+thisID+')').append(tabContent).wrap('<a href="#" class="'+tab+'"/>');
     } else {
      $('.navItems',thisSet).append('<a href="#" class="'+tab+'">'+tabContent+'</a>');
     }
	};

    $('img[id$=imgPartGlyph]',thisSet).css('width','auto').css('min-width','inherit');
	
   });
   $('.navItems',thisSet).append('<div style="clear:both;font-size:0;line-height:0"/>');


   var tallest = 0;
   var widest = 0;
   
   $('.item',this).each(function(){
	   	    
   	if ( $(this).outerHeight(true) > tallest ) {
      tallest = $(this).outerHeight(true);
	  //if (display == 'tabs') { tallest = tallest+24 }
      $('.'+pane+'s',thisSet).height(tallest);
	};
	
   	if ( $(this).outerWidth(true) > widest ) {
      widest = $(this).outerWidth(true);
	};
	
	
   /*if (widest > tallest) {
	$('.slide img').css('width','100%').css('height','auto');   
   } else {
	$('.slide img').css('width','auto').css('height','100%');   	   
   }

   */
    if (display != 'slider') {
     $(this).width(widest).css('position','absolute').hide();
	} else {
	 $(this).width(widest);
	}
   });
   
   
   if (tab == 'browse') {
     $('.navItems',thisSet).hide();
   } else if ((tab == 'none') || (display == 'slider')) {
     $('.'+pane+'Nav',thisSet).remove();		
   }
   
   if (display == 'tabs') {
	$('div.'+pane+'Nav',thisSet).tabs('.'+pane+'s > .item', tabParams);
   } else if ((display == 'slideshow') || (display == 'album')) {
    $('.'+pane+'Nav',thisSet).before('<div class="slideshownav"><span class="backward slidenav">'+prev+'</span><span class="forward slidenav">'+next+'</span></div>');   
	$('div.'+pane+'Nav',thisSet).tabs('.'+pane+'s > .item', tabParams).slideshow(slideshowParams);
   };

   if ((display == 'slider') || (display == 'album')) {

    if (display == 'album') {
     var tabSelector = '.'+tab;
	} else if (display == 'slider') {
     var tabSelector = '.item';
	}


	var selContainer = $(tabSelector,thisSet).parent();
	selContainer.before('<div class="pageSet"/>');
	var pageSet = selContainer.prev('.pageSet');
	pageSet.append('<a class="prev browse left">&laquo;</a><div class="wrapItems"><div class="items"/></div><a class="next browse right">&raquo;</a>');
	
	if (nav == 'top') {
		pageSet.prepend('<div class="navi"/>');
	} else if (nav == 'bottom') {
		pageSet.append('<div class="navi"/>');
	}

	var selWidth = parseInt($(tabSelector,thisSet).css('width'));
    var selOutWidth = $(tabSelector,thisSet).outerWidth(true);
	if (display == 'slider') {
		var selOutHeight = tallest;
	} else {
        var selOutHeight = $(tabSelector,thisSet).outerHeight(true);
	}
	var selMargin = parseInt((selOutWidth-selWidth));
	var conWidth = $('.wrapItems',thisSet).width();
	var selAmt = $(tabSelector,thisSet).length;
	
	if (num == 'auto') {
		var qty = Math.floor(conWidth/selOutWidth);
	} else {
		var qty = num; 
	}
	
    while($(tabSelector,selContainer).length) {
     var newContainer = $('<div class="set"/>')
        .appendTo('.items',pageSet);

     $(tabSelector+':lt('+qty+')',selContainer).appendTo(newContainer);
    }

    $('.set',pageSet).each(function(i){
   	 $(this).addClass('#'+i);
    });
	
    selContainer.remove();
	 
    $('.set',pageSet)
	 .css('width',selOutWidth*qty)
	 .css('height',selOutHeight)
	 .css('padding',selMargin/2);

	 if ($('.set',thisSet).length < 2) {
		 $('.browse',thisSet).css('visibility','hidden').addClass('disabled');
		 $('.navi',thisSet).hide();
	 }

    $('.wrapItems',pageSet)
 	 .css('width',selOutWidth*qty+selMargin)
	 .css('height',selOutHeight+selMargin);

    $('.items',pageSet)
	 .css('width',(selOutWidth*selAmt)*2);

    var scroller = $('.wrapItems',pageSet);
	scroller.scrollable(scrollParams).autoscroll(autoScroll);
    if ($('.navi').length > 0) {
	 scroller.navigator();
	}
	
	if (display == 'album') {
 	 var tabsAPI = $('div.'+pane+'Nav',thisSet).data('tabs');
     var scrollAPI = $('.wrapItems',thisSet).data('scrollable');

     tabsAPI.onClick(function(){
	  var slideIdx = this.getIndex();
	  var currScroll = $(tabSelector).eq(slideIdx).parent();
	  scrollAPI.seekTo(currScroll,'fast');
     });
	}
 
   }
   	 
	 
		
  }); // end execution
 }; // end function
})(jQuery); // end plugin
