var commentsLoadMask = null;
var pageLoadMask = null;
var ratingsMask = null;
function submitCommentsForm(image_id) {
	if ( commentsLoadMask == null ) {
		commentsLoadMask = new Ext.LoadMask('comments');
	}
	commentsLoadMask.show();
	$('add_image_comment').request({
		onComplete: function(t) {
			if ( t.responseJSON.success ) {
				new Ajax.Updater('media_comments',HOME_DIR+'user_image_comments/profile_list/'+image_id,{
					onSuccess: function() {
						commentsLoadMask.hide();
					}
				});
				$('comment_text').value = '';
			}
			else {
				Ext.Msg.alert('Error',t.responseJSON.error.reasons);
				commentsLoadMask.hide();
			}
		},
		on404: function() {
			Ext.Msg.alert('Error','Not Found!');
			commentsLoadMask.hide();
		},
		onFailure: function() {
			Ext.Msg.alert('Error','Failed!');
			commentsLoadMask.hide();
		},
		evalJSON: true,
		sanitizeJSON: true
	});
}
Ext.QuickTips.init();
SendToFriendForm = function(loggedin) {
	if ( ! loggedin ) {
		this.formItems = [
			{
				name: 'your_email',
				allowBlank: false,
				vtype: 'email',
				fieldLabel: 'Your Email',
				validationDelay: 1000
			}
			,{
				name: 'your_name',
				allowBlank: false,
				minLength: 3,
				fieldLabel: 'Your Name',
				validationDelay: 1000
			}
			,{
				name: 'friend_email',
				allowBlank: false,
				vtype: 'email',
				fieldLabel: 'Freind Email',
				validationDelay: 1000
			}
			,{
				name: 'friend_name',
				allowBlank: false,
				minLength: 3,
				fieldLabel: 'Friend Name',
				validationDelay: 1000
			}
		];
	}
	else {
		this.formItems = [
			{
				name: 'friend_email',
				allowBlank: false,
				vtype: 'email',
				fieldLabel: 'Freind Email',
				validationDelay: 1000
			}
			,{
				name: 'friend_name',
				allowBlank: false,
				minLength: 3,
				fieldLabel: 'Friend Name',
				validationDelay: 1000
			}
		];
	}
	SendToFriendForm.superclass.constructor.call(this,{
		autoScroll: true,
		defaultType: 'textfield',
		labelWidth: 80,
		labelAlign: 'right',
		bodyStyle: 'padding: 10px',
		items: this.formItems
	});
};
Ext.extend(SendToFriendForm,Ext.form.FormPanel);
SendToFriendWindow = function(image_id,loggedin) {
	this.image_id = image_id;
	this.loggedin = loggedin;
	this.form = new SendToFriendForm(loggedin);
	var cfg = {
		width: 300,
		modal: true,
		title: 'Send To A Friend',
		layout: 'fit',
		items: [ this.form ],
		bbar: [
			{
				text: 'Send Email',
				handler: this.submitForm,
				scope: this
			}
			,{
				text: 'Cancel',
				handler: function() {
					this.close();
				},
				scope: this
			}
		]
	};
	if ( loggedin ) {
		Ext.apply(cfg,{ height: 135 });	
	}
	else {
		Ext.apply(cfg,{ height: 190 });	
	}
	SendToFriendWindow.superclass.constructor.call(this,cfg);
};
Ext.extend(SendToFriendWindow,Ext.Window,{
	submitForm: function() {
		this.form.getForm().submit({
			url: HOME_DIR+'user_images/sendtofriend/'+this.image_id,
			success: function(f,a) {
				if ( a.result.success ) {
					Ext.Msg.alert('Email Sent','An email was sent to your friend.');
					this.close();
				}
				else {
					Ext.Msg.show({
						title: 'Error!',
						msg: a.result.error.reasons,
						buttons: Ext.MessageBox.OK,
						icon: Ext.Msg.ERROR
					});
				}
			},
			failure: function(f,a) {
				if ( typeof(a.result.error) != "undefined" ) {
					Ext.Msg.show({
						title: 'Error!',
						msg: a.result.error.reasons,
						buttons: Ext.MessageBox.OK,
						icon: Ext.Msg.ERROR
					});
				}
				else {
					Ext.Msg.show({
						title: 'Error!',
						msg: 'Unable to submit your information',
						buttons: Ext.MessageBox.OK,
						icon: Ext.Msg.ERROR
					});
				}
			},
			scope: this
		});
	}
});
function sendToFriend(image_id,logged_in) {
	var sendWin = new SendToFriendWindow(image_id,logged_in);
	sendWin.show();
}

