/*
Name: jQuery List Control Plugin
Version: 0.1
*/

(function($){  
	
	// default settings
	var defaults = {
		//callurl:				'"cadillac/services/selectEvent.cfc?method=setCurrentEvent&value="+event_id',
		callurl:				'"calls/test.html?eventid="+event_id',
		popdelay:				1000,
		descr_maxheight:		200,
		list_maxheight:			200,
		descr_minheight:		0,
		list_minheight:			20,
		accordEaseDuration: 	1000,
		accordEaseFunction: 	"easeInOutExpo"
	};
	
	// Properties
	var 	myoptions, 	// plugin properties
			mylist, 	// list container object
			mydescr;	// descr container object

	var mymethods = {
		
		// initialize everything
		init : function( options ) { 
			//alert('list init');
			myoptions = $.extend(defaults, options);
			mylist=$(this).find('.list2');
			mydescr=$(this).find('.list1');
			mybutton=$(this).find('.button a');
			if (mybutton.length==0) {$.fn.listcontrol.error('Button not found');return}
			mybutton.unbind('click'); // if it will be initializing twice.
			mybutton.click(function(e){
				var list=$(this).parent().parent().parent().parent();	
				list.listcontrol('showhide');
				return false;
			});
			
			// highlight odd lines
			mylist.find(".location:odd").addClass('odd');
			
			// hover handler 
			mylist.find(".location").mouseenter(function () {
				var a=$(this);
				
				// find event id
				if (a.parent().is('.event')) {
					var event_id=a.parent().attr('rel');										
					event_id=String(event_id).slice(6);
					if (event_id.length>0) {
						a.addClass('hover');
						var my_event=event_id;
						if ($('#event'+event_id+' .popup:visible').length==0) {
								
							$('.popup:visible').fadeOut();
							a.animate({opacity: 1},myoptions.popupdelay,function () {
								
								 //alert(my_event+'|'+event_id);
								 if ($(this).is('.hover')) {
									//alert($(this).attr('id'));
									
									$(this).listcontrol('popup', $(this));
								}
							});
						}
					}
				}
				/*setTimeout(function() {
					if (a.is('.hover')) {
						a.;
					}
					
				}, myoptions.popdelay);
				*/
			});
			
			/*($('#slider').mouseover(function(){
				alert(1);
			});*/
			
			
			
			mylist.find(".location").mouseleave(function () {
				
				if ($(this).parent().is('.event')) {
					var event_id=$(this).parent().attr('rel');										
					event_id=String(event_id).slice(6);
					$(this).removeClass('hover');					
				}
			});
			
			// hide popup
			mylist.find (".popup").mouseleave(function() {
				$(this).fadeOut();
			});
			
			// 
			mylist.mouseleave(function() {
				$(this).find('.popup:visible').fadeOut();
			});
			
			// click handler
			//alert('init');
			//mylist.find(".state_trigger,.view_handled").unbind('click');
			mylist.find(".state_trigger,.view_handled").bind('click',function() {
				var event_id='';
				var mode=1;
				var a=($(this));
				if ($(this).parent().is('.event')) {
					var li=$(this).parent();
					event_id=li.attr('rel');										
					event_id=String(event_id).slice(6);
	
				}else if ($(this).parent().parent().is('.event')){ // We are in the trigger inside popup
					var li=$(this).parent().parent();
					//alert(1);
					if (a.is('.view_handled')) {
						mode=2;
					}
					event_id=li.attr('rel');										
					event_id=String(event_id).slice(6);					
				}	
				if (event_id!='') {
					var url=eval(myoptions.callurl);
					$.ajax({
					url: url,					
					context: document.body,
					success: function(data){
						var myData = $.parseJSON(data);
						if (myData.status=='1') {
							
							$(this).find('.popup:visible').hide();
							if (mode==1) { // standard trigger
								var id=a.attr('id');
								$.fn.econtrol.triggerhash(id);
							} else	{ // trigger inside the view
								$.fn.econtrol.mytrigger6(a,event_id);
							}
							
							//alert(1);
							
							
							//alert(1);
							//alert(event_id);
							
							//$(this).addClass("done");
						} else {
							$.fn.listcontrol.error('Event ID ' + event_id + ' not set');
						}
					},
					error: function(jqXHR, textStatus, errorThrown) {
						$.fn.listcontrol.error('Error while settin Event ID ' + event_id );
					}
					});		
				}
				return false;
			});
			

		},
		
	
		// popup
		popup : function(a) {
			var li=a.parent();
			if (li.length==0) { return;}
			var div=li.find('.popup');
			if (div.length==0) {return;}
			//if (div.is(':visible')) {div.fadeOut();}
			div.fadeIn();
			//alert(1);
		},
		
		// show or hide event list control
		showhide : function () {	
			var list=$(this).find('.list2');
			var descr=$(this).find('.list1');
			//alert(descr.parent().attr('class'));
			if (list.is('.visible')) { // hide list
				
				descr.animate({ height: myoptions.descr_maxheight }, myoptions.accordEaseDuration, myoptions.accordEaseFunction);
				list.animate({ height: myoptions.list_minheight }, myoptions.accordEaseDuration, myoptions.accordEaseFunction);
				list.removeClass('visible');
				
			}else {	// show list
				descr.animate({ height: myoptions.descr_minheight }, myoptions.accordEaseDuration, myoptions.accordEaseFunction);
				list.animate({ height: myoptions.list_maxheight }, myoptions.accordEaseDuration, myoptions.accordEaseFunction);
				list.addClass('visible');
			}
			//well_url.slide=targetPanel;
		}
	}
	
	// we can call methods like $('div').listcontrol('methodname')
	$.fn.listcontrol = function( method ) {    
		if ( mymethods[method] ) {
		  return mymethods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
		  return mymethods.init.apply( this, arguments );
		} else {
		  $.fn.listcontrol.error( 'Method ' +  method + ' does not exist on jQuery.econtrol' );
		}   
	}
	
	// Error handler
	$.fn.listcontrol.error =function(msg) {
		alert(msg);
	}
	
})(jQuery); 

