
// Optomized from MooTools to jQuery:
function highlight(element, callback) {
	$j('#'+element).css('backgroundColor','#FFFF00');
	$j('#'+element).animate({'backgroundColor':'#FFFFFF'},2000,callback);
}

// Used a lot, can optimize:
function removeProfileItem(actionUrl, id, oldDiv, showingDiv) {
	if( confirm("Are you sure you want to remove this?") ) {
		$j.post(actionUrl,{'id':id}, function(data) {
			var parent = $j('#'+oldDiv).parent();
			$j('#'+oldDiv).remove();
			if( data ) {
				parent.html( parent.html().replace(/<div\s+class\s*=\s*"clear"\s*>\s*<\/div>/g, "") ).append(data+'<div class = "clear"></div>');
			}
		});
	}
}


function removeItem(actionUrl, id, oldDiv, showingDiv) {
	if( confirm("Are you sure you want to remove this?") ) {
		$j.post(actionUrl,{'id':id}, function(data) {
			var parent = $j('#'+oldDiv).parent();
			$j('#'+oldDiv).remove();
			decNums(showingDiv, true);
		});
	}
}

function decNums(e,b){
	if( $(e) == null ) return;
	if( b == null ) b=false;
	var r = /(Showing\s)([0-9]+)(\sof\s)([0-9]+)(.*)/.exec( $j('#'+e).html() );
	$j('#'+e).html("" + r[1] + (b?r[2]-1:r[2]) + r[3] + (r[4]-1) + r[5]);
}

function incNums(e,b){
	if( $(e) == null ) return;
	if( b == null ) b=false;
	var r = /(Showing\s)([0-9]+)(\sof\s)([0-9]+)(.*)/.exec( $j('#'+e).html() );
	$j('#'+e).html("" + r[1] + (b?r[2]+1:r[2]) + r[3] + (r[4]+1) + r[5]);
}


//rotating thumb functions
var changing_thumbs = new Array();
function changeThumb(index, i, num_thumbs, path, premium_flag) {
	if( isNaN(premium_flag) || premium_flag == 0 ) {
		imgBase = path.indexOf('cdn1.image.keezmovies.phncdn.com') > -1 || path.indexOf('cdn1.videothumbs.xtube.com') >- 1 ? '' : 'small' ;
	}
	else {
		imgBase = '0';
	}

	if( changing_thumbs[index] ) {
		if( path.indexOf('{i}') !== -1 ) {
			$j('#'+index).attr('src', path.replace('{i}',i) );
		}
		else if( path.indexOf('{index}') !== -1 ) {
			$j('#'+index).attr('src', path.replace('{index}',i) );
		}
		else {
			if( path.indexOf(".jpg", path.length - 4) !== -1 ) {
				lastIndexOfDash = path.lastIndexOf("/");
				path = path.substring(0,lastIndexOfDash+1);
				$j('#'+index).attr('src', path + i + ".jpg" );
			} else {
				$j('#'+index).attr('src', path + imgBase + i + ".jpg" );
			}
		}
		i = i % num_thumbs + 1;
		changing_thumbs[index] = setTimeout("changeThumb('" + index + "'," + i + ", " + num_thumbs + ", '" + path + "'," + premium_flag + ")", 600);
	}
}


function startThumbChange(index, num_thumbs, path, premium_flag) {	
	changing_thumbs[index] = true;
	changeThumb(index, 1, num_thumbs, path, isNaN(premium_flag) ? 0 : premium_flag);
}


function endThumbChange(index, path, premium_flag, xtube_flag) {
	clearTimeout(changing_thumbs[index]);
	if(isNaN(premium_flag)) {
		premium_flag = 0;
	}
	if( isNaN(xtube_flag) || premium_flag == 0) {
		imgBase = path.indexOf('cdn1.image.keezmovies.phncdn.com')>-1 || path.indexOf('cdn1.videothumbs.xtube.com')>-1 ? '1' : 'small';
	}
	else {
		imgBase = '01';
	}

	if(xtube_flag == 0) {
		if( path.indexOf(".jpg", path.length - 4) !== -1 ) {
			document.getElementById(index).src = path;
		}else{
			document.getElementById(index).src = path + imgBase + ".jpg";
		}
	} else {
		document.getElementById(index).src = path;
	}
}


// Show more/less links:
function showMoreLess(id, more_text, less_text) {
	if( $j('#'+id+'_link').html() == more_text ) {
		$j('#'+id).css({'visibility':'visible','display':'inline'});
		$j('#'+id+"_link").html(less_text);
	} else {
		$j('#'+id).css({'visibility':'hidden','display':'none'});
		$j('#'+id+"_link").html(more_text);
	}
}



