preloadedImg_1 = new Image();
preloadedImg_1.src = "/img/load.gif";

var cid;

/************************************* Rating Form ***********************************/
function rOver(i) {
	if (i == 1) swapTxt("I hate it!");
	else if (i == 2) swapTxt("I don\'t like it");
	else if (i == 3) swapTxt("It\'s OK");
	else if (i == 4) swapTxt("I like it");
	else if (i == 5) swapTxt("I love it!");
	else if (i == 6) swapTxt("My Favorite!");
	
	if(document.images){
		if (i >= 1) document.images['r1'].src = "/img/s4.png";
		if (i >= 2) document.images['r2'].src = "/img/s4.png";
		else 	    document.images['r2'].src = "/img/s0.png";
		if (i >= 3) document.images['r3'].src = "/img/s4.png";
		else 	    document.images['r3'].src = "/img/s0.png";
		if (i >= 4) document.images['r4'].src = "/img/s4.png";
		else        document.images['r4'].src = "/img/s0.png";
		if (i >= 5) document.images['r5'].src = "/img/s4.png";
		else 	    document.images['r5'].src = "/img/s0.png";
		if (document.images['r6']) {
			if (i == 6) document.images['r6'].src = "/img/s5.png";
			else        document.images['r6'].src = "/img/s0.png";
		}
	}
}

function rOut() {
	swapTxt(swapTxt_default);
	if (document.images) {
		document.images['r1'].src = "/img/s0.png";
		document.images['r2'].src = "/img/s0.png";
		document.images['r3'].src = "/img/s0.png";
		document.images['r4'].src = "/img/s0.png";
		document.images['r5'].src = "/img/s0.png";
		if (document.images['r6']) document.images['r6'].src = "/img/s0.png";
	}
}

function swapTxt(x){
	$("rating_txt").innerHTML = x;
}

/************************************* postRating ***********************************/
function postRating(r) {
	// Loading
	$("rating").innerHTML = "<center><img src='/img/s.gif' style='vertical-align:middle;' width='1' height='41' alt='' /><img src='/img/load.gif' name='preloadedImg_1' style='vertical-align:middle;' /> Loading..</center>";

	new Ajax.Request("/?ajaxReq=postRating&ctype=" + ctype + "&cid=" + cid + "&r=" + r,
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			$("rating").innerHTML = r;
		}
	});
}

/************************************* postDeleteRating ***********************************/
function postDeleteRating(id) {	
	new Ajax.Request("/?ajaxReq=postDeleteRating&id=" + id,
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			$("rating").innerHTML = r;
		}
	});	
}

/************************************* postComment ***********************************/
function postComment() {
	new Ajax.Request("/?ajaxReq=postComment&ctype=" + ctype + "&cid=" + cid + "&comment=" + escape(document.form_comment.comment.value),
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			$("comments_box").innerHTML = r;
		}
	});	
	
	document.form_comment.comment.value = "";
	document.form_comment.comment.focus();
}

/************************************* postDeleteComment ***********************************/
function confirmDeleteComment(id) {
	if (confirm("Are you sure you want to do this?")) {
		postDeleteComment(id);
	}
}

function postDeleteComment(id) {
	new Ajax.Request("/?ajaxReq=postDeleteComment&id=" + id,
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			$("comments_box").innerHTML = r;
		}
	});	
}

/************************************* getCommentQuote ***********************************/
function getCommentQuote(id) {
	new Ajax.Request("/?ajaxReq=getCommentQuote&id=" + id,
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			document.form_comment.comment.value = r + "\n";
			document.form_comment.comment.focus();
		}
	});
}

/** openEdit ******************************************************* **/
function openEdit(id) {
    hideLayer(id);
	showLayer(id + "_edit");
	$(id + "_value").focus();
}

/** closeEdit ****************************************************** **/
function closeEdit(id) {
    $(id + "_edit").hide();
	$(id).show();
}

/** postEditTitle ************************************************** **/
function postEditTitle() {
    var title = $("title_value").value;
	new Ajax.Request("/?ajaxReq=postEditTitle&ctype=" + ctype + "&cid=" + cid + "&title=" + escape(title));
	
	if (!title) {
		title = "Click to Add a Title";
	}
	
	$("title").innerHTML = "<h1>" + title + "</h1>";
	closeEdit("title");
}

