var SearchGrid = {
	init: function(){
		SearchGrid.urlTo='';
		SearchGrid.appendTo='';
	},
	showTwitter: function(id){
		if(jsLoginStatus=='203'){
			SearchGrid.showMessage("KO",CalendarLocalization.NO_LOGIN_FOR_ADD_MESSAGE);
			return false;
		}		
		var id;
		jQuery.ajax({
			url: '/event/act/?type=twitter&id='+id+'&callback=?',
			type: 'GET',
			dataType:'jsonp',
			success: function(dataJSON){
				if(dataJSON.STATUS=='OK'){
					var message = (Twitts.userActive)?'d '+Twitts.userActive+' '+dataJSON.TWITTER:dataJSON.TWITTER
					jQuery("#msg-click-box").click();
					jQuery("#msgbox").val(message);
					SearchGrid.showMessageHome(dataJSON.STATUS, dataJSON.MESSAGE);
					Twitts.userActive=false;
				}
				else{
					SearchGrid.showMessage(dataJSON.STATUS, dataJSON.MESSAGE);
				}
			}
		});
	},
	showWidget: function(id){
		var id;
		jQuery("#calendar_widget").fadeIn(500);
		Widget.ID=id;
		Widget.init();
		jQuery("#labels_calendar .labels_calendar").hide();
		jQuery("#labels_calendar .label_return_to_search").show();
	},
	showEvent: function(id){
		var id;
		jQuery("#calendar-accept").load("/event/view/?id="+id, function(){
			SearchGrid.details.init(id);
		});
	},
	editEvent: function(id){
		
		if(jsLoginStatus=='203'){
			SearchGrid.showMessage("KO", "You must be logged in for this action");
			return;
		}
		
		jQuery("#calendar_attendee_details").fadeIn(500);
		
		SearchGrid.currentIdEdit=id;
		SearchGrid.urlTo='/event/attendee/?id='+id;
		SearchGrid.appendTo='#calendar_attendee_details_grid';
		SearchGrid.makeGrid();
		
		jQuery("#labels_calendar span").hide();
		jQuery("#labels_calendar .label_return_to_search").show();
		
		SearchUser.type='edit';
		SearchUser.init();
	},
	infoEvent: function(element){
		SearchGrid.makeSquareTooltip(element);
	},
	submitEditEvent: function(){
		if(jsLoginStatus=='203'){
			SearchGrid.showMessage("KO", "You must be logged in for this action");
			return;
		}		
		
		var data=jQuery("#form_user_edit").serialize();
		
		jQuery("#form_user_edit input[type='submit']").hide();
		jQuery("#form_user_edit .submit_loading").show();
		
		jQuery.ajax({
			url: '/event/act/?type=attendee&id='+SearchGrid.currentIdEdit+'&callback=?',
			type: 'POST',
			data: data,
			dataType:'jsonp',
			success: function(dataJSON){
				if(dataJSON.STATUS=='OK'){
					if(dataJSON.IDCONFIRMLIST && dataJSON.IDCONFIRMLIST!=''){
						var aIdConfirm=dataJSON.IDCONFIRMLIST.split(",");
						if(aIdConfirm.length>0){
							SearchGrid.idAttendee = [];
							for(x in aIdConfirm){
								if(aIdConfirm[x]!=''){
									SearchGrid.idAttendee[x] = aIdConfirm[x];
								}
							}
						}
					}
					SearchGrid.clearFormEdit();					
					SearchGrid.urlTo='/event/attendee/?id='+SearchGrid.currentIdEdit;
					SearchGrid.makeGrid();
					SearchUser.clearAll();
				}
				SearchGrid.showMessage(dataJSON.STATUS, dataJSON.MESSAGE);
				jQuery("#form_user_edit .submit_loading").hide();
			}
		});
	},
	deleteEvent: function(id){
		if(jsLoginStatus=='203'){
			SearchGrid.showMessage("KO", "You must be logged in for this action");
			return;
		}		
		var id;
		var r=confirm("You really want to delete this event?");
		if(!r){
			return;
		} else {
			jQuery.ajax({
				url: '/event/act/?type=delete&id='+id+'&callback=?',
				type: 'GET',
				dataType:'jsonp',
				success: function(dataJSON){
					if(dataJSON.STATUS=='OK'){
						jQuery("#event_"+id).fadeOut(1000);
					}
					SearchGrid.showMessage(dataJSON.STATUS, dataJSON.MESSAGE);
				}
			});
		}
	},
	backToSearch: function(){
		jQuery('#calendar_attendee_details').fadeOut(500);
		SearchGrid.appendTo='#calendar_search_grid';
	},
	makeGrid: function(){
		
		if(SearchGrid.appendTo!='' && SearchGrid.urlTo !=''){
			SearchGrid.makeLoading();
		
			jQuery(SearchGrid.appendTo).load(SearchGrid.urlTo, function(){
				
				jQuery("#calendar-dialog #tab, #calendar-dialog #applet").removeAttr("id");				
				
				SearchGrid.bindHref();
				jQuery("#mask_grid").remove();
				jQuery(".grid-container *[title]").unbind('tooltip').tooltip({
					track: true, 
					delay: 0, 
					showURL: false, 
					showBody: " - ", 
					fade: 250
				});
				if(SearchGrid.idAttendee && SearchGrid.idAttendee.length>0){
					jQuery.each(SearchGrid.idAttendee, function(i, val){
						jQuery("#attendee_"+val+' td').css("backgroundColor","rgb(255, 255, 204)");
						jQuery("#attendee_"+val+' td').animate({
							background: 'rgb(255, 255, 255)'
						},400);
					});
				}
				if(SearchGrid.lastIdInsert){
					jQuery("#event_"+SearchGrid.lastIdInsert+' td').css("background","#FFFFCC");
					SearchGrid.lastIdInsert='';
				}				
				
			});	
		}
	},
	makeLoading: function(){
		var divMask=jQuery("<div></div>");
		divMask.attr("style","position:absolute; width:100%; height:100%; top:0px; left:0px; background:white;");
		divMask.attr("id","mask_grid");
		divMask.attr("style",divMask.attr("style")+"opacity:0.4; border:1px solid white;");
		divMask.append("<center><br><br><br><br><br><img src=\"/trade-project/images/stream/loading.gif\"></center>");
		jQuery(SearchGrid.appendTo).css("position","relative");
		jQuery(SearchGrid.appendTo).append(divMask);
	},
	bindHref: function(){
		var hrefGridPagination = jQuery(SearchGrid.appendTo+" .grid_pagination a");
		hrefGridPagination.unbind("click");
		hrefGridPagination.click(function(){
			SearchGrid.urlTo=jQuery(this).attr("href");
			jQuery(this).removeAttr("href");
			SearchGrid.makeGrid();
		});
	},
	showMessage: function(esito, message){
		var bgColorMessage;
		
		if(esito=='OK'){
			bgColorMessage='#90EE90';
		} else {
			bgColorMessage='red';
		}
		
		jQuery("#calendar_edit_message")
		.css("background",bgColorMessage)
		.css("color","white")
		.css("text-align","center")
		.html('<h1 style="color:white">'+message+'</h1>')
		.slideDown(500);
		
		setTimeout(function(){
			jQuery("#calendar_edit_message").slideUp(500);
		},5000);		
	},
	showMessageChart: function(esito, message){
		var bgColorMessage;
		
		if(esito=='OK'){
			bgColorMessage='#90EE90';
		} else {
			bgColorMessage='red';
		}
		
		jQuery("#chart_edit_message")
		.css("background",bgColorMessage)
		.css("color","white")
		.css("text-align","center")
		.html('<h1 style="color:white">'+message+'</h1>')
		.slideDown(500);
		
		setTimeout(function(){
			jQuery("#chart_edit_message").slideUp(500);
		},5000);		
	},	
	showMessageHome: function(esito, message){
		var bgColorMessage;
		var colorMessage;
		
		jQuery("#calendar_twitter_ok").remove();
		
		var divHome=jQuery('<div id="calendar_twitter_ok" style="position:absolute; top:0px; left:0px; width:98.5%; text-align:center; padding:20px; z-index:999999"></div>');
		
		if(esito=='OK'){
			bgColorMessage='#90EE90';
			colorMessage = 'black';
		} else {
			bgColorMessage='red';
			colorMessage = 'white';
		}
		
		divHome
		.css("background",bgColorMessage)
		.css("color",colorMessage)
		.html('<h1 style="color:'+colorMessage+'">'+message+'</h1>');
		
		divHome.prependTo("body").slideDown(500);
		
		if(jQuery("#calendar-dialog").length){
			DialogUtil.hidePopup("#calendar-dialog");		
		}
		setTimeout(function(){
			jQuery("#calendar_twitter_ok").hide(500);
		},5000);			
	},
	controlTypedInput: function(){
		var emailsInput = jQuery("#form_user_edit textarea[name='add_emails']");
		var usersSelected = jQuery("#selected_user_edit input");
		
		if(emailsInput.val()!='' || usersSelected.length>0){
			jQuery("#form_user_edit input[type='submit']").show();
		} else {
			jQuery("#form_user_edit input[type='submit']").hide();
		}
	},
	clearFormEdit: function(){
		jQuery("#form_user_edit textarea").empty().val("");
		jQuery("#form_user_edit input[type!=submit]").empty().val("");
		jQuery("#form_user_edit input[type='submit']").hide();
	},
	makeSquareTooltip: function(element){
		var element, tooltip, htmlto;
		element = jQuery(element);
		jQuery(document).click(function(e){
			if(e.target!=this){
				tooltip.hide();
			}
		});
		htmlto=element.attr("rel").replace(/-/gi,'<br>');
		jQuery(".details_tooltip").remove();
		tooltip=jQuery("<div></div>");
		tooltip.css({
			'background' : 'white',
			'font-size' : '11px',
			'padding' : '10px',
			'border' : '2px solid #E9E9E9',
			'position': 'absolute',
			'top' : (element.offset().top+20),
			'left': (element.offset().left+20),
			'max-width': '280px',
			'z-index':'999999',
			'cursor': 'pointer'
		});
		tooltip.attr("class","details_tooltip");
		tooltip.click(function(){
			jQuery(this).hide(500);
		});
		tooltip.append(htmlto);
		tooltip.append("<div style=\"padding-top:5px;border-top:1px solid #E9E9E9; color:blue; border-bottom:1px dotted font-size:10px;\">Click to hide</div>");
		tooltip.hide().appendTo("body").show(500);
	},
	returnToSearchResult: function(){
		jQuery(".label_text_for_search").show();
		jQuery(".label_text_for_search_2").show();
		jQuery(".label_return_to_search").hide();
		
		jQuery("#calendar_adv_search").hide();
		jQuery("#calendar-accept").hide();
		jQuery("#calendar_attendee_details").hide();
		jQuery("#calendar_widget").hide();
		
		jQuery("#calendar_search").show();
		
		jQuery(".calendar-tab a").removeClass("selected");
		jQuery(".calendar-tab a:first").addClass("selected");
		
		SearchGrid.appendTo = '#calendar_search_grid';
		
	},
	
	externalSearch: function(queryString){
		
		jQuery("#event-btn").click();
		
		SearchGrid.urlTo='/event/room/?'+queryString;
		SearchGrid.makeGrid();
		
		jQuery(".cancel_and_return_event, .label_text_for_search").show();
		
		jQuery(".label_return_to_search").hide();		
		
		
		
	}
	
	
};

