/*
 * Francesc Galbany 2010-04-20
 * Licensed under the MIT license
 *
 * $Date: 2010-04-28 (Mon, 19 Apr 2010) $
 * $version: 0.3
 *
 */
(function($) {
	$.fn.divescam = function(options) {
		
		var defaults = {
			section: '.integral',
			button:'.boto',
			div: '.finestra',
			hide_effect: 'fade',
			hide_effect_options: {direction:'left'},
			hide_duration: 500,
			hide_mov_duration: 500,
			hide_mov_easing: 'easeOutExpo',
			show_effect: 'fade',
			show_effect_options: {direction:'left'},
			show_duration: 500,
			show_mov_duration: 500,
			show_mov_easing: 'fade',
			show_all_bt: '',
			sel_class: '',
			unsel_class: '',	
			open_coords: {x:0,y:0},
			open_mov: false,
			mnu_group: true,
			mnu_coords: {x:0,y:0},
			mnu_margin: 5,
			mnu_align: 'left',
			auto_hide: false,
			special_sels: {},
			special_unsels: {},
			hide_on_sel: [],		/**Dins del div**/
			show_on_sel: [],		/**A tot arreu**/
			hide_on_sel_g:[],
			extra_animate_sel:{},
			extra_animate_unsel:{},
			etype: 'click'
		};
		
		var options = $.extend(defaults, options);
		
		if (!this) return false;
		
		return this.each(function() {
			var me = $(this)
			var sections = []
			var currSection;

			function showSection(section){
			    if (currSection == section) {
				if (defaults.auto_hide){
				  showAll();
				  currSection = null;
				}
				return;
			    }
			    //console.log('showSection');
			    offsety = 0;
			    for(i=0; i< sections.length;i++){
			      if (sections[i].sid == section.sid) continue;
			      
			      sections[i].bt.removeClass(sections[i].selClass);
			      sections[i].bt.addClass(sections[i].unselClass);
			      if ( ((!sections[i].hidding) || (!defaults.hide_duration)) && (sections[i].div.css('display') != 'none')) {
				sections[i].hidding = true;
				sections[i].div.stop(false,true);
				sections[i].div.hide(defaults.hide_effect,defaults.hide_effect_options,defaults.hide_duration);
			      }
			      if(defaults.mnu_group){
				if (defaults.mnu_align == 'right'){
				  sections[i].bt.animate({left:defaults.mnu_coords.x - sections[i].bt.outerWidth(),top:defaults.mnu_coords.y + offsety, duration:defaults.hide_mov_duration, easing:defaults.hide_mov_easing});
				}else if(defaults.mnu_align == 'left'){
				  sections[i].bt.animate({left:defaults.mnu_coords.x,top:defaults.mnu_coords.y + offsety, duration:defaults.hide_mov_duration, easing:defaults.hide_mov_easing});
				}
				offsety += sections[i].bt.outerHeight() + defaults.mnu_margin;
			      }
			      sections[i].bt.animate(defaults.extra_animate_unsel);
			      for (j=0; j< defaults.hide_on_sel.length; j++){
				  sections[i].sid.find(defaults.hide_on_sel[j]).fadeOut(400);
			      }
				  for (j=0; j< defaults.show_on_sel.length; j++){
				  $(defaults.show_on_sel[j]).fadeIn(400);
			      }
				  for (j=0; j< defaults.hide_on_sel_g.length; j++){
					  $(defaults.hide_on_sel_g[j]).fadeOut(400);
				  }

			     
			    }
			    if(defaults.mnu_group){
			      m_x = section.initBtX;
			      m_y = section.initBtY;
			      if(defaults.open_mov){
				m_x = defaults.open_coords.x;
				m_y = defaults.open_coords.y;
			      }
			      section.bt.animate({left:m_x, top:m_y, duration:defaults.show_mov_duration, easing:defaults.show_mov_easing}, 
			      function(){
				  if (section.div.css('display') == 'none') section.div.show(defaults.show_effect,defaults.show_effect_options,defaults.show_duration);
			      });
				    
			    }else{
			     if (section.div.css('display') == 'none')  section.div.show(defaults.show_effect,defaults.show_effect_options,defaults.show_duration);
			    }
			    section.bt.removeClass(section.unselClass);
			    section.bt.addClass(section.selClass);
			    section.hidding = false;
			    currSection = section;
			    section.bt.animate(defaults.extra_animate_sel);
			    for (j=0; j< defaults.hide_on_sel.length; j++){
				  section.sid.find(defaults.hide_on_sel[j]).fadeOut(400);
			    }
				for (j=0; j< defaults.show_on_sel.length; j++){
				  $(defaults.show_on_sel[j]).fadeIn(400);
			    }
				for (j=0; j< defaults.hide_on_sel_g.length; j++){
				  $(defaults.hide_on_sel_g[j]).fadeOut(400);
			    }
			}

			function showAll(){
			  //console.log('showAll');
			  for(i=0; i<sections.length;i++){
			     if (sections[i].div.css('display') != 'none') sections[i].div.hide(defaults.hide_effect,defaults.hide_effect_options,defaults.hide_duration); 
			     if (defaults.mnu_group) sections[i].bt.animate({left:sections[i].initBtX, top:sections[i].initBtY, duration:defaults.show_mov_duration, easing:defaults.show_mov_easing});
			     sections[i].bt.removeClass(sections[i].selClass);
			     sections[i].bt.removeClass(sections[i].unselClass);
			     sections[i].hidding = false;
			     sections[i].bt.animate(defaults.extra_animate_unsel);
			     for (j=0; j< defaults.hide_on_sel.length; j++){
				  sections[i].sid.find(defaults.hide_on_sel[j]).fadeIn(400);
			     }
				 for (j=0; j< defaults.show_on_sel.length; j++){
				  $(defaults.show_on_sel[j]).fadeOut(400);
			     }
			  }
			  for (j=0; j< defaults.hide_on_sel_g.length; j++){
				  $(defaults.hide_on_sel_g[j]).fadeIn(400);
			  }
			}

			me.find(defaults.section).each(function(){
			    var section;
			    var bt;
			    var div;
			    var found = false;
			    $(this).find(defaults.button).each(function(){
			      bt = $(this);
			      found = true;
			      return false;
			    });
			    if (!found) return;		//continue
			    found = true;

			    $(this).find(defaults.div).each(function(){
			      div = $(this);
			      found = true;
			      return false;
			    });
			    if (!found) return;		//continue

			    var styleSelClass = defaults.sel_class;
			    var styleUnselClass = defaults.unsel_class;
			    
			    for (k in defaults.special_sels){
				if(bt.hasClass(k)) styleSelClass = defaults.special_unsels[k];
			    }
			    for (k in defaults.special_unsels){
				if(bt.hasClass(k)) styleUnselClass = defaults.special_unsels[k];
			    }
// 			    alert(styleUnselClass);
			    section = { sid: $(this), 'bt': bt, 'div':div, 'initBtX':bt.position().left, 'initBtY':bt.position().top, 'hidding':false, unselClass:styleUnselClass, selClass:styleSelClass};
			    sections.push(section);	
			    eval('section.bt.' + defaults.etype + '( function(){showSection(section)} );');
			    //section.bt.click( function(e){alert('e');alert(section.initBtX);showSection(section)} );
			});	
			if (defaults.show_all_bt.length) $(defaults.show_all_bt).click(showAll);
			//showAll();
                });
        };
})(jQuery);
