var Events = {Images: {}};

Events.newEvent = function() {
	var responseDiv = $j("#new-response-div");
	responseDiv.html("Creating new Event Page ...");
	var params = {title: $("new-event-title").value, event_type: $("new-event-type").value};
	var onSuccess = function(val) {
		responseDiv.html("event created successfully");
		var url = "/cms/events/edit.php?id=" + val;
		loadUrl(url);
	};
	var onFailure = function(val) {
		responseDiv.html("ERROR: " + val);
	};
	callJson("/cms/events/new.php", {parameters: params, onSuccess: onSuccess, onFailure: onFailure});
}.bind(Events);

Events.updateTitleAndBody = function(id) {
	var responseDiv = $j("#edit-response-div");
	responseDiv.html("Updating event title and body ...");
	var params = {title: $("edit-event-title").value, body: $("edit-event-body").value, id: id};
	var onSuccess = function(val) {
		responseDiv.html("event updated successfully");
		reload();
	};
	var onFailure = function(val) {
		responseDiv.html("ERROR: " + val);
	};
	callJson("/cms/events/edit_title_and_body.php", {parameters: params, onSuccess: onSuccess, onFailure: onFailure, method: 'post'});
}.bind(Events);


Events.addImage = function(id) {
	TINY.box.show("/cms/events/add_image.php?id=" + id, 1, 400, 120, 2);
}.bind(Events);


Events.removeImage = function(id, filename) {
	var res = confirm("Are you sure you want to delete this image ?");
	if(!res) return;
	var onSuccess = function() {
		$j("#" + filename).fadeOut();
	};
	var onFailure = function(val) {
		alert('failed');
	};
	callJson("/cms/events/remove_image_submit.php", {parameters: {event_id: id, filename: filename}, onSuccess: onSuccess, onFailure: onFailure});	
}.bind(Events);

Events.updateLogo = function(id) {
	TINY.box.show("/cms/events/update_logo.php?id=" + id, 1, 400, 120, 2);
}.bind(Events);

Events.Images.init = function() {
	var clicked = function() {
		Events.Images.clicked(this);
	};
	var links = $j(".image-links .image-link");
	links.click(clicked);
	links.first().click();
}.bind(Events.Images);

Events.Images.clicked = function(el) {
	var viewport = $j(".event-images .viewport");
	viewport.html("");
	var img = new Element("img", {src: el.getAttribute("imgsrc")});
	viewport.html(img);
	var links = $j(".image-links .image-link");
	links.removeClass("clicked");
	el.addClassName("clicked");
}.bind(Events.Images);

Events.updateYoutubeLink = function(id) {
	var params = {
		id: id, 
		url: $j("#edit-event-youtube-link").val()
	}
	callJson("/cms/events/edit_video_link.php", {parameters: params, onSuccess: function() {}, method: 'post'});
}.bind(Events);


Events.deleteEvent = function(id, divId) {
	var res = confirm("Are you sure you want to delete this event ?");
	if(!res) return;
	var params = {id: id}
	var onSuccess = function() {
		$j("#" + divId).fadeOut();
	};
	callJson("/cms/events/delete.php", {parameters: params, onSuccess: onSuccess, method: 'post'});
}.bind(Events);
