/**
 * 
 * Event functionality.
 * 
 */

function displayEventPhoto(eventId,orderId) {
	// Lookup photo informations
	$.post("data/eventPhotoHandler.php",{eventId:eventId,orderId:orderId},function (data) {
		if(data.error==0) {
			// Event photo found, set up display
			savedEventId = eventId;
			savedOrderId = orderId;
			if(data.isVertical) {
				$("#eventsVerticalPopupImage").attr("src","data/getEventImage.php?imageId="+data.eventPhotoId);
				$("#eventsVerticalPopupDownload").attr("href","data/getEventImage.php?imageId="+data.eventPhotoId);
				$("#eventsVerticalPopupFacebook").attr("href",data.facebookLink);
				$("#eventsVerticalPopupIndex").text(data.orderId);
				$("#eventsVerticalPopupMax").text(data.maxId);
				$("#eventsVerticalPopupDescription").html(data.title+" - "+data.formattedEventDate);
				if(data.orderId > 1) {
					$("#eventsVerticalPopupPrevious").show();
				} else {
					$("#eventsVerticalPopupPrevious").hide();
				}
				if(data.orderId<data.maxId) {
					$("#eventsVerticalPopupNext").show();
				} else {
					$("#eventsVerticalPopupNext").hide();
				}
				
				$("#blackoutEventsHolder").show();
				$("#eventsHorizontalPopup").hide();
				$("#eventsVerticalPopup").show();
				$("#eventsCloseSlideshow").show();				
			} else {
				$("#eventsHorizontalPopupImage").attr("src","data/getEventImage.php?imageId="+data.eventPhotoId);
				$("#eventsHorizontalPopupDownload").attr("href","data/getEventImage.php?imageId="+data.eventPhotoId);
				$("#eventsHorizontalPopupFacebook").attr("href",data.facebookLink);
				$("#eventsHorizontalPopupIndex").text(data.orderId);
				$("#eventsHorizontalPopupMax").text(data.maxId);
				$("#eventsHorizontalPopupDescription").html(data.title+" - "+data.formattedEventDate);
				if(data.orderId > 1) {
					$("#eventsHorizontalPopupPrevious").show();
				} else {
					$("#eventsHorizontalPopupPrevious").hide();
				}
				if(data.orderId<data.maxId) {
					$("#eventsHorizontalPopupNext").show();
				} else {
					$("#eventsHorizontalPopupNext").hide();
				}
				$("#blackoutEventsHolder").show();
				$("#eventsVerticalPopup").hide();
				$("#eventsHorizontalPopup").show();
				$("#eventsCloseSlideshow").show();
			}
			
		}
	},"json");
	
}

function loadCityList(stateId) {
	$.post("data/eventSearchHandler.php",{actionId:1,stateId:stateId},function (data) {
		if(data.error==0) {
			cityList = new Array();
			for (i=0;i<data.cityList.length;i++) {
				cityList.push(new Array(data.cityList[i].cityId,data.cityList[i].cityName));
			}
			buildCityBox();
		}
	},"json");
}
function loadDateList(cityId) {
	$.post("data/eventSearchHandler.php",{actionId:2,cityId:cityId},function (data) {
		if(data.error==0) {
			dateList = new Array();
			for (i=0;i<data.dateList.length;i++) {
				dateList.push(new Array(data.dateList[i].eventDate,data.dateList[i].formattedEventDate));
			}
			buildDateBox();
		}
	},"json");
}

