Function.prototype.method = function(name, fn) {
	this.prototype[name] = fn;
	return this;
};

var comments = function() {

	var contentType;
	var objectId;
	var hintText = "Leave a Comment";

	var ajaxForm = function(id, callback) {
		$('#'+id+" form").ajaxForm({
			target: '#'+id,
			beforeSubmit: beforeSubmit,
			success: function() {
				ajaxForm(id, callback);
				callback();
			}
		}); 
	};

	var beforeSubmit = function(formData, jqForm, options) {
		if (jqForm[0].comment.value == hintText) {
			showError(options.target, "Comment cannot be blank");
			return false;
		}
		return true;
	};

	var addComment = function(commentsId) {
		$("#"+commentsId).load("/comments/?contentType="+contentType+"&objectId="+objectId);
	};

	var initValidation = function(formId) {
		$("#"+formId+" form").validationAideEnable(null, {showInlineMessages:true});
	};

	var storeContentType = function(formId) {
		var form = $("#"+formId+" form");
		contentType = form.find("#id_content_type").val()
		objectId = form.find("#id_object_pk").val()	
	};

	var showError = function(formId, message) {
		var div = document.createElement('div');
		div.className = "Error";
		div.innerHTML = message;
		$(formId).append(div);
	};

	var bindCommentBoxHint = function(formId) {
		var commentBox = $("#"+formId+" #id_comment");
		var currentColor = commentBox.css("color");

		var init = function() {
			commentBox.val(hintText);
			commentBox.css("color", "#ccc");
		}

		commentBox.bind("focus", function() {
			if ($(this).val() == hintText) {
				$(this).val('');
				$(this).css("color", currentColor);
			}
		});

		commentBox.bind("blur", function() {
			if ($(this).val() == '') {
				init();	
			}
		});

		init();
	};
	return {

		init: function(commentsId, formId) {
			$(function() {
				bindCommentBoxHint(formId);
				storeContentType(formId);
				//initValidation(formId);
				ajaxForm(formId, function() {
					addComment(commentsId);
				});
			});
		}
	}

}();

var ratings = function() {
	var postUrl = "/ratings/vote/";
	return {
		vote: function(ratingHref, contentType, objectPk, score) {
			$.post(postUrl, { 'contentType': contentType, 'objectPk': objectPk, 'score': score }, function(data) {
				ratingHref.parentNode.parentNode.innerHTML = data;
			});
		}
	}
}();

var navigation = function() {

	var getSelectedIndex = function(liArray) {
		var index = -1;
		liArray.each(function(i) {
			if ($(this).hasClass("selected"))
				index = i;
		});
		return index;
	};

	return {
		render: function(navigationId) {
			var nav = $("#"+navigationId);
			var liArray = nav.find("li");
			var total = liArray.length;
			var lastIndex = total -1;
			var index = getSelectedIndex(liArray);
			$(liArray[lastIndex]).addClass("nav0");
			if (index != -1) {
				if (index == 0) {
					$(liArray[0]).addClass("nav10");
				}
				else if (index == lastIndex) {
					$(liArray[lastIndex]).addClass("nav1");
					$(liArray[index-1]).addClass("nav01");
				}
				else {
					$(liArray[index]).addClass("nav10");
					$(liArray[index-1]).addClass("nav01");
				}
			}
		}
	}
}();


var RotatePhotos = function(elementNode) {
	this._element = elementNode;
	this._photos = elementNode.getElementsByTagName("img");
	this._currentIndex = this._photos.length - 1;
	var self = this;
	$(function() {
		self._rotate();
	});
}
RotatePhotos.
	method('_rotate', function() {
		var self = this;
		var currentPhoto = this._getCurrentPhoto();
		$(currentPhoto).fadeOut(function() {
			if (self._photos.length == (self._currentIndex + 1))
				self._currentIndex = 0;
			else
				self._currentIndex++;
			var nextPhoto = self._getCurrentPhoto();
			$(nextPhoto).fadeIn();
			setTimeout(function() {
				self._rotate();
			}, 4000);
		});
	}).
	method("_getCurrentPhoto", function() {
		return this._photos[this._currentIndex];
	});