// Used by add_photos page:
function AC_AddExtension(src, ext) {
	if( src.indexOf('?') !== -1 )
		return src.replace(/\?/, ext+'?');
	else
		return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) {
	var str = '<';
	if(isIE && isWin && !isOpera) {
		str += 'object';
		for (var i in objAttrs) {
			str += ' ' + i + '="' + objAttrs[i] + '"';
		}
		str += '>';
		for (var i in params) {
			str += '<param name="' + i + '" value="' + params[i] + '" />';
		}
		str += '</object>';
	} else {
		str += 'embed';
		for (var i in embedAttrs) {
			str += ' ' + i + '="' + embedAttrs[i] + '"';
		}
		str += '></embed>';
	}

	document.write(str);
}

function AC_FL_RunContent() {
	var ret = AC_GetArgs(arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000", "application/x-shockwave-flash");
	AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent() {
	var ret = AC_GetArgs(  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000", null);
	AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

	function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
		var ret = new Object();
		ret.embedAttrs = new Object();
		ret.params = new Object();
		ret.objAttrs = new Object();
		for (var i=0; i < args.length; i=i+2){
			var currArg = args[i].toLowerCase();

			switch (currArg){
				case "classid":
					break;
				case "pluginspage":
					ret.embedAttrs[args[i]] = args[i+1];
					break;
				case "src":
				case "movie":
					args[i+1] = AC_AddExtension(args[i+1], ext);
					ret.embedAttrs["src"] = args[i+1];
					ret.params[srcParamName] = args[i+1];
					break;
				case "onafterupdate":
				case "onbeforeupdate":
				case "onblur":
				case "oncellchange":
				case "onclick":
				case "ondblclick":
				case "ondrag":
				case "ondragend":
				case "ondragenter":
				case "ondragleave":
				case "ondragover":
				case "ondrop":
				case "onfinish":
				case "onfocus":
				case "onhelp":
				case "onmousedown":
				case "onmouseup":
				case "onmouseover":
				case "onmousemove":
				case "onmouseout":
				case "onkeypress":
				case "onkeydown":
				case "onkeyup":
				case "onload":
				case "onlosecapture":
				case "onpropertychange":
				case "onreadystatechange":
				case "onrowsdelete":
				case "onrowenter":
				case "onrowexit":
				case "onrowsinserted":
				case "onstart":
				case "onscroll":
				case "onbeforeeditfocus":
				case "onactivate":
				case "onbeforedeactivate":
				case "ondeactivate":
				case "type":
				case "codebase":
				case "id":
					ret.objAttrs[args[i]] = args[i+1];
					break;
				case "width":
				case "height":
				case "align":
				case "vspace":
				case "hspace":
				case "class":
				case "title":
				case "accesskey":
				case "name":
				case "tabindex":
					ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
					break;
				default:
					ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
			}
		}
		ret.objAttrs["classid"] = classid;
		if(mimeType) ret.embedAttrs["type"] = mimeType;
		return ret;
	}



var DragNDrop = new Class({
	initialize: function(options){
		this.indexes = options.indexes;
		this.page = 1;
		this.pages_url = options.pages_url;
		this.image_base = options.image_base;

		this.loadDraggables();
		this.loadDroppables();
	},

	loadDraggables: function(){
		$$("#draggables div.draggable").each (function (drag){
			drag.orig_x = drag.style.left;
			drag.orig_y = drag.style.top;

			var span = drag.getElementsByTagName("span")[0];

			drag.friend_id = span.innerHTML;

			drag.addEvent("mousedown", function(e){
				e = new Event(e).stop();

				var clone = this.clone()
				.setStyles(this.getCoordinates())
				.setStyles({
					"opacity": 0.8,
					"position": "absolute"
				})
				.addEvent("emptydrop", function(){
					this.remove();
				})
				.inject(document.body);

				clone.makeDraggable({
					droppables: $$('#droppables div.droppable')
				}).start(e);
				clone.friend_id = drag.friend_id;
				clone.real = drag;
			});
		});
	},

	loadDroppables: function(){
		var me = this;

		$$("#droppables div.droppable").each (function (drop, index){
			drop.dropPos = index;
			drop.friend_id = me.indexes[index];

			drop.addEvents({
				"over": function (el, obj){
					this.setStyle("background-color", "#333");
				},

				"leave": function (el, obj){
					this.setStyle("background-color", "#111");
				},

				"drop": function (el, obj){
					this.setStyle("background-color", "#111");

					//check the friend ID to see if its already in the list
					var used = -1;
					for (var i = 0; i < me.indexes.length; i++){
						if(i != this.dropPos && me.indexes[i] == el.friend_id){
							used = i;
							break;
						}
					}
					var old = this.innerHTML;

					/*el.style.left = el.orig_x;
					el.style.top = el.orig_y;*/

					if(used < 0){
						this.innerHTML = el.real.innerHTML;

						this.friend_id = el.friend_id;
						me.indexes[this.dropPos] = this.friend_id;
					}else{
						var otherDrop = $("droppable" + used);

						this.innerHTML = otherDrop.innerHTML;
						otherDrop.innerHTML = old;

						var old_id = this.friend_id;
						this.friend_id = me.indexes[otherDrop.dropPos];
						otherDrop.friend_id = old_id;

						me.indexes[otherDrop.dropPos] = old_id;
						me.indexes[this.dropPos] = this.friend_id;
					}

					el.remove();
				}
			});
		});
	},

	getPage: function(page){
		var me = this;
		$("draggables").innerHTML = "<img src = \"" + this.image_base + "ajax-loader.gif\" alt = \"Loading...\" />";
		new Request({
			url: this.pages_url,
			method: "get",
			data: {
				page: page
			},
			onComplete: function(response){
				$("draggables").innerHTML = response;
				me.page = page;
				me.loadDraggables();
			}
		}).send();
	},

	submit: function(){
		var str = "<form action = \"" + window.location.href +
		"\" method = \"post\" id = \"submit_form\">";
		this.indexes.each( function(item, index){
			str += "<input type = \"hidden\" name = \"index[" +
			index + "]\" value = \"" + item + "\" />";
		});
		str += "</form>";

		$("draggables").innerHTML = str;
		$("submit_form").submit();
	},

	removeSpot: function(i){
		var drop = $("droppable" + i);

		drop.friend_id = 0;
		drop.innerHTML = "";
		this.indexes[i] = 0;
	}
});

/**
 * Javascript class for the ThumbsUp plugin
 */
var ThumbsUp = new Class({
	initialize: function(options){
		this.id = options.id;
		this.type = options.type;
		this.submit_url = options.submit_url;
		this.rating = options.rating;
		this.num_ratings = options.num_ratings;
		this.feedback_box = options.feedback_box;
		this.object_name = options.object_name;

		this.locked = false;

		if(options.post_ajax)
			this.post_ajax = options.post_ajax;
		else
			this.post_ajax = function(){}
	},

	rate: function(num){
		if(!this.locked){
			this.locked = true;

			var me = this;
			new Request({
				url: this.submit_url,
				method: "post",
				data: {
					id: this.id,
					value: num
				},
				onComplete: function(response){
					me.feedback_box.innerHTML = response + "<br />";
					me.post_ajax();
				}
			}).send();
		}
	}
});

// jQuery version:
// CONTINUE HERE !!!!!!!!!!!!!!!!!!
( function( $ ) {
	var settings = {};
	var callbacks = {
		set: function(key, val, options ) {
			$.extend(settings, typeof options == "object" ? options : {} );
			if( typeof val == null || typeof val == "undefined" ) {
				settings.expires = -1;
			} else if( typeof settings.expires == "number" ) {
				settings.expires = new Date();
				settings.expires.setDate( settings.expires.getDate() + options.expires );
			}
			return (document.cookie = [
				encodeURIComponent(key), '=',
				settings.raw ? val : encodeURIComponent(val),
				settings.expires ? '; expires=' + settings.expires.toUTCString() : '',
				settings.path ? '; path=' + settings.path : '',
				settings.domain ? '; domain=' + settings.domain : '',
				settings.secure ? '; secure' : ''
			].join(''));
		},
		get: function(key, options) {
			$.extend(settings, typeof options == "object" ? options : {} );
			var value = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)
			return value ? ( settings.raw ? value[1] : decodeURIComponent(value[1]) ) : null;
		}

	};
	$.thumbsUp = function() {
		if( callbacks[arg] ) {
			return callbacks[arg].apply(this);
		} else if( typeof arg === 'object' || !arg ) {
			return callbacks.init.apply(this, arguments);
		} else {
			return this;
		}
	};

} )( jQuery );



/**
 * Javascript class for the Share plugin
 */
var Share = new Class({
	initialize: function(options){
		this.object_id = options.object_id;
		this.submit_url = options.submit_url;
		this.button = options.button;
		this.feedback_box = options.feedback_box;
		this.object_name = options.object_name;
		this.on_complete = options.on_complete;
		this.share_to = options.share_to;
		this.share_name = options.share_name;
		this.share_message = options.share_message;
	},

	send: function(){
		var me = this;
		this.button.disabled = 'disabled';
		this.feedback_box.innerHTML = '<div class="notice">Sending...</div>';
		//alert(this.submit_url);
		new Request({
			url: this.submit_url,
			method: "post",
			data: {
				id: this.object_id,
				to: me.share_to.value,
				name: me.share_name.value,
				message: me.share_message.value
			},
			onComplete: function() {
				me.feedback_box.innerHTML = '<div class="success">Sent successfully</div>';
				setTimeout(me.object_name + ".feedback_box.innerHTML = ''; " + me.object_name + ".button.disabled = '';", 3000);
				me.on_complete();
			}
		}).send();
	}
});



/**
 * Javascript class for the Share plugin
 */
var Flagger = new Class({
	initialize: function(options){
		this.object_id = options.object_id;
		this.submit_url = options.submit_url;
		this.num_types = options.num_types;
		this.button = options.button;
		this.feedback_box = options.feedback_box;
		this.object_name = options.object_name;
		this.on_complete = options.on_complete;
		this.is_producer = options.is_producer;
	},

	send: function(){
		var me = this;

		var selected = 0;
		for (var i = 1; i <= this.num_types; i++){
			if($("flag_" + i).checked){
				selected = i;
				break;
			}
		}

		if(this.is_producer == undefined && ($("flag_" + selected).value == "copyright" || $("flag_" + selected).value == "copy"))
		{
			alert("Please send any copyright reports to copyright@pornhub.com. Thank you.");
			return;
		}

		if(selected == 0)
			this.feedback_box.set('html', 'Please Select a Reason');
		else
		{
			this.button.disabled = 'disabled';
			this.feedback_box.set('html', 'Posting...');
			new Request({
				url: this.submit_url,
				method: "post",
				data: {
					id: this.object_id,
					reason: $("flag_reason").value,
					checked: selected
				},
				onComplete: function() {
					me.feedback_box.set('html', 'Flagged successfully!');
					setTimeout(me.object_name + ".feedback_box.innerHTML = ''; " + me.object_name + ".button.disabled = '';", 3000);
					me.on_complete();
				}
			}).send();
		}
	}
});



var CommentController = new Class(
{
	initialize: function(options)
	{
		this.item_id = options.item_id;
		this.item_owner_id = options.item_owner_id;
		this.item_type = options.item_type;
		this.writer_id = options.writer_id;
		this.comment_box = "comment_box_"+options.unique;
		this.js_object = options.js_object;
		this.unique = options.unique;
		this.video_attacher = options.video_attacher;
		this.action = options.action;
		this.comment_box_index = 1;
		this.max_chars_number = options.max_chars_number;
	},

	show_chars_left: function()
	{
		var chars_left = this.max_chars_number - $("message_" + this.unique).value.length;

		if(chars_left < 0)
			chars_left = 0;

		$("char_left_box_" + this.unique).innerHTML = chars_left;
	},

	postComment: function(url_base)
	{
		if($("message_" + this.unique).value.length > this.max_chars_number)
		{
			$("feedback_" + this.unique).innerHTML = '<div class="profile-box-content-center"><div class="error">You can\'t have more than ' + this.max_chars_number + ' characters.</div></div>';
			return;
		}

		if($("message_" + this.unique).value.match(/^\s*$/))
		{
			$("feedback_" + this.unique).innerHTML = '<div class="profile-box-content-center"><div class="error">You have to type a message.</div></div>';
			return;
		}

		$("feedback_" + this.unique).innerHTML = '<div class="profile-box-content-center"><div class="warning">Posting...</div></div>';

		$("comment_submit_" + this.unique).disabled = true;

		var video_id = (this.video_attacher == null ? 0 : eval(this.video_attacher + ".getVideo()"));
		var rnd_id = Math.floor(Math.random() * 100000000 + 1);
		var me = this;
		var postBody = {
			item_id: this.item_id,
			writer_id: this.writer_id,
			item_owner_id: this.item_owner_id,
			item_type: this.item_type,
			comment: $("message_" + this.unique).value,
			i: this.comment_box_index,
			js_object: this.js_object,
			unique: this.unique,
			video_id: video_id,
			rnd_id: rnd_id
		};

		$("message_" + this.unique).readOnly = true;

		new Request({
			url: url_base,
			method: 'post',
			data: postBody,
			onComplete: function (html) {
				$("message_" + me.unique).readOnly = false;
				$("comment_submit_" + me.unique).disabled = false;

				if(me.video_attacher != null){
					eval(me.video_attacher + ".showPanel(false)");
				}

				if(html.indexOf('time_limit_spam') != -1)
				{
					$("feedback_" + me.unique).innerHTML = '<div class="profile-box-content-center"><div class="error">Please Don\'t Spam!</div></div>';
				}
				else if(html.indexOf('spam_filter_spam') != -1)
				{
					$("feedback_" + me.unique).innerHTML = '<div class="profile-box-content-center"><div class="error">Message Posting Failed!</div></div>';
				}
				else
				{
					if($("no_comments_yet_" + me.unique)) {
						$("no_comments_yet_" + me.unique).style.display = "none";
					}
					$(me.comment_box).innerHTML = html;
					$("num_comments_" + me.unique).innerHTML = $("num_comments_" + me.unique).innerHTML*1+1;
					me.comment_box = 'comment_box_' + me.unique + "_" + me.comment_box_index;
					me.comment_box_index++;

					$("feedback_" + me.unique).innerHTML = '<div class="profile-box-content-center"><div class="success">Message Posted Successfully</div></div>';
					$("message_" + me.unique).value = "";
					me.show_chars_left();
				}

				if(video_id != 0) {
					eval(me.video_attacher + ".setToNull()");
				}

				setTimeout(me.js_object + ".clearFeedbackBox()", 3000);
			}
		}).send();
	},

	deleteComment: function(id, url_base)
	{
		var me = this;
		$("comment_" + me.unique + "_" + id).innerHTML = '<div class="profile-box-content-center"><div class="warning">Deleting...</div></div>';
		new Request({
			url: url_base + "&id=" + id + "&type=" + me.item_type + "&item_id=" + me.item_id,
			method: 'post',
			data: {},
			onComplete: function (html) {
				window.location.href = me.action;
			}
		}).send();
	},

	clearFeedbackBox: function() {
		$("feedback_" + this.unique).innerHTML = "";
	},

	reportSpam: function(id, owner_id, url_base)
	{
		var me = this;

		new Request({
			url: url_base + "&id=" + id + "&type=" + me.item_type + "&owner_id=" + owner_id,
			method: 'post',
			data: {},
			onComplete: function (html) {
				$("report_spam_" + me.unique + "_" + id).innerHTML = "Marked as spam";
			}
		}).send();
	},

	blockUser: function(id, url_base, user_to_block_id, block_or_unblock, hash)
	{
		if(block_or_unblock)
			if(!confirm("Are you sure you want to block this user?"))
				return false;

		var me = this;

		var postBody = {
			id: user_to_block_id,
			block: block_or_unblock,
			hash: hash
		};

		new Request({
			url: url_base,
			method: 'post',
			data: postBody,
			onComplete: function (html) {
				window.location.href = me.action;
			}
		}).send();
	}
});



var DropDown = new Class({

	initialize: function(options){
		this.selectedItem = -1;
		this.unique = options.unique;
		this.tags_box = options.tags_box;
		this.target_url = options.target_url;

		var dd = $("dropdown_" + this.unique), cb = $("chooser_box_" + this.unique);
		dd.setStyle("left", cb.getPosition().x);
		dd.setStyle("top", cb.getPosition().y + 20);
	},

	checkItem: function(event){
		var text = $("chooser_box_" + this.unique).value;

		var key = (event.which ? event.which : event.keyCode);

		if(key == 40 && this.selectedItem < 9)
			this.selectedItem++;
		else if(key == 38 && this.selectedItem > -1)
			this.selectedItem--;

		var me = this;
		if(text.length > 2){
			new Request({
				url: this.target_url,
				method: "get",
				data: {
					text: text,
					selectedItem: this.selectedItem
				},
				onComplete: function(response){
					$("dropdown_" + me.unique).innerHTML = response;
					$("dropdown_" + me.unique).style.display = "block";
				}
			}).send();
		}else{
			$("dropdown_" + this.unique).style.display = "none";
		}
	},

	selectItem: function(){
		this.tags_box.innerHTML += " " +
		(this.selectedItem == -1 ? $("chooser_box_" + this.unique).value : $("selectedItem_" + this.unique).innerHTML);

		$("dropdown_" + this.unique).style.display = "none";
		$("chooser_box_" + this.unique).value = '';
		this.selectedItem = -1;
	},

	highlightItem: function(i){
		this.selectedItem = i;
		var me = this;
		new Request({
			url: this.target_url,
			method: "get",
			data: {
				text: $("chooser_box_" + this.unique).value,
				selectedItem: this.selectedItem,
				unique: this.unique
			},
			onComplete: function(response){
				$("dropdown_" + me.unique).innerHTML = response;
				$("dropdown_" + me.unique).style.display = "block";
			}
		}).send();
	}
});



// JavaScript Document

var ThumbCropper = new Class({

	initialize: function(options) {
		this.left = 10;
		this.top = 10;
		this.ratio = options.thumb_height / options.thumb_width;
		this.min_left = 0;
		this.max_right = options.max_right;
		this.min_top = 0;
		this.max_bottom = options.max_bottom;
		this.min_width = options.min_width;
		this.min_height = options.min_height;
		this.image_filter = (options.image_filter == null ? "" : options.image_filter);
		this.action = options.action;

		this.thumb_width = options.thumb_width;
		this.thumb_height = options.thumb_height;
		this.in_pic = options.in_pic;
		this.out_pic = options.out_pic;
		this.dark_pic = options.dark_pic;

		this.pre_ajax = (options.pre_ajax == null ? function(){} : options.pre_ajax);
		this.post_ajax = (options.post_ajax == null ? function(){} : options.post_ajax);

		this.resizing = false;
		this.moving = false;

		this.container = $('cropper-container');
		this.resizer = $('cropper-resizer');
		this.mover = $('cropper-mover');

		var pos = this.container.getPosition();
		var size = this.mover.getSize();
		this.container_left = pos.x;
		this.container_top = pos.y;
		this.mover_width = size.x;
		this.mover_height = size.y;
		var pos = this.mover.getPosition();
		this.mover_left = pos.x - this.container_left;
		this.mover_top = pos.y - this.container_top;

		$('cropper-container-border').addEvent("mouseenter", this.enter.bind(this));
		$('cropper-container-border').addEvent("mouseout", this.out.bind(this));
		document.addEvent("mouseup", this.mouseUp.bind(this));
		document.addEvent("mousemove", this.move.bind(this));
		this.resizer.addEvent("mousedown", this.resizeMouseDown.bind(this));
		this.mover.addEvent("mousedown", this.moveMouseDown.bind(this));

		this.x_offset = 0;
		this.y_offset = 0;
	},

	enter: function(event) {
		event = new Event(event);

	},

	out: function(event) {
		event = new Event(event);
	},

	mouseUp: function(event) {
		event = new Event(event);

		if(this.resizing)
		{
			this.resizing = false;
		}

		if(this.moving)
		{
			this.moving = false;
		}
	},

	move: function(event) {
		event = new Event(event);

		var difference = 0;

		if(this.moving)
		{
			var mouse_x = event.page.x - this.container_left;
			var mouse_y = event.page.y - this.container_top;

			if(mouse_x - this.x_offset + this.mover_width > this.max_right)
				this.mover_left = this.max_right - this.mover_width;
			else if(mouse_x - this.x_offset < this.min_left)
				this.mover_left = this.min_left;
			else
				this.mover_left = mouse_x - this.x_offset;

			if(mouse_y - this.y_offset + this.mover_height > this.max_bottom)
				this.mover_top = this.max_bottom - this.mover_height;
			else if(mouse_y - this.y_offset < this.min_top)
				this.mover_top = this.min_top;
			else
				this.mover_top = mouse_y - this.y_offset;

			//reset offset (since sometimes cursor can move when box doesn't
			this.x_offset = mouse_x - this.mover_left;
			this.y_offset = mouse_y - this.mover_top;

			this.mover.setStyle("top",this.mover_top);
			this.mover.setStyle("left",this.mover_left);
			//this.mover.setStyle("background-position", "-" + (this.mover_left + 1) + "px -" + (this.mover_top + 1) + "px;");
			this.mover.style.backgroundPosition = "-" + (this.mover_left + 1) + "px -" + (this.mover_top + 1) + "px";
			this.resizer.setStyle("top",this.mover_top + this.mover_height);
			this.resizer.setStyle("left",this.mover_left + this.mover_width);
		}
		else if(this.resizing)
		{
			difference = event.page.x - this.container_left - this.mover_left - this.mover_width;
			var do_resize = false;



			if(this.mover_left + this.mover_width + difference <= this.max_right &&
				this.mover_width + difference > this.min_width &&
				parseInt(this.mover_top + this.mover_height + (difference * this.ratio)) <= this.max_bottom &&
				parseInt(this.mover_height + (difference * this.ratio)) > this.min_height)
				{
				this.mover_width += difference;
				this.mover_height = parseInt(this.mover_width * this.ratio);

				this.mover.setStyle("width",this.mover_width - 2);        //-2 for border
				this.mover.setStyle("height",this.mover_height - 2);    //-2 for border
				this.resizer.setStyle("top",this.mover_top + this.mover_height);
				this.resizer.setStyle("left",this.mover_left + this.mover_width);

			}

		}
	},

	resizeMouseDown: function(event) {
		event = new Event(event);
		this.resizing = true;
		this.moving = false;
	},

	moveMouseDown: function(event) {
		event = new Event(event);
		this.moving = true;
		this.resizing = false;

		this.x_offset = event.page.x - this.mover_left - this.container_left;
		this.y_offset = event.page.y - this.mover_top  - this.container_top;
	},

	crop: function() {
		this.pre_ajax();

		$("cropper_msg").innerHTML = "Cropping...";

		var postBody = {
			in_pic: this.in_pic,
			out_pic: this.out_pic,
			left: this.mover_left,
			top: this.mover_top,
			width: this.mover_width,
			height: this.mover_height,
			thumb_width: this.thumb_width,
			thumb_height: this.thumb_height,
			image_filter: this.image_filter
		};
		var out_pic = this.out_pic;
		var me = this;
		new Request({
			url: this.action,
			method: 'post',
			data: postBody,
			onComplete: function(response){
				$("cropper_msg").innerHTML = "Done!";
				setTimeout("$('cropper_msg').innerHTML = '';", 3000);
				me.post_ajax(response);
			}
		}).send();
	},

	recrop: function() {
		$('cropper').setStyle("display", "block");
		$('cropper').setStyle("visibility", "visible");
		$('thumb_preview').setStyle("display", "none");
		$('thumb_preview').setStyle("visibility", "hidden");
	},

	cancel: function() {
		$('thumb_preview').setStyle("display", "block");
		$('thumb_preview').setStyle("visibility", "visible");
		$('cropper').setStyle("display", "none");
		$('cropper').setStyle("visibility", "hidden");
	}

});



/**
 * Javascript class for the Share plugin
 */
var FeedbackPoll = new Class({
	initialize: function(options){
		this.message_box = options.message_box;
		this.button = options.button;
		this.feedback_box = options.feedback_box;
		this.object_name = options.object_name;
		this.submit_url = options.submit_url;
		this.num_options = options.num_options;
		this.on_complete = options.on_complete;
		this.referral_url = options.referral_url;
		this.feedback_id = options.feedback_id;
		this.user_id = options.user_id;
		this.browsing_info = options.browsing_info;
	},

	send: function(){
		var me = this;

		var selected = 0;
		for (var i = 1; i <= this.num_options; i++){
			if($(this.object_name + '_' + i).checked){
				selected = $(this.object_name + '_' + i).value;
				break;
			}
		}

		if(selected == 0)
			this.feedback_box.innerHTML = '<div class="error">Please Select a Reason</div>';
		else if(selected == 'other' && this.message_box.value == "")
			this.feedback_box.innerHTML = '<div class="error">Please enter a message if you choose the option "Other"</div>';
		else
		{
			this.button.disabled = 'disabled';
			this.feedback_box.innerHTML = '<div class="notice">Posting...</div>';
			new Request({
				url: this.submit_url,
				method: "post",
				data: {
					message: me.message_box.value + me.browsing_info,
					option_id: selected,
					referral_url: me.referral_url,
					feedback_id: me.feedback_id,
					user_id: me.user_id
				},
				onComplete: function(html) {
					if(html == "spam")
					{
						me.feedback_box.innerHTML = '<div class="error">You cannot report problems this fast! SLOW DOWN!!!</div>';
						me.button.disabled = '';
					}
					else
					{
						me.message_box.value = '';
						me.feedback_box.innerHTML = html;
						setTimeout(me.object_name + ".feedback_box.innerHTML = ''; " + me.object_name + ".button.disabled = '';", 3000);
						me.on_complete();
					}
				}
			}).send();
		}
	}
});



var SideScroller = new Class({
	initialize: function(options){
		this.divHeight = options.divHeight;
		this.divWidth = options.divWidth;
		this.numItems = options.numItems;
		this.itemWidth = options.itemWidth;
		this.scrollingDiv = options.scrollingDiv;

		this.scrollPos = 0;
		this.increments = 5;
		this.timer = 0;

		var sl = options.scrollLeft;
		var sr = options.scrollRight;

		var stop = function() {
			scroller.stopScroll();
			return false;
		};

		var me = this;

		sl.addEvent("mouseup", function(){
			me.scrollingDiv.scrollLeft = me.scrollPos = 0;
		});
		sl.addEvent("mouseover", function(){
			scroller.scrollLeft();
			return false;
		});
		sl.addEvent("mouseout", stop);
		sl.style.cursor = "pointer";

		sr.addEvent("mouseup", function(){
			me.scrollPos = me.numItems * me.itemWidth - me.divWidth;
			me.scrollingDiv.scrollLeft = me.scrollPos;
		});
		sr.addEvent("mouseover", function(){
			scroller.scrollRight();
			return false;
		});
		sr.addEvent("mouseout", stop);
		sr.style.cursor = "pointer";
	},

	scrollLeft: function(){
		if(this.timer) clearTimeout(this.timer);

		this.scrollPos = Math.max(this.scrollPos - this.increments, 0);
		this.scrollingDiv.scrollLeft = this.scrollPos;

		this.timer = setTimeout("scroller.scrollLeft()", 15);
	},

	scrollRight: function(){
		if(this.timer) clearTimeout(this.timer);

		this.scrollPos = Math.min(this.scrollPos + this.increments, this.numItems * this.itemWidth - this.divWidth);
		this.scrollingDiv.scrollLeft = this.scrollPos;

		this.timer = setTimeout("scroller.scrollRight()", 15);
	},

	stopScroll: function(){
		clearTimeout(this.timer);
		this.timer = 0;
	}
});



var VideoAttacher = new Class({
	initialize: function(options){
		this.selected_id = options.selected_id;
		this.image_base = options.image_base;
		this.page_url = options.page_url;
	},

	showPanel: function(show){
		$("video_attacher_panel").style.display = (show ? "block" : "none");
	},

	getPage: function(page, type){
		this.selected_id = 0;
		$("video_attacher_content").innerHTML = "<img src = \"" + this.image_base + "ajax-loader.gif\" alt = \"\" />";
		new Request({
			url: this.page_url,
			method: "get",
			data: {
				page: page,
				type: type,
				selected_id: this.selected_id
			},
			onComplete: function(response){
				$("video_attacher_content").innerHTML = response;
			}
		}).send();
	},

	clickVideo: function(id){
		if(this.selected_id)
			$("video_box_" + this.selected_id).style.border = "1px solid #000";

		$("video_box_" + id).style.border = "1px dashed #F98E00";
		this.selected_id = id;
	},

	attachVideo: function(){
		if(this.selected_id != $("attached_video_id").value)
		{
			$("attached_video_id").value = this.selected_id;
			this.selected_id = 0;
		}
	},

	getVideo: function(){
		return this.selected_id;
	},

	setToNull: function(){
		this.selected_id = 0;
	}
});


// Filters menu wraper show/hide function:
function filters_show(which) {

	$j('.jc-submenu-wrapper').css('display','none');
	$j('#toprated').css('display','none');
	$j('#mostviewedsubmenu').css('display','none');

	if( which != 'none' ) {
		$j('#'+which).css('display','block');
		$j('.jc-submenu-wrapper').css('display','block');
	}
}


/* **** THIS CAN EASILY BE TURNED INTO A JQUERY PLUGIN ****
// Helper function to replace a tag's CSS class.
// Action is a string composed of "className [add|remove] classNameAddition" .
function replaceClass(tag, action) {
	var params = action.split(/\s/i);
	if( params[1] == 'add' ) {
		if( $j(tag).hasClass(params[0]) ) {
			$j(tag).removeClass(params[0]);
			$j(tag).addClass(params[0]+'_'+params[2]);
		}
	} else if( params[1] == 'remove' ) {
		if( $j(tag).hasClass(params[0]+'_'+params[2]) ) {
			$j(tag).removeClass(params[0]+'_'+params[2]);
			$j(tag).addClass(params[0]);
		}
	}
}
*/

/***
 * Lightbox/popup handling using jQuery.
 */

var lightboxCSS 	   = null;
var lightboxContent    = null;
var lightboxBackground = null;

// Lightbox object.
var lightbox = {
	show: function(route,w,h,e) {
		
		//		var scroll = document.viewport.getScrollOffsets();
		lightboxCSS = {
			'display':'block',
			'width': w+'px',
			'height': (h>0?h+'px':'auto')
		}

		$j.ajax({
			type: "POST",
			dataType: "html",
			url: route,
			success: function(data,status) {
				 
				lightboxContent
					.html(data)
					.css( lightboxCSS );
				
				recalcLightbox();
				lightboxBackground.css({'display':'block'});
		  
				lightboxBackground.show();
		 
				
			}
		});
	},
	hide: function() {
		lightboxContent.hide();
		lightboxBackground.hide();
	}
}


// We need to handle this so the dimmed background works good.
function recalcLightbox(e) {
	if( $j('div.lightbox_content')[0] )
	{
		lightboxContent.css( {
			'top': (180/*+$j(document).scrollTop()*/)+'px',
			'left': ( ($j(document).width()-lightboxContent.innerWidth())/2) +'px'
		});
		
	}
}


/* Fixing background for search input box for PS3 */
function fixPs3() {
	if(navigator.userAgent && navigator.userAgent.match(/PLAYSTATION/)) 
	{
		document.getElementById("search_value").style.height = "20px";
		document.getElementById("search_value").style.backgroundImage = "none";
		document.getElementById("search_value").style.backgroundColor = "white";
		document.getElementById("search_value").style.width = "151px";
	}
}

// TO BE REPLACED BY JQUERY PLUGIN
function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if(x==c_name) {
			return unescape(y);
		}
	}
}