function markImageAsSpam(image_id) {
	if ( pageLoadMask == null ) {
		pageLoadMask = new Ext.LoadMask(document.body);
	}
	pageLoadMask.show();
	new Ajax.Request(HOME_DIR+'user_images/spam/'+image_id,{
		onComplete: function(t) {
			if ( t.responseJSON.success ) {
				$('spam_link').hide();
				pageLoadMask.hide();
			}
			else {
				Ext.Msg.alert('Error',t.responseJSON.error.reasons);
				pageLoadMask.hide();
			}
		},
		on404: function() {
			Ext.Msg.alert('Error','Not Found!');
			pageLoadMask.hide();
		},
		onFailure: function() {
			Ext.Msg.alert('Error','Failed!');
			pageLoadMask.hide();
		},
		evalJSON: true,
		sanitizeJSON: true
	});
}
function deleteImage(image_id) {
	if ( pageLoadMask == null ) {
		pageLoadMask = new Ext.LoadMask(document.body);
	}
	pageLoadMask.show();
	new Ajax.Request(HOME_DIR+'user_images/user_delete/'+image_id,{
		onComplete: function(t) {
			if ( t.responseJSON.success ) {
				Ext.Msg.alert('Success','This image has been deleted');
				document.location.href = HOME_DIR+'user_profiles/view';
				pageLoadMask.hide();
			}
			else {
				Ext.Msg.alert('Error',t.responseJSON.error.reasons);
				pageLoadMask.hide();
			}
		},
		on404: function() {
			Ext.Msg.alert('Error','Not Found!');
			pageLoadMask.hide();
		},
		onFailure: function() {
			Ext.Msg.alert('Error','Failed!');
			pageLoadMask.hide();
		},
		evalJSON: true,
		sanitizeJSON: true
	});
}


function removeFromFavorites(favorite_id,image_id) {
	if ( pageLoadMask == null ) {
		pageLoadMask = new Ext.LoadMask(document.body);
	}
	pageLoadMask.show();
	new Ajax.Request(HOME_DIR+'favorites/delete/'+favorite_id,{
		onComplete: function(t) {
			if ( t.responseJSON.success ) {
				$('favorite_link').innerHTML = 'Add to favorites';
				$('favorite_link').onclick = eval('function() { addToFavorites('+image_id+'); return false; };');
				pageLoadMask.hide();
				Ext.Msg.alert('Success','This image has been removed from your favorites');
			}
			else {
				Ext.Msg.alert('Error',t.responseJSON.error.reasons);
				pageLoadMask.hide();
			}
		},
		on404: function() {
			Ext.Msg.alert('Error','Not Found!');
			pageLoadMask.hide();
		},
		onFailure: function() {
			Ext.Msg.alert('Error','Failed!');
			pageLoadMask.hide();
		},
		evalJSON: true,
		sanitizeJSON: true
	});
}
function addToFavorites(image_id) {
	if ( pageLoadMask == null ) {
		pageLoadMask = new Ext.LoadMask(document.body);
	}
	pageLoadMask.show();
	new Ajax.Request(HOME_DIR+'user_images/add_to_favorites/'+image_id,{
		onComplete: function(t) {
			if ( t.responseJSON.success ) {
				$('favorite_link').innerHTML = 'Remove favorite';
				$('favorite_link').onclick = eval('function() { removeFromFavorites('+t.responseJSON.favorite_id+','+image_id+'); return false; };');
				pageLoadMask.hide();
				Ext.Msg.alert('Success','This image has been added to your favorites');
			}
			else {
				Ext.Msg.alert('Error',t.responseJSON.error.reasons);
				pageLoadMask.hide();
			}
		},
		on404: function() {
			Ext.Msg.alert('Error','Not Found!');
			pageLoadMask.hide();
		},
		onFailure: function() {
			Ext.Msg.alert('Error','Failed!');
			pageLoadMask.hide();
		},
		evalJSON: true,
		sanitizeJSON: true
	});
}
var starTO = null;
var rated = false;
function doStarOut() {
	for ( var i = 0; i < 5; i++ ) {
		if ( typeof($('star'+i).origsrc) == 'string' ) {
			$('star'+i).src = $('star'+i).origsrc;
		}
	}
}
function starOut(num) {
	if ( ! rated ) {
		starTO = setTimeout('doStarOut('+num+');',300);
	}
}
function starOver(num) {
	if ( ! rated ) {
		if ( starTO != null ) {
			clearTimeout(starTO);
			starTO = null;
		}
		for ( var i = 0; i < 5; i++ ) {
			if ( i <= num ) {
				if ( typeof($('star'+i).origsrc) != 'string' ) {
					$('star'+i).origsrc = $('star'+i).src;
				}
				$('star'+i).src = HOME_DIR+'images/gold_star.gif';
			}
			else {
				if ( typeof($('star'+i).origsrc) == 'string' ) {
					$('star'+i).src = $('star'+i).origsrc;
				}
			}
		}
	}
}
function rateImage(image_id,rating) {
	rated = true;
	if ( ratingsMask == null ) {
		ratingsMask = new Ext.LoadMask('ratings');
	}
	ratingsMask.show();
	for ( var i = 0; i < 5; i++ ) {
		$('starlink'+i).title = 'You rated this '+rating+' stars.';
		new Ajax.Request(HOME_DIR+'user_image_ratings/rate/'+image_id+'/'+rating,{
			onComplete: function(t) {
				if ( t.responseJSON.success ) {
					ratingsMask.hide();
				}
				else {
					Ext.Msg.alert('Error',t.responseJSON.error.reasons);
					ratingsMask.hide();
				}
			},
			on404: function() {
				Ext.Msg.alert('Error','Not Found!');
				ratingsMask.hide();
			},
			onFailure: function() {
				Ext.Msg.alert('Error','Failed!');
				ratingsMask.hide();
			},
			evalJSON: true,
			sanitizeJSON: true
		});
	}
}