function buildCityBox() {
	var maxNumberToShow = 6;
	var numberOfItemsToShow = Math.min(cityList.length,maxNumberToShow);
	$("#eventsCitySelectItems").css("height",(numberOfItemsToShow*18)+"px");
	
	if(cityList.length>numberOfItemsToShow) {
		$("#eventsCitySelectUp").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#eventsCitySelectDown").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#eventsCitySelectUp").click(function() {
			if(cityListPosition>0) {
				cityListPosition--;
				$("#eventsCitySelectItems").scrollTop(cityListPosition*18);
			}
		});
		$("#eventsCitySelectDown").click(function() {
			if(cityListPosition<(cityList.length-maxNumberToShow)) {
				cityListPosition++;
				$("#eventsCitySelectItems").scrollTop(cityListPosition*18);
			}
		});
	} else {
		$("#eventsCitySelectUp").unbind('mouseenter mouseleave click');
		$("#eventsCitySelectDown").unbind('mouseenter mouseleave click');
	}
	
	$("#eventsCitySelectItems").empty();
	for (i=0; i<cityList.length; i++) {
		recordId = "city-"+cityList[i][0];
		$("#eventsCitySelectItems").append("<div id='"+recordId+"' name='"+cityList[i][1]+"' class='eventsCityRecord'>"+cityList[i][1]+"</div>");
		$("#"+recordId).hover(function() {
			$(this).css('cursor', 'pointer');
			$(this).css('background','url(./images/events/cityRecordSelected.png)');
		}, function() {
			$(this).css('background','url(./images/events/cityRecord.png)');
		});
		$("#"+recordId).click(function() {
			// City selected - read information
			cityId = $(this).attr('id').substring($(this).attr('id').indexOf("-")+1);
			cityName = $(this).attr('name');
			
			// Hide city list and set city field
			$("#eventsCitySelect").hide();
			$("#eventsCitySelectValue").text(cityName);
			
			// Hide any other fields that may be present on reset
			$("#eventsDateSelect").hide();
			$("#eventsDateSelectValue").text("CHOOSE DATE");
			dateListPosition = 0;
			selectedCity = cityId;
			
			// Load date box
			loadDateList(cityId);
		});
	}
	
	$("#eventsCitySelectValue").click(function() {
		$("#eventsCitySelect").show();
	});
	$("#eventsCitySelectValue").hover(function() {
		$(this).css('cursor', 'pointer');
	});
}
function buildDateBox() {
	var maxNumberToShow = 4;
	var numberOfItemsToShow = Math.min(dateList.length,maxNumberToShow);
	$("#eventsDateSelectItems").css("height",(numberOfItemsToShow*18)+"px");
	
	if(dateList.length>numberOfItemsToShow) {
		$("#eventsDateSelectUp").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#eventsDateSelectDown").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#eventsDateSelectUp").click(function() {
			if(dateListPosition>0) {
				dateListPosition--;
				$("#eventsDateSelectItems").scrollTop(dateListPosition*18);
			}
		});
		$("#eventsDateSelectDown").click(function() {
			if(dateListPosition<(dateList.length-maxNumberToShow)) {
				dateListPosition++;
				$("#eventsDateSelectItems").scrollTop(dateListPosition*18);
			}
		});
	} else {
		$("#eventsDateSelectUp").unbind('mouseenter mouseleave click');
		$("#eventsDateSelectDown").unbind('mouseenter mouseleave click');
	}
	
	$("#eventsDateSelectItems").empty();
	for (i=0; i<dateList.length; i++) {
		recordId = "date-"+dateList[i][0];
		$("#eventsDateSelectItems").append("<div id='"+recordId+"' name="+dateList[i][1]+" class='eventsDateRecord'>"+dateList[i][1]+"</div>");
		$("#"+recordId).hover(function() {
			$(this).css('cursor', 'pointer');
			$(this).css('background','url(./images/events/dateRecordSelected.png)');
		}, function() {
			$(this).css('background','url(./images/events/dateRecord.png)');
		});
		$("#"+recordId).click(function() {
			// Date selected - read information
			dateId = $(this).attr('id').substring($(this).attr('id').indexOf("-")+1);
			dateName = $(this).attr('name');
			
			// Hide date list and set date field
			$("#eventsDateSelect").hide();
			$("#eventsDateSelectValue").text(dateName);
			
			// Forward to appropriate album page
			window.location = "eventAlbums.php?locationId="+selectedCity+"&date="+dateId;
		});
	}
	
	$("#eventsDateSelectValue").click(function() {
		$("#eventsDateSelect").show();
	});
	$("#eventsDateSelectValue").hover(function() {
		$(this).css('cursor', 'pointer');
	});
}

