var panel = null;
var panelDisplayStatus = null;
var panelDisplayTimer = null;

var panelContentTimer = null;

function setPanelDisplayStatus(id){
	panelDisplayTimer = null;
	if(!panelDisplayStatus){
		/*if(navigator.appName == "Opera"){
			$("#panel-"+id).css("display","none");
		}
		else{*/
			$("#panel-"+id).fadeOut("fast");
		//}
	}
}

function setOptionsEvents(type){
	panelContentTimer = null;
	switch(type){
		case "/notices/crew":
			setCrewNoticesOptions();
			break;
		case "/notices/gathering":
			setEventNoticesOptions();
			break;
	}
}

function setCrewNoticesOptions(){
	
	
	for(var c=0;document.getElementById("joincrew"+String(c)) != undefined && document.getElementById("joincrew"+String(c)) != null; c++){
		
		$("#joincrew"+String(c)).click(function(){
			var crew_id = $(this).attr('crew_id');
			var parent = $(this).parent();
			$(parent).css('text-align','center');
			$(parent).html('<img src="/images/ajax-loader-black.gif" />');
			
			$.post('/crew/' + crew_id + '/join?callback=yes', function(data){
				if(data.code == 9006)
				{
					
					$('#notice_crews').html(parseInt($("#notice_crews").html()) - 1);
					if(parseInt($("#notice_crews").html()) == 0){
						$("#panel-crew").fadeOut('fast');
						$("#notice_crews").fadeOut('fast');
					}
					else{
						$('#invite-crew-' + crew_id).slideUp(500);
					}
				}
				else{
					$(parent).html('<span style="color:red;">Oops! An error occured!</span>');
				}	
			});
		});

		$("#ignorecrew"+String(c)).click(function(){
			var crew_id = $(this).attr('crew_id');
			var parent = $(this).parent();
			$(parent).css('text-align','center');
			$(parent).html('<img src="/images/ajax-loader-black.gif" />');

			$.post('/crew/' + crew_id + '/ignore', function(data){
				if(data.code == 9008)
				{
					
					$('#notice_crews').html(parseInt($("#notice_crews").html()) - 1);
					if(parseInt($("#notice_crews").html()) == 0){
						$("#panel-crew").fadeOut('fast');
						$("#notice_crews").fadeOut('fast');
					}
					else{
						$('#invite-crew-' + crew_id).slideUp(500);
					}
				}
				else{
					$(parent).html('<span style="color:red;">Oops! An error occured!</span>');
				}
			});
		});
	}
	
}

function setEventNoticesOptions(){
	
	for(var c=0;document.getElementById("joingathering"+String(c)) != undefined && document.getElementById("joingathering"+String(c)) != null;c++){
		
		$("#joingathering"+String(c)).click(function(){

			var gathering_id = $(this).attr('gathering_id');
			var parent = $(this).parent();
			$(parent).css('text-align','center');
			$(parent).html('<img src="/images/ajax-loader-black.gif" />');
			
			$.post('/event/' + gathering_id + '/1?callback=yes', function(data){
				if(data.code == 10011)
				{
					
					$('#notice_gatherings').html(parseInt($("#notice_gatherings").html()) - 1);
					if(parseInt($("#notice_gatherings").html()) == 0){
						$("#panel-gathering").fadeOut('fast');
						$("#notice_gatherings").fadeOut('fast');
					}
					else{
						$('#invite-gathering-' + gathering_id).slideUp(500);
					}
				}
				else{
					$(parent).html('<span style="color:red;">Oops! An error occured!</span>');
				}	
			});
		});

		$("#ignoregathering"+String(c)).click(function(){
			var gathering_id = $(this).attr('gathering_id');
			var parent = $(this).parent();
			$(parent).css('text-align','center');
			$(parent).html('<img src="/images/ajax-loader-black.gif" />');

			$.post('/event/' + gathering_id + '/3', function(data){
				if(data.code == 10011)
				{
					
					$('#notice_gatherings').html(parseInt($("#notice_gatherings").html()) - 1);
					if(parseInt($("#notice_gatherings").html()) == 0){
						$("#panel-gathering").fadeOut('fast');
						$("#notice_gatherings").fadeOut('fast');
					}
					else{
						$('#invite-gathering-' + gathering_id).slideUp(500);
					}
				}
				else{
					$(parent).html('<span style="color:red;">Oops! An error occured!</span>');
				}
			});
		});

		$('#maybegathering'+String(c)).click(function(){
			var gathering_id = $(this).attr('gathering_id');
			var parent = $(this).parent();
			$(parent).css('text-align','center');
			$(parent).html('<img src="/images/ajax-loader-black.gif" />');

			$.post('/event/' + gathering_id + '/2', function(data){
				if(data.code == 10011)
				{
					
					$('#notice_gatherings').html(parseInt($("#notice_gatherings").html()) - 1);
					if(parseInt($("#notice_gatherings").html()) == 0){
						$("#panel-gathering").fadeOut('fast');
						$("#notice_gatherings").fadeOut('fast');
					}
					else{
						$('#invite-gathering-' + gathering_id).slideUp(500);
					}
				}
				else{
					$(parent).html('<span style="color:red;">Oops! An error occured!</span>');
				}
			});
		});
	}
}

$(document).ready(function(){
	function isset (variable) {
	  return (typeof variable != 'undefined');
	}
	
	(function($){
		
		$.fn.setpanel = function(options) {
			
			var button = $(this);
			
			var defaults = null;
			if(options.page == "/notices/crew"){
				defaults = {
					autosize: true,
					width: -62,
					height: button.height()+2,
					shown: false,
					method: 'get',
					post: null,
					position: 'left',
					type: 'crew'
				};
			}
			else if(options.page == "/notices/gathering"){
				defaults = {
					autosize: true,
					width: -60,
					height: button.height()+2,
					shown: false,
					method: 'get',
					post: null,
					position: 'left',
					type: 'gathering'
				};
			}
			
			var options = $.extend(defaults, options);
			
		
			//var panel = $('<div id="panel-' + options.type + '" class="notice-panel"></div>');
			var panel = $("#panel-"+options.type);
			
			if(isset(options.page))
			{
				
				
				if(parseInt($("#notice_"+options.type+"s").html()) != 0){
					$("#notice_"+options.type+"s").fadeIn("1000");
				}
				
				setOptionsEvents(options.page);
				
				/* Hover action */
				$(this).hover(
					function(){
							panelDisplayStatus = true;
							
							
							if(!options.autosize)
								$(panel).css("width", options.width).css("height", options.height);
							
							var position = button.offset();
							
							
							$(panel).css("position","absolute");
							$(panel).css("top", options.height);
							$(panel).css("left", options.width);
							
							
							/*if(navigator.appName == "Opera"){
								$(panel).css("display","block");
							}
							else{*/
								$(panel).fadeIn("fast",function(e){
									//do something on call back
								})
							//}
							
					},
					function(){
						panelDisplayStatus = false;
						panelDisplayTimer = window.setInterval("setPanelDisplayStatus('"+options.type+"')","200");
					}
				);
				
				$(panel).hover(function(){
					panelDisplayStatus = true;
				},
				function(){
					panelDisplayStatus = false;
					panelDisplayTimer = window.setInterval("setPanelDisplayStatus('"+options.type+"')","200");
				});
			}
			
			
		};
	
	})(jQuery);
});