SearchGrid.details = {
	init: function(eid){
		SearchGrid.details.id = eid;
		jQuery("#form_to_accept").hide();
		jQuery("#calendar-accept").show();
		jQuery(".labels_calendar").hide();
		jQuery(".label_return_to_search").show();
		SearchGrid.details.bindButtons();
		jQuery("#form_to_accept input[name='eid']").val(SearchGrid.details.id);
	},
	bindButtons: function(){
		
		var isLogged = (jsLoginStatus=='203') ? false : true
		
		jQuery("#buttons_accept a").bind("click", function(){
			
			var ref = jQuery(this).attr("ref");
			
			if(!ref){
				return;
			}
			
			jQuery("#form_to_accept input[name='status']").val(ref);
			
			if(!isLogged){	
				SearchGrid.details.showFormToAccept();
				
			} else {
				SearchGrid.details.submitFormEvent();
			}
		});
	},
	showFormToAccept: function(){
		jQuery(".calendar-tab .labels_calendar").hide();
		jQuery(".calendar-tab .label_return_to_details").show();
		jQuery("#form_to_accept").show();
		jQuery(".calendar-tab .label_return_to_details a").unbind("click").bind("click",function(){
			SearchGrid.details.init(SearchGrid.details.id);
		});
		
	},
	submitFormEvent: function(){
		var form = jQuery("#form_to_accept form");
		var data = form.serialize();
		jQuery.ajax({
			url: '/event/confirm/?callback=?',
			data: data,
			type: "POST",
			dataType: 'jsonp',
			success: function(dataJSON){
				jQuery("#form_to_accept").hide();
				//SearchGrid.showEvent(SearchGrid.details.id);
				jQuery("#form_to_accept input[type='text']").val("");
				SearchGrid.details.showMessageAccept(dataJSON.STATUS,dataJSON.MESSAGE);
			}
		});
	},
	showMessageAccept: function(status, message){
		//jQuery("#message_accept").remove();
		var divMsg = jQuery("#message_accept");
		
		if(status=='KO') divMsg.css("background","darkred");
		if(status=='OK') divMsg.css("background","darkgreen");
		
		divMsg.text(message);
		
		//jQuery("#calendar-accept").append(divMsg);
		
		divMsg.slideDown(500);
		
		setTimeout(function(){
			divMsg.slideUp(500, function(){
				divMsg.css("z-index","000000");	
				
				if(status=='OK'){
					jQuery("#calendar-accept").show().load("/event/view/?id="+SearchGrid.details.id, function(){
						SearchGrid.details.init(SearchGrid.details.id);
					});
				}		
									
			});		
			
		},2000);
		
	},
	externalSource: function(eid){
		jQuery('#event-btn').click();
		jQuery("#calendar-accept").show().load("/event/view/?id="+eid, function(){
			SearchGrid.details.init(eid);
		});
	},
	clickExternal: function(eid, ref){
		SearchGrid.details.externalSource(eid);
		jQuery("#buttons_accept a[ref='"+ref+"']").click();
	},
};