function buildStateBox() {
	var maxNumberToShow = 8;
	var numberOfItemsToShow = Math.min(stateList.length,maxNumberToShow);
	$("#eventsStateSelectItems").css("height",(numberOfItemsToShow*18)+"px");
	
	if(stateList.length>numberOfItemsToShow) {
		$("#eventsStateSelectUp").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#eventsStateSelectDown").hover(function() {
			$(this).css('cursor', 'pointer');
		});
		$("#eventsStateSelectUp").click(function() {
			if(stateListPosition>0) {
				stateListPosition--;
				$("#eventsStateSelectItems").scrollTop(stateListPosition*18);
			}
		});
		$("#eventsStateSelectDown").click(function() {
			if(stateListPosition<(stateList.length-maxNumberToShow)) {
				stateListPosition++;
				$("#eventsStateSelectItems").scrollTop(stateListPosition*18);
			}
		});
	}
	
	for (i=0; i<stateList.length; i++) {
		recordId = "state-"+stateList[i][0];
		$("#eventsStateSelectItems").append("<div id='"+recordId+"' name="+stateList[i][1]+" class='eventsStateRecord'>"+stateList[i][1]+"</div>");
		$("#"+recordId).hover(function() {
			$(this).css('cursor', 'pointer');
			$(this).css('background','url(./images/events/stateRecordSelected.png)');
		}, function() {
			$(this).css('background','url(./images/events/stateRecord.png)');
		});
		$("#"+recordId).click(function() {
			// State selected - read information
			stateId = $(this).attr('id').substring($(this).attr('id').indexOf("-")+1);
			stateName = $(this).attr('name');
			
			// Hide state list and set state field
			$("#eventsStateSelect").hide();
			$("#eventsStateSelectValue").text(stateName);
			
			// Hide any other fields that may be present on reset
			$("#eventsCitySelect").hide();
			$("#eventsCitySelectValue").text("CHOOSE CITY");
			$("#eventsDateSelect").hide();
			$("#eventsDateSelectValue").text("CHOOSE DATE");
			cityListPosition = 0;
			dateListPosition = 0;
			$("#eventsCitySelectValue").unbind('mouseenter mouseleave click');
			$("#eventsDateSelectValue").unbind('mouseenter mouseleave click');
			$("#eventsDateSelectValue").hover(function() {$(this).css('cursor', 'auto');});
			
			// Load city box
			loadCityList(stateId);
		});
	}
	
}

var savedEventId = 0;
var savedOrderId = 0;
var stateListPosition = 0;
var cityListPosition = 0;
var dateListPosition = 0;
$(document).ready(function(){
	if(location.href.indexOf("events.php")!=-1) {
		buildStateBox();
		$("#eventsStateSelectValue").click(function() {
			$("#eventsStateSelect").show();
		});
		$("#eventsStateSelectValue").hover(function() {
			$(this).css('cursor', 'pointer');
		});
	}
	if(location.href.indexOf("eventAlbum.php")!=-1) {
		$("#eventsHorizontalPopupPrevious").click(function() {
			if(savedEventId>0 && savedOrderId>0) {
				displayEventPhoto(savedEventId,savedOrderId-1);
			}
		});
		$("#eventsHorizontalPopupNext").click(function() {
			if(savedEventId>0 && savedOrderId>0) {
				displayEventPhoto(savedEventId,savedOrderId+1);
			}
		});	
		$("#eventsVerticalPopupPrevious").click(function() {
			if(savedEventId>0 && savedOrderId>0) {
				displayEventPhoto(savedEventId,savedOrderId-1);
			}
		});
		$("#eventsVerticalPopupNext").click(function() {
			if(savedEventId>0 && savedOrderId>0) {
				displayEventPhoto(savedEventId,savedOrderId+1);
			}
		});
		$("#eventsCloseSlideshow").click(function() {
			$("#eventsHorizontalPopup").hide();
			$("#eventsVerticalPopup").hide();
			$("#blackoutEventsHolder").hide();
			savedEventId = 0;
			savedOrderId = 0;
			$("#eventsCloseSlideshow").hide();
		});
	}
});