// TO BE REPLACED BY JQUERY PLUGIN
function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function block(url, id, block, token, update) {
	$j.post(url + '?block=' + block + '&id=' + id + '&layout=off&token=' + token + '&update=' + update, function(data) {
		if(update) {
			if(document.getElementById(update)) {
				document.getElementById(update).innerHTML = data;
			} else {
				jQuery('span.' + update).html(data);
			}
		} else {
			window.location.reload();// Add some event handlers to the window :
			$j(document).ready( function(e) {
				
				lightboxContent    = $j('div.lightbox_content'),
				lightboxBackground = $j('div.lightbox_background');
				
				if( lightboxContent[0] )
				{
					lightboxBackground
						.css('opacity',0.5)
						.click(lightbox.hide);
					$j(window)
						.resize(recalcLightbox)
						.scroll(recalcLightbox);
				}

			/* JavaScript For Language Flags */
				if($j('div.flag-wrapper')[0]) {
					$j('li.flag').mouseover(function() {
						$j('div.language-marker').show();
						$j('div.language-marker').css("right", -1*($j(this).position().left-$j('li.flags-title').width()-(65+38+6)-$j('li.flag').length*32));
						$j('div.language-marker').text($j(this).find("span").text());
					});
					$j('li.flag').mouseout(function() {
						$j('div.language-marker').hide();
					});
				}
			});
		}
	});
}



/* Rewrite: put template in-line code here */

// DOM ready 
$j(document).ready( function(e) {
	// Add some event handlers to the window:
	lightboxContent    = $j('div.lightbox_content');
	lightboxBackground = $j('div.lightbox_background');
	if( lightboxContent[0] ) {
		lightboxBackground
			.css('opacity',0.5)
			.click(lightbox.hide);
		$j(window)
			.resize(recalcLightbox)
			.scroll(recalcLightbox);
	}

	// JavaScript For Language Flags:
	if($j('div.flag-wrapper')[0]) {
		$j('li.flag').mouseover(function() {
			$j('div.language-marker').show();
			$j('div.language-marker').css("right", -1*($j(this).position().left-$j('li.flags-title').width()-(65+38+6)-$j('li.flag').length*32));
			$j('div.language-marker').text($j(this).find("span").text());
		});
		$j('li.flag').mouseout(function() {
			$j('div.language-marker').hide();
		});
	}
});
