/*
 * Copyright (c) 2009 Massimiliano Balestrieri
 * 
 * $Date: 2009-10-01 15:07:41 +0200 (gio, 01 ott 2009) $
 * $Rev: 197 $
 * @requires jQuery v1.3.2
 * 
 * Copyright (c) 2008 Massimiliano Balestrieri
 * Examples and docs at: http://maxb.net/blog/
 * Licensed GPL licenses:
 * http://www.gnu.org/licenses/gpl.html
 */

;(function($) {


////////////////////////////////////////////////////////////////////////////////
//========== BOX A SCOMPARSA ===================================================
////////////////////////////////////////////////////////////////////////////////

var YTogglePanel = {
	build  : function(options){

		
		var _options = $.extend({
			link 		  : 'span',
			pattern_panel : 'togglepanel_',
			pattern_link  : 'togglelink_',
			pattern_handle: 'toggle_',
			label_on	  : 'Apri',
			label_off	  : 'Chiudi',
			open_onstart  : '.toggle_open',
			hide_onstart  : '.toggle_close',
			menu 		  : false,
			menu2 		  : false,
			menu_on 	  : 'visualizza tutte le sezioni',
			menu_off 	  : 'nascondi tutte le sezioni',
			menu_on_class : 'visualizza',
			menu_off_class: 'nascondi',
			menu_sep	  : '&nbsp;|&nbsp;',
			after_click	  : false
		}, options);

		return this.each(function(){
			
			var that = this;
			//alternate
			var _is_alternate = $(that).is(".toggle_alternate");
			
			var _links = $(_options.link, that);
			
			if(_options.menu)
				_menu();
			
			if(_options.menu2)
				_menu2();
			
			_auto_hide();
			
			_links.each(function(){
				add_link(this);
			});		
			
		    function _auto_hide(){
				$(_options.hide_onstart).hide();
			}

			function add_link (el) {

			    //se trova l'id
				if(!(el.id && el.id.indexOf(_options.pattern_handle) !== -1))
					return;

				if($(el).is(".ytogglepanel_handle")){
    			    //console.log("gia usato");
    			    return;
				}else{
				   $(el).addClass("ytogglepanel_handle");				
				}
					    	
				var _target = _link_to_target(el);
			    var _id = _uid(el.id);
				var _link = $('<a class="toggle_handle" href="#" id="'+ _options.pattern_link + _id + '"></a>');
				
				_link.bind("text", function(){
					//	console.log($(this).text() == _options.label_off);
					//$(this).text() == _options.label_off ? $(this).text(_options.label_on) : $(this).text(_options.label_off); 	
					
					var _c1 = _clean_html_for_ie($(this).html());
					var _c2 = _clean_html_for_ie(_options.label_off);
					var _flag = _c1 == _c2;
					
					if(_flag){
						$(this)
						.html(_options.label_on);
					} else{
						$(this)
						.html(_options.label_off);
					}  
					//$(this).after(_c1 + ' ' + _c2);
					if(_options.after_click)
						_options.after_click(this, _flag);
					
					
					function _clean_html_for_ie(str){
						str = str.toString().toLowerCase().replace(/\"/g, '');
						return str;
					}	
						
				});
				
				_link.bind("apri",function(){
					$("#" + _target).show();	
				    $(this).html(_options.label_off);
				    return false;
				});
				_link.bind("chiudi", function(){
					//console.log(_target)
					$("#" + _target).hide();	
				    $(this).html(_options.label_on);
				    return false;
				});
				
				_link.click(function(){
					if(_is_alternate){
						$('.toggle_handle',that).not(this).trigger("chiudi").html(_options.label_on);
						//console.log(_links)
						$(this).trigger("apri").html(_options.label_off);
					}else{
						$("#" + _target).toggle();	
					    $(this).trigger("text");	
					}
				    return false;
				});
				
				if($("#" + _target).css('display') != 'none'){
					_link.html(_options.label_off);
				}else{
					_link.html(_options.label_on);
				}
				
				//.empty()
				$(el)
				.append(_link);
			}
			
			function _link_to_target(el){
				var _id = _uid(el.id);
				return _options.pattern_panel + _id;
			}
			
			function _uid(str){
				str = str.replace(_options.pattern_panel,"");
				str = str.replace(_options.pattern_link,"");
				str = str.replace(_options.pattern_handle,"");
				return str;
			}
			
			function _menu(){
				
				var _start = $(_options.menu).hasClass("parti_chiuso");
				var _c1 = _start ? _options.menu_off_class : _options.menu_on_class;
				var _c2 = _start ? _options.menu_on : _options.menu_off;
				
				var _menu = '<a href="#" class="' + _c1 + '">' + _c2 + '</a>';
				$(_options.menu, that).html(_menu);

				var _handles = $(_options.menu, that).find("a");

				_handles.each(function(){
					$(this).click(function(){
						var _flag = $(this).text() == _options.menu_on;
						var _h = $(".toggle_handle", that);
						var _handle = this;
						_h.each(function(){
							if(_flag){
								$(_handles).text(_options.menu_off);
								$(_handles).removeClass(_options.menu_off_class).addClass(_options.menu_on_class);
								_h.trigger("apri");
							}else{
								$(_handles).text(_options.menu_on);
								$(_handles).removeClass(_options.menu_on_class).addClass(_options.menu_off_class);
								_h.trigger("chiudi");
							}
						//	$(this).trigger("click");
						});
						//alert(_h);
						return false;//TODO: sistemare l'attivazione di questo plugin
					});
				});
		
			}
			
			function _menu2(){
				var _menu_on = '<a href="#" class="'+_options.menu_on_class+'">' + _options.menu_on + '</a>';
				var _menu_off = '<a href="#" class="'+_options.menu_off_class+'">' + _options.menu_off + '</a>';
				var _sep = _options.menu_sep;
				$(_options.menu2, that).html(_menu_on + _sep + _menu_off);
				
				$(_options.menu2, that).each(function(){
					var _handles = $(this).find("a");
					
					_handles.eq(0).click(function(){
						_click(true);
					});
					_handles.eq(1).click(function(){
						_click(false);
					});
				});
				var _click = function(flag){
					var _h = $(".toggle_handle");
					_h.each(function(){
						if(flag){
							_h.trigger("apri");
						}else{
							_h.trigger("chiudi");
						}
					//	$(this).trigger("click");
					});
					//alert(_h);
					return false;
				};
			}
			
		});
		
		
	}	
	
};

$.fn.ytogglepanel = YTogglePanel.build;

})(jQuery);