/** postEditDescription ******************************************** **/
function postEditDescription() {
    var description = $("description_value").value;
	new Ajax.Request("/?ajaxReq=postEditDescription&ctype=" + ctype + "&cid=" + cid + "&description=" + escape(description));
	
	if (!description) {
		description = "Click to Add a Description";
	}
	
	$("description").innerHTML = description;
	closeEdit("description");
}



/** postEditSource ************************************************* **/
function postEditSource() {
	var source = $("source_value").value;
	new Ajax.Request("/?ajaxReq=postEditSource&ctype=" + ctype + "&cid=" + cid + "&source=" + escape(source));

	if (!source) {
		source = "Click to Add a Source";
	}

	$("source").innerHTML = "&copy; " + source;
	closeEdit("source");
}

/** postEditTags *************************************************** **/
function postEditTags() {
    var tags = $("tags_value").value;
	new Ajax.Request("/?ajaxReq=postEditTags&ctype=" + ctype + "&cid=" + cid + "&tags=" + escape(tags),
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			if (!r) {
				r = "Click to Add Tags";
			}

			$("tags").innerHTML = "Tags: " + r;
			closeEdit("tags");
		}
	});
}

/************************************* loadLineup ***********************************/
Event.observe(document, "dom:loaded", function() {
	if (typeof(lineupCenter) != "undefined") {
		new Ajax.Request("/?ajaxReq=lineup&center=" + lineupCenter + "&offset=0&type=" + lineupType,
		{
			method:"get",
			onSuccess: function(t){
				var r = t.responseText || "";
				$("lineup").innerHTML = r;
			}
		});	
	}
});

/************************************* changeLineup ***********************************/
centerOffset = 0;
function changeLineup(offset) {
	centerOffset = centerOffset + offset;
	
	new Ajax.Request("/?ajaxReq=lineup&center=" + lineupCenter + "&offset=" + centerOffset + "&type=" + lineupType,
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			$("lineup").innerHTML = r;
		}
	});
}

/************************************* Key Press ***********************************/
Event.observe(window, "keypress", function(e) {
	var key;
	var keychar;
	
	if (window.event) key = window.event.keyCode;
	else if (e) key = e.which;
	
	if (key && key == 13) {
		if (document.form_search.q.value && document.form_search.q.value != "Search Videos" && document.form_search.q.value != "Search Photos" && document.form_search.q.value != "Search The Site") {
			document.form_search.submit();
			return true;	
		}
		
		if (document.form_tags.tags.value) {
			postTags();
			return false;
		}
	}	
});

/** toggleAddToAlbum ****************************************** **/
function toggleAddToAlbum() {
	$("add_to_album").toggle();
}

/** addToAlbum ************************************************ **/
function addToAlbum() {
    $("add_to_album_saving").style.height = $("add_to_album").getHeight() + "px";
	$("add_to_album").hide();
	$("add_to_album_saving").show();
	window.setTimeout("$('add_to_album').show();$('add_to_album_saving').hide();", 500);
	
	var album = document.getElementsByName("album_id");
	for (i = 0; i < album.length; i++) {
		if (album[i].value != "new") {
			new Ajax.Request("/?ajaxReq=addToAlbum&album_id=" + album[i].value + "&ctype=" + ctype + "&cid=" + cid + "&in=" + album[i].checked);
		} else if (album[i].checked) {
			new Ajax.Request("/?ajaxReq=addToAlbum&album_id=" + album[i].value + "&name=" + document.getElementsByName("album_name")[0].value + "&ctype=" + ctype + "&cid=" + cid);
		}
	}
}

/** confirm_delete ******************************************** **/
function confirm_delete(id){
	if (confirm("Are you sure you'd like to delete this " + ctype + "?")){
		location.replace("/" + ctype + "s/?delete=true&id=" + id);
	}
}

/** showOlderComments ****************************************** **/
function showOlderComments(ctype, cid, start, limit) {
	new Ajax.Request("/?ajaxReq=showOlderComments&ctype=" + ctype + "&cid=" + cid + "&start=" + start + "&limit=" + limit,
	{
		method:"get",
		onSuccess: function(t){
			var r = t.responseText || "";
			$("old_comments").insert({ after: "<center><div style='width:520px;height:1px;background:#CFCFCF;margin:5px 0px 10px 0px;'></div></center>" });
			$("old_comments").remove();
			$("comments_box").innerHTML += r;
		}
	});	
}