/**
* an on/off button
*/
var toggleSwitch = Class.create();
toggleSwitch.prototype = {
	initialize: function(button, content_id, onAction, offAction, onHandler, offHandler, onVerify, offVerify) {
		this.buttonName = button;
		this.button = $(button);
		this.content_id = content_id;
		this.onAction = onAction;
		this.offAction = offAction;
		this.onHandler = onHandler;
		this.offHandler = offHandler;
		this.onVerify = onVerify;
		this.offVerify = offVerify;
		if(this.button)
		{
			this.button.childNodes[0].onclick = this.toggle.bindAsEventListener(this);
		}
	},
	
	toggle: function () {
		if( this.isOn() ) {
			this.off();
		} else {
			this.on();
		}
		return false;
	},
	
	on: function () {
		if( this.onAction ) {
			if( this.onVerify ) {
				if( ! confirm(this.onVerify) ) {
					return false;
				}
			}
			submitdata = "submit=submit&submitMethod=ajax";
			submitdata += "&content_id=" + this.content_id;
			
			new Ajax.Request(
		         this.onAction + this.content_id,
		         {method:'post', postBody:submitdata, onSuccess:this.onHandler, onFailure:this.errorHandler});
		}
	},
	
	off: function () {
		if( this.offAction ) {
			if( this.offVerify ) {
				if( ! confirm(this.offVerify) ) {
					return false;
				}
			}
			submitdata = "submit=submit&submitMethod=ajax";
			submitdata += "&content_id=" + this.content_id;
			
			new Ajax.Request(
		         this.offAction + this.content_id,
		         {method:'post', postBody:submitdata, onSuccess:this.offHandler, onFailure:this.errorHandler});
		}
	},
	
	isOn: function () {
		if(this.button.childNodes[0].href.indexOf(this.offAction) >= 0) {
			return true;
		}
		return false;
	}
}

/**
 * TRACKING FUNCTIONS
 */
var startTracking = function(content_id) {
	var statusDiv = getValue('track_status');
	var url = '/comment/startwatch/' + content_id;
	
	hidediv('item_track');
	hidediv('item_untrack');
	
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			content_id: content_id,
			submit: 'submit',
			submitMethod: 'ajax'
		},
		onLoading: function(request) {
			statusDiv.innerHTML = '<img src="/images/common/indicator.gif" />';
		},
		onSuccess: function(t) {
			showdiv('item_untrack');
			statusDiv.innerHTML = '';
		},
		onFailure: function(t) {
			// Connection Error
			statusDiv.innerHTML = 'Error!';
		}
	});
	
	return false;
}

var stopTracking = function(content_id) {
	var statusDiv = getValue('track_status');
	var url = '/comment/stopwatch/' + content_id;
	
	hidediv('item_track');
	hidediv('item_untrack');
	
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			content_id: content_id,
			submit: 'submit',
			submitMethod: 'ajax'
		},
		onLoading: function(request) {
			statusDiv.innerHTML = '<img src="/images/common/indicator.gif" />';
		},
		onSuccess: function(t) {
			showdiv('item_track');
			statusDiv.innerHTML = '';
		},
		onFailure: function(t) {
			// Connection Error
			statusDiv.innerHTML = 'Error!';
		}
	});
	
	return false;
}

/**
 * FAVORITE FUNCTIONS
 */
var addFavorite = function(content_id) {
	var statusDiv = getValue('fave_status');
	var url = '/favorites/add/' + content_id;
	
	hidediv('item_fave');
	hidediv('item_unfave');
	
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			content_id: content_id,
			submit: 'submit',
			submitMethod: 'ajax'
		},
		onLoading: function(request) {
			statusDiv.innerHTML = '<img src="/images/common/indicator.gif" />';
		},
		onSuccess: function(t) {
			showdiv('item_unfave');
			statusDiv.innerHTML = '';
		},
		onFailure: function(t) {
			// Connection Error
			statusDiv.innerHTML = 'Error!';
		}
	});
	
	return false;
}

var removeFavorite = function(content_id) {
	var statusDiv = getValue('fave_status');
	var url = '/favorites/remove/' + content_id;
	
	hidediv('item_fave');
	hidediv('item_unfave');
	
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			content_id: content_id,
			submit: 'submit',
			submitMethod: 'ajax'
		},
		onLoading: function(request) {
			statusDiv.innerHTML = '<img src="/images/common/indicator.gif" />';
		},
		onSuccess: function(t) {
			showdiv('item_fave');
			statusDiv.innerHTML = '';
		},
		onFailure: function(t) {
			// Connection Error
			statusDiv.innerHTML = 'Error!';
		}
	});
	
	return false;
}

/**
 * FLAG FUNCTIONS
 */
function HandleItemFlag(t) {
	if( t.responseText == 'ok' ) {
		var btn = $('item_flag');
		
		btn.className = btn.id + '_on';
		btn.udpdate('<span>This item has been flagged as inappropriate</span>');
		btn.blur();
	} else {
		console.log( t.responseText + " Error: not flagged" );
	}
}

/**
 * WATCHLIST FUNCTIONS
 */
function HandleStartWatching (t) {
	if( t.responseText == 'ok' ) {
		var btn = $('item_watch');
		if( ! btn ) {
			console.log( r.message + " Item Tracked" );
		}
		btn.className = btn.id + '_on';
		btn.childNodes[0].update('TRACKING');
		btn.childNodes[0].pathname = btn.childNodes[0].pathname.replace(/start/, 'stop');
		btn.childNodes[0].blur();
	} else {
		console.log( t.responseText + " Error: not watched" );
	}
}

function HandleStopWatching (t) {
	if( t.responseText == 'ok' ) {
		var btn = $('item_watch');
		if( ! btn ) {
			console.log( r.message + " Item not Tracked" );
		}
		btn.className = btn.id + '_off';
		btn.childNodes[0].update('TRACK');
		btn.childNodes[0].pathname = btn.childNodes[0].pathname.replace(/stop/, 'start');
		btn.childNodes[0].blur();
	} else {
		console.log( t.responseText + " Error: not un watched" );
	}
}


/**
 * PROMOTE/SHARE FUNCTIONS
 */
function showShare() {
	$('share_block').style.display = 'block';
	$('share_show').style.display = 'none';
	$('share_hide').style.display = 'inline';
}

function hideShare() {
	$('share_block').style.display = 'none';
	$('share_show').style.display = 'inline';
	$('share_hide').style.display = 'none';
}


var shareResponse = function(t) {
	hideShare();
	$('share_status').update(t.responseText);
	$('share_status').style.display = "block";
};

/**
* Tag Functions
*/
var deleteTag = function(tag, content_id, tagDiv_id) {
	if( ! confirm( "Delete the tag '"+tag+"'?" ) ) {
		return false;
	}
	var tagDiv = getValue(tagDiv_id);
	var url = '/channel/deletetags/'+tag+'/'+content_id+'/';
	
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			tag: tag,
			content_id: content_id,
			submit: 'submit',
			submitMethod: 'ajax',
			confirm: '1'
		},
		onLoading: function(request) {
			tagDiv.innerHTML = '<img src="/images/common/indicator.gif" />';
		},
		onSuccess: function(t) {
			tagDiv.innerHTML = '';
		},
		onFailure: function(t) {
			// Connection Error
			tagDiv.innerHTML = 'Error!';
		}
	});
	
	return false;
}

/**
* Location Functions
*/
var deleteLocation = function(location, content_id) {
	if( ! confirm( "Delete the location '"+location+"'?" ) ) {
		return false;
	}
	var submitdata = "location=" + location;
	submitdata += "&content_id=" + content_id;
	submitdata += "&submit=submit&submitMethod=ajax&confirm=1";
	
	new Ajax.Updater('locs', '/channel/deletelocations/'+location+'/'+content_id+'/',
							 {method:'post', postBody:submitdata, onFailure:errFunc});
	return false;

}


var embedSelect = function(inputEl, size) {
	var imp = $(inputEl);
	imp.value = imp.value.replace(/(mm|dd|sm)\.jpg/, size+'.jpg');
	imp.focus();
	imp.select();
	
	$('embed_select_sm').className = 'embed_select';
	$('embed_select_mm').className = 'embed_select';
	$('embed_select_dd').className = 'embed_select';
	$('embed_select_'+size).className = 'embed_selected';
}
