$(document).ready( function() {
	boxr = new boxr();
	
	/* search autofocus */
	$('#header_search_query').focus();
	/* search autofocus */
	
	/* ajax login */
	$('#header-upload').live('click', function() {
		boxr.show('login', true, function() {
			if (boxr.get('input:text').val()!=''&&boxr.get('input:password').val()!='') {
				$.ajax({
					url : boxr.get('form').attr('action'),
					type : 'post',
					dataType : 'json',
					data : boxr.get('form').serializeArray(),
					beforeSend : function() { boxr.get('p').css({'color':'#888', 'padding-top':'10px'}) .text('Sending request...');},
					error : function() { boxr.get('p').css('color','#c00') .text('An unknown error has occured. Please, try later...');},
					success : function(data) {
						if (data.status=='ok') {
							if ($('#contents[rel="login"]').length) location.href = "/";
							else {
								$.ajax({
									url : location.href + '#ajax',
									beforeSend : function() { boxr.request('Please, wait a bit...', true);},
									error : function() { boxr.error('An unknown error has occured.<br />Please, refresh the page...', true);},
									success : function(data) {
										$('#userbar-inner').html($('#userbar-inner',data).html());
										if ($('#contents[rel="social"]').length) {
											$('#contents').html($('#contents',data).html());
											loadPictures();
										} else {
											if ($('#contents_related_comments_wrap').length) $('#contents_related_comments_wrap').html($('#contents_related_comments_wrap',data).html());
											if ($('#contents-post-actions').length) $('#contents-post-actions').html($('#contents-post-actions', data).html());
										};
										boxr.success('You have been successfully logged in!', true);
									}
								});
							};
						}
						else {
							var errors = '';
							$.each(data.errors, function (key, value) { errors += value + '<br />';});
							errors = errors.replace(/(<br \/>)$/,'');
							boxr.get('p').css('color','#c00') .html(errors);
						};
					}
				});
			}
			else { boxr.get('p').css({'color':'#c00', 'padding-top':'10px'}) .text('Please, fill in all the fields...');};
		});
		return false;
	});
	/* ajax login */
	
	/* ajax retrieve */
	$('a[href*="/lostpassword/"]').live('click', function() {
		boxr.show('retrieve', true, function() {
			if (boxr.get('input:text:eq(0)').val()!='') {
				$.ajax({
					url : boxr.get('form').attr('action'),
					type : 'post',
					dataType : 'json',
					data : boxr.get('form').serializeArray(),
					beforeSend : function() { boxr.get('p').css({'color':'#888', 'padding-top':'10px'}) .text('Sending request...');},
					error : function() { boxr.get('p').css('color','#c00') .text('An unknown error has occured. Please, try later...');},
					success : function(data) {
						if (data.status=='ok') { boxr.success('Please, check your e-mail for further instructions', true);}
						else {
							var errors = '';
							$.each(data.errors, function (key, value) { errors += value + '<br />';});
							errors = errors.replace(/(<br \/>)$/,'');
							boxr.get('p').css('color','#c00') .html(errors);
						};
					}
				});
			}
			else { boxr.get('p').css({'color':'#c00', 'padding-top':'10px'}) .text('Please, fill in the field...');};
		});
		return false;
	});
	/* ajax retrieve */
	
	/* post «like» & «dislike» buttons» */
	$('#contents-post-actions a:eq(0), #contents-post-actions a:eq(1)').live('click',function() {
		var cookies = document.cookie.toString().indexOf('postrate');
		if (cookies==-1) {
			$.ajax({
				url : '/?action=vote',
				type : 'post',
				dataType : 'json',
				data : {'object':window.post.type, 'object_id':window.post.id, 'vote':$(this).attr('vote'), 'post':'vote'},
				error : function() { boxr.error('An unknown error has occured. Please, try later...');},
				success : function(data) {
					if (data.status=='ok') {
						var date = new Date();
						date.setYear(date.getFullYear() + 1);
						document.cookie = 'postrate=true; expires=' + date.toUTCString();
						$.ajax({
							url : location.href + '#ajax',
							error : function() { boxr.error('Error while retrieving post rating.<br />Please, refresh the page...');},
							success : function(data) { 
								$('#contents-post-actions').html($('#contents-post-actions', data).html());
								$('#contents-post-actions a:eq(0), #contents-post-actions a:eq(1)').addClass('active');
							}
						});
					}
					else { boxr.error(data.data);};
				}
			});
		}
		else { boxr.error('You have already voted for this post recently');};
		return false;
	});
	/* post «like» & «dislike» buttons» */
	
	/* model «like» & «dislike» buttons» */
	$('#contents-post-actions-model a:eq(0), #contents-post-actions-model a:eq(1)').live('click',function() {
		//var cookies = document.cookie.toString().indexOf('postrate');
		//if (cookies==-1) {
			$.ajax({
				url : '/?action=vote',
				type : 'post',
				dataType : 'json',
				data : {'object':window.post.type, 'object_id':window.post.id, 'vote':$(this).attr('vote'), 'post':'vote'},
				error : function() { boxr.error('An unknown error has occured. Please, try later...');},
				success : function(data) {
					if (data.status=='ok') {
						var date = new Date();
						date.setYear(date.getFullYear() + 1);
						//document.cookie = 'postrate=true; expires=' + date.toUTCString(); 
						$.ajax({
							url : location.href + '#ajax',
							error : function() { boxr.error('Error while retrieving post rating.<br />Please, refresh the page...');},
							success : function(data) { 
								$('#contents-post-actions-model').html($('#contents-post-actions-model', data).html());
								$('#contents-post-actions-model a:eq(0), #contents-post-actions-model a:eq(1)').addClass('active');
							}
						});
					}
					else { boxr.error(data.data);};
				}
			});
		//}
		//else { boxr.error('You have already voted for this post recently');};
		return false;
	});
	/* model «like» & «dislike» buttons» */	
	
	/* models alphabetical filter */
	$('#models-filter a').live('click', function() {
		var current = $(this).parents('#models-filter').find('span > span').get();
		$('#models_rating-' + $(current).text()).hide();
		$('#models-' + $(current).text()).hide();
		$('#models_toprating_letter-' + $(current).text()).hide();
		$('#models_listall_letter-' + $(current).text()).hide();
		$(current).parent('span').after('<a title="Switch models">' + $(current).text() + '</a>') .remove();
		$('#models-' + $(this).text()).show();
		$('#models_rating-' + $(this).text()).show();
		$('#models_toprating_letter-' + $(this).text()).show();
		$('#models_listall_letter-' + $(this).text()).show();
		$(this).after('<span><span>' + $(this).text() + '</span></span>') .remove();
		return false;
	});
	/* models alphabetical filter */	
	
	/* favourites */
	$('#contents-post-fav-add').live('click', function() {
		if ($(this).attr('href')!='/login/') {
			$.ajax({
				url : '/?action=add_to_favorites',
				type : 'post',
				dataType : 'json',
				data : {'object':window.post.type, 'object_id':window.post.id, 'post':'true'},
				beforeSend : function() { boxr.request('Please, wait a bit...');},
				error : function() { boxr.error('An unknown error has occured.<br />Please, try later...', true);},
				success : function(data) {
					if (data.status=='ok') { boxr.success('Post was successfully added to your favourites', true);}
					else { boxr.error(data.data, true);};
				}
			});
			return false;
		}else{
		return true;
		};
	});
	
	$('a[act="remove_fav"]').click( function() {
		var _this = $(this).get();
		var params = $(this).attr('object').split(':');
		$.ajax({
			url : '/?action=remove_favorites',
			type : 'post',
			dataType : 'json',
			data : {'object':params[0], 'object_id':params[1], 'post':'true'},
			error : function() { boxr.error('An unknown error has occured.<br />Please, try later...');},
			success : function(data) {
				if (data.status!='error') $(_this).parents('div.contents_gallery_item_wrap').animate({'filter':'alpha(opacity=0)','opacity':'0'},500, function() { $(this).remove();});
				else boxr.error('An unknown error has occured.<br />Please, try later...');
			}
		});
		return false;
	});
	/* favourites */
	
	/* report */
	$('a[act="show_report"]').click( function() {
		var postdata;
		if ($(this).attr('uid')) postdata = {'object':'user', 'object_id':$(this).attr('uid'), 'comment':'', 'post':'action'};
		else {
			if ($(this).attr('oid')) postdata = {'object':'photo', 'object_id':$(this).attr('oid'), 'comment':'', 'post':'action'};
			else postdata = {'object':window.post.type, 'object_id':window.post.id, 'comment':'', 'post':'action'};
		};
		boxr.show('report-abuse', true, function() {
			postdata.comment = boxr.get('textarea').val();
			if (postdata.comment) {
				$.ajax({
					url : '/?action=add_report',
					type : 'post',
					dataType : 'json',
					data : postdata,
					beforeSend : function() { boxr.get('p:eq(1)').css({'color':'#888','padding-top':'5px'}) .text('Sending request. Please, wait...');},
					error : function() { boxr.get('p:eq(1)').css('color','#c00') .text('An unknown error has occured');},
					success : function(data) {
						if (data.status=='error') { boxr.get('p:eq(1)').css('color','#c00') .text(data.data);}
						else { boxr.success('Thank you for sending the report!', true);};
					}
				});
			}
			else { boxr.get('p:eq(1)').css({'color':'#c00','padding-top':'5px'}) .text('Please, fill in the comment field...');};
		});
		return false;
	});
	
	$('a[act="report"]').live('click',function(){
		var params = $(this).attr('object').split(':');
		boxr.confirm('Do you really want to report this message as spam?', true, function() {
			$.ajax({
				url : '/?action=add_report',
				type : 'post',
				dataType : 'json',
				data : {'object':params[0], 'object_id':params[1], 'post':'action'},
				error : function() { boxr.error('An unknown error has occured.<br />Please, try later...', true);},
				success : function(data) {
					if (data.status=='ok') { boxr.success('Thank you for sending the report!', true);}
					else { boxr.error('An unknown error has occured.<br />Please, try later...', true);};
				}
			});
		});
		return false;
	});
	/* report */
	
	/* movie download */
	window.download = false;
	$('#contents-post-download').live('click', function() {
		if ($(this).attr('href')!='/login/') {
			if (!window.download) {
				$.ajax({
					url : '/?action=show_download_link&gallery_id=' + window.post.id,
					dataType : 'json',
					beforeSend : function() { $('#contents-post-download-container').text('Sending request. Please, wait...'); $('#contents-post-download-container').slideDown();},
					error : function() {
						$('#contents-post-download-container').html('<span style="color: #c00;">An unknown error has occured. Please, try again later!</span>');
						setTimeout( function() { $('#contents-post-download-container').slideUp(); $('#contents-post-download-container').html('');}, 2000);
					},
					success : function(data) {
						if (data.status=='ok') {
							window.download = {
								link : data.data.download_url,
								size : data.data.filesize
							};
							$('#contents-post-download-container').html('<a href="' + window.download.link + '" title="Click here to download this movie">Click here to download this movie (' + window.download.size + ')</a>');
							setTimeout( function() { $('#contents-post-download-container').slideUp(); $('#contents-post-download-container').html('');}, 10000);
						}
						else {
							$('#contents-post-download-container').html('<span style="color: #c00;">An unknown error has occured. Please, try again later!</span>');
							setTimeout( function() { $('#contents-post-download-container').slideUp(); $('#contents-post-download-container').html('');}, 2000);
						};
					}
				});
			}
			else {
				$('#contents-post-download-container').html('<a href="' + window.download.link + '" title="Click here to download this movie">Click here to download this movie (' + window.download.size + ')</a>');
				$('#contents-post-download-container').slideDown();
				setTimeout( function() { $('#contents-post-download-container').slideUp(); $('#contents-post-download-container').html('');}, 10000);
			};
			return false;
		}else{
		return true;
		};
	});
	/* movie download */
	
	/*messages*/
	$('a[act="reply_msg"]').click(function(){
		id_msg = $(this).attr('id_msg');
		if ($('div.msg_report_form_div').length>0) $('.msg_report_form_div').hide();
		$('div.msg_reply_form_div[id_msg="'+id_msg+'"]').slideDown('fast');
		return false;
	});
	
	$('input[act="submit_msg_cancel_reply"]').click(function(){
		id_msg = $(this).attr('id_msg');
		$('.msg_reply_form_div[id_msg="'+id_msg+'"]').slideUp('fast');
		$('.reply_result[id_msg="'+id_msg+'"]').html('');
	});
	
	$('input[act="submit_msg_reply"]').click(function(){
		id_msg = $(this).attr('id_msg');
		$('.reply_error[id_msg="' + id_msg + '"]').html('');
		if (!$('.msg_reply_text[id_msg="' + id_msg + '"]').val()) $('.reply_error[id_msg="' + id_msg + '"]').html('Please, fill in the field...');
		else {
			$.ajax({
				url : '/?action=send_message',
				type : 'post',
				dataType : 'json',
				data : {'to':$('#msg_to').val(), 'msg_text':$('.msg_reply_text[id_msg="'+id_msg+'"]').val(), 'post':'action'},
				error : function() { boxr.error('An unknown error has occured.<br />Please, try again later...');},
				success : function(data) {
					if (data.status!='error') {
						$('.msg_reply_form_div[id_msg="' + id_msg + '"]').hide();
						$('.msg_reply_text[id_msg="'+ id_msg +'"]').val('');
						boxr.success('Message sent!', true);
					}
					else {
						$('.reply_error[id_msg="' + id_msg + '"]').html(data.data);
					};
				}
			});
		};
		return false;
	});
	
	$('a[act="delete_msg"]').click(function(){
		var mid = $(this).attr('id_msg');
		var type = $(this).attr('type');
		boxr.confirm('Do you really wan to delete this message?', true, function() {
			$.ajax({
				url : '/?action=delete_message',
				type : 'post',
				dataType : 'json',
				data : {'id_msg':mid, 'post':'action'},
				error : function() { boxr.error('An unknown error has occured.<br />Please, try again later', true);},
				success : function(data) {
					if (data.status=='ok') {
						boxr.hide();
						if (type=='listing') {
							$('.contents_msg[id_msg="' + mid + '"]').hide('slow');
						} else if (type=='inside') {
							$('a[act="delete_msg"][id_msg="' + mid + '"]').after('<span>Was deleted</span>').remove();
						}
					}
					else { boxr.error('An unknown error has occured.<br />Please, try again later', true);};
				}
			});
		});
		return false;
	});
	/*messages*/
	
	/* block & unblock */
	$('a[act="block_user"]').live('click', function() {
		var _this = $(this).get();
		boxr.confirm('Do you really want to block this user?', true, function() {
			$.ajax({
				url : '/?action=manage_block_user',
				type : 'post',
				dataType : 'json',
				data : {'other_user':$(_this).attr('uid'), 'post':'block'},
				error : function() { boxr.error('An unknown error has occured.<br />Please, try again later', true);},
				success : function(data) {
					if (data.status=='ok') { 
						$('a[act="block_user"][uid="' + $(_this).attr('uid') + '"]').html('Unblock user').attr('act','unblock_user');
						boxr.success('This user won\'t bother you anymore', true);
					}
					else boxr.error(data.data, true);
				}
			});
		});
		return false;
	});
	
	$('a[act="unblock_user"]').live('click',function() {
		var _this = $(this).get();
		$.ajax({
			url : '/?action=manage_block_user',
			type : 'post',
			dataType : 'json',
			data : {'other_user':$(this).attr('uid'), 'post':'unblock'},
			beforeSend : function() { boxr.request('Please, wait a bit...');},
			error : function() { boxr.error('An unknown error has occured.<br />Please, try again later...', true);},
			success : function(data) {
				if (data.status=='ok') {
					$('a[act="unblock_user"][uid="' + $(_this).attr('uid') + '"]').html('Block user').attr('act','block_user');
					boxr.success('User was unblocked successfully', true);
				}
				else boxr.error('An unknown error has occured.<br />Please, try again later...', true);
			}
		});
		return false;
	});
	/* block & unblock */
	
	/* friends */
	$('a[act="add_to_friends"]').live('click',function(){
		$.ajax({
			url : '/?action=manage_friends',
			type : 'post',
			dataType : 'json',
			data : {'other_user':$(this).attr('other_user'), 'post':'add_to_friends'},
			error : function() { boxr.error('An unknown error has occured.<br />Please, try again later...', true);},
			success : function(data) {
				console.log(data);
				if (data.status=='ok') boxr.success('This user will become your friend after approving the request', true);
				else boxr.error(data.data, true);
			}
		});
		return false;
	});
	
	$('a[act="approve_friend"]').live('click',function(){
		var link = $(this).get();
		$.ajax({
			url : '/?action=manage_friends',
			type : 'post',
			dataType : 'json',
			data : {'other_user':$(this).attr('other_user'), 'post':'approve_friend'},
			error : function() { boxr.error('An unknown error has occured.<br />Please, try again later...', true);},
			success : function(data) {
				if (data.status=='ok') {
					if (!$(link).hasClass('friend-request')) $('a[act="approve_friend"][other_user="'+other_user+'"]').hide().after('<span>This user is your friend now</span>');
					else $(link).parents('p.contents-useractions').html('This user is your friend now');
				}
				else { boxr.error(data.data, true);};
			}
		});
		return false;
	});
	
	$('a[act="ignore_request"]').live('click',function(){
		var link = $(this).get();
		$.ajax({
			url : '/?action=manage_friends',
			type : 'post',
			dataType : 'json',
			data : {'other_user':$(this).attr('other_user'), 'post':'ignore_request'},
			error : function() { boxr.error('An unknown error has occured.<br />Please, try again later...', true);},
			success : function(data) {
				if (data.status=='ok') $(link).parents('p.contents-useractions').html('Friend request ignored');
				else { boxr.error(data.data, true);};
			}
		});
		return false;
	});
	
	$('a[act="delete_from_friends"]').live('click',function(){
		other_user = $(this).attr('other_user');
		$.ajax({
			url : '/?action=manage_friends',
			type : 'post',
			dataType : 'json',
			data : {'other_user':$(this).attr('other_user'), 'post':'delete_from_friends'},
			error : function() { boxr.error('An unknown error has occured.<br />Please, try again later...', true);},
			success : function(data) {
				if (data.status=='ok') {
					$('a[act="delete_from_friends"][other_user="'+other_user+'"]').html('Add to friends').attr('act','add_to_friends');
					boxr.success('User was deleted from your friend list successfully', true);
				}
				else { boxr.error(data.data, true);};
			}
		});
		return false;
	});
	/* friends */
	
	/* comment posting */
	$('#post-comments form, #photo-comments form, #profile-wall form').live('submit', function() {
		var _this = $(this).get();
		$.ajax({
			url : '/?action=add_comment',
			type : 'post',
			dataType : 'json',
			data : $(this).serialize(),
			beforeSend : function() { $(_this).children('span').css('visibility','visible');},
			error : function() { $(_this).children('span').css('visibility','hidden'); boxr.error('An unknown error has occured.<br />Please, try again later...', true);},
			success : function(data) {
				if (data.status=='ok') {
					$('#comments').html(data.data);
					$('#comments_cnt_wrap').html("(" + data.cnt + ")");
					$(_this).find('textarea').val('');
					$(_this).children('span').css('visibility','hidden');
				} else {
					$(_this).children('span').css('visibility','hidden');
					boxr.error(data.data, true);
				};
			}
		});
		return false;
	});
	/* comment posting */
	
	/* ajax comments paginator */
	$('#comments-paging a').live('click',function() {
		var params = $(this).attr('object').split(':');
		$.post(
			'/?action=show_comments',
			{
				post      : 'show',
				object 	  : params[0],
				object_id : params[1],
				page      : params[2]
			},
			function (data) {
				if (data.status=='ok') {
					$('#comments').html(data.data);
					$(document).scrollTop($('#comments').offset().top-250);
				} else if (data.status=='error') {
					boxr.error(data.data, true);
				}				
			},
			"json"
		);
		return false;
	});
	/* ajax comments paginator */
	
	/* search form redirect to a URL similar to /search/%searchquery%/ */
	$('#header_search > form').submit( function() {
		if ($(this).children('input:text').val()!='') location.pathname = '/search/' + $(this).children('input:text').val().replace(/\s/g,'+') + '/';
		return false;
	});
	/* search form redirect to a URL similar to /search/%searchquery%/ */
	
	/* bookmark */
	$('a.bookmark-link').click( function() {
		_gaq.push(['_trackPageview','bookmark_button']);
		if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel("Free HQ Porn Videos — Pornerbros.com","http://www.pornerbros.com/", "");
		else {
			if (typeof window.external == "object" && navigator.userAgent.toLowerCase().indexOf('webkit')==-1) window.external.AddFavorite("http://www.pornerbros.com/", "Free HQ Porn Videos — Pornerbros.com");
			else boxr.alert('To bookmark this page, please, press Ctrl+D<br />(or Cmd+D under MacOS)', true, true);
		};
		return false;
	});
	
	window.isCtrl = false;
	$(document).keyup( function(event) { if (event.which==17) window.isCtrl=false;});
	$(document).keydown( function(event) {
		if (event.which==17) window.isCtrl=true;
		if (event.which==68&&window.isCtrl) _gaq.push(['_trackPageview','bookmark_by_ctrl_d']);
	});
	/* bookmark */
	
	/* post comments */
	$('#post-comments h6 a').click( function() {
		if ($('#post-comments-inner').css('display')!='block') {
			$('#post-comments-inner').slideDown();
			$(this).text('Hide comments') .attr('title','Hide comments');
		} else {
			$('#post-comments-inner').slideUp();
			$(this).text('Show comments') .attr('title','Show comments');
		};
		return false;
	});
	/* post comments */
	
	/* post «related» menu */
	$('#contents_related_menu a').live('click', function() {
		var index = {
			show : $('#contents_related_menu li').index($(this).parents('li')),
			hide : $('#contents_related_menu li').index($('#contents_related_menu .contents_related_menu_active'))
		};
		$('#contents_related_menu .contents_related_menu_active').find('span').html('<a href="">' + $('#contents_related_menu .contents_related_menu_active').find('span').html() + '</a>');
		$('#contents_related_menu .contents_related_menu_active').removeClass('contents_related_menu_active');
		$(this).parents('li').addClass('contents_related_menu_active');
		$('div.contents_related_item').eq(index.hide).hide();
		$('div.contents_related_item').eq(index.show).show();
		$(this).parent('span').text($(this).text());
		return false;
	});
	/* post «related» menu */
	
	/* post «related videos» rotation */
	$('.contents-related-more > a').click( function() {
		var container = $(this).parent('p').parent('div').parent('div').get();
		var page = $(this).parent('p').parent('div').get();
		$(page).hide();
		if ($(page).next('div').length>0) $(page).next('div').show();
		else $(container).children('div:first-child').show();
		return false;
	});
	/* post «related videos» rotation */
	
	/* slideshow */
	if (!$('div.contents-gallery-categories').length) {
		if ($('div.contents_gallery_item').length) var images = new Array();
		$('div.contents_gallery_item').each( function() {
			var tmp = $(this).find('img').attr('src').replace(/^(.*\/)(\d+)(_\d+x\d+\.jpg)$/,'$1;$2;$3').split(';');
			var image = {
				path : tmp[0],
				main : tmp[1],
				postfix : tmp[2],
				thumbs : $(this).find('img').attr('thumbs').split(';')
			};
			images.push(image);
		});
		
		$('div.contents_gallery_item > h3 > a').live('mouseover mouseout', function(event) {
			if (event.type=='mouseover') {
				$(this).children('img').addClass('slideshow');
				if (!$(this).children('img').hasClass('sliding')) slideShow($(this).children('img').get());
			} else $(this).children('img').removeClass('slideshow');
		});
		
		function slideShow(image, count) {
			var index = $('div.contents_gallery_item').index($(image).parent('a').parent('h3').parent('div'));
			if ($(image).hasClass('slideshow')) {
				$(image).addClass('sliding');
				var tmp = {
					thumb : new Image(),
					time : 500,
					count : 0,
					intl : setInterval( function() { tmp.count++;}, 10)
				};
				
				count = count ? count : 0;
				tmp.thumb.onload = function() {
					$(image).attr('src',tmp.thumb.src);
					clearInterval(tmp.intl);
					count = count<images[index].thumbs.length-1 ? count + 1 : 0;
					tmp.time = (tmp.time - tmp.count*10 > 0) ? tmp.time - tmp.count : 0;
					setTimeout( function() { slideShow(image, count);}, tmp.time);
				};
				tmp.thumb.src = images[index].path + images[index].thumbs[count] + images[index].postfix;
			}
			else {
				$(image).removeClass('sliding');
				$(image).attr('src',images[index].path + images[index].main + images[index].postfix);
			};
		};
	};
	/* slideshow */
	
	/* album adding and editing */
	if ($('#contents-newalbum select').length>0) {
		var intl = setInterval( function() {
			if (jqueryCFSelectLoaded) {
				$('#contents-newalbum select').cf_select();
				clearInterval(intl);
			};
		}, 10);
		
		$('#contents-newalbum select').change(function(){
			if ($(this).val()==0) $('#album-name, #album-privacy').show();
			else $('#album-name, #album-privacy').hide();
		});
	};
	
	$('#create-album-form').submit( function() {
		if (!$(this).find('option:selected').val()&&$('#new_album_name_input').val()=='') {
			$('#new_album_name_error').text('Album name shouldn\'t be empty');
			setTimeout( function() { $('#new_album_name_error').animate({'filter':'alpha(opacity=0)','opacity':'0'},500,function() { $(this).text('') .css({'filter':'alpha(opacity=100)','opacity':'1'});});}, 1500);
			return false;
		};
	});
	
	$('a[act="delete_album"]').click( function() {
		var aid = $(this).attr('album_id');
		boxr.confirm('Do you really want to delete this album?', true, function() {
			$.ajax({
				url : '/?action=delete_album',
				type : 'post',
				dataType : 'json',
				data : {'album_id':aid, 'post':'delete_album'},
				error : function() { boxr.error('An unknown error has occured.<br/ >Please, try later...', true);},
				success : function(data) {
					if (data.status=='ok') { boxr.success('Album deleted successfully!<br />Redirecting you back to your albums...'); setTimeout( function() { location.href = $('#contents-useralbum-actions a:first-child').attr('href');}, 2000);}
					else { boxr.error(data.data, true);};
				}
			});
		});
		return false;
	});
	/* album adding and editing */
	
	/* photo editing */
	$('a[act="use_as_cover"]').click( function() {
		var _this = $(this).get();
		$.ajax({
			url : '/?action=use_as_cover',
			type : 'post',
			dataType : 'json',
			data : {'photo_id':$(_this).attr('photo_id'), 'post':'use_as_cover'},
			error : function() { boxr.error('An unknown error has occured.<br/ >Please, try later...', true);},
			success : function(data) {
				if (data.status=='ok') { boxr.success('Photo successfully set as an album cover', true);}
				else { boxr.error(data.data, true);}
			}
		});
		return false;
	});
	
	$('a[act="delete_photo"]').click( function() {
		var pid = $(this).attr('photo_id');
		boxr.confirm('Do you really want to delete this photo?', true, function() {
			$.ajax({
				url : '/?action=delete_photo',
				type : 'post',
				dataType : 'json',
				data : {'photo_id':pid, 'post':'delete_photo'},
				error : function() { boxr.error('An unknown error has occured.<br/ >Please, try later...', true);},
				success : function(data) {
					if (data.status=='ok') {
						boxr.success('Photo deleted successfully!<br />Redirecting you ' + ($('#photo img').parent('a').length ? 'to the next photo...' : 'back to the album...')); setTimeout( function() { location.href = $('#photo img').parent('a').length ? $('#photo img').parent('a').attr('href') : $('#photo-nav a:eq(2)').attr('href');}, 2000);
					}
					else { boxr.error(data.data, true);};
				}
			});
		});
		return false;
	});
	/* photo editing */
	
	/* upload videos editing */
	$('a[act="delete_video"]').click(function(){
		var gid = $(this).attr('gallery_id');
		boxr.confirm('Do you really want to delete this video?', true, function() {
			$.ajax({
				url : '/?action=delete_video',
				type : 'post',
				dataType : 'json',
				data : {'gallery_id':gid, 'post':'delete_video'},
				error : function() { boxr.error('An unknown error has occured.<br/ >Please, try later...', true);},
				success : function(data) {
					if (data.status=='ok') { $('tr[gallery_id="'+gallery_id+'"]').hide(); boxr.success('Video deleted successfully', true);}
					else boxr.error(data.data, true);
				}
			});
		});
		return false;
	});
	/* upload videos editing */
	
	/* uploading photos */
	if ($('#fileInput').length) {
		$('#fileInput').uploadify({
			'uploader'       : '/js/uploadify.swf',
			'script'         : '?action=add_photo',
			'cancelImg'      : 'http://edge01.images.pornerbros.com/design/img/cancel.png',
			'folder'         : '/upload',
			'fileDesc'		 : 'Image files',
			'fileExt'		 : '*.jpeg;*.jpg;*.gif;*.png',	
			'auto'           : false,
			'multi'          : true,
			'buttonImg'		 : 'http://edge01.images.pornerbros.com/design/img/browse.png',
			'buttonText' 	 : '',
			'cancelImg'		 : 'http://edge01.images.pornerbros.com/design/img/clear.gif',
			'width'			 : '80',
			'height'		 : '25',
			'scriptData'	 : {'login':$('#c_login').val(),'pass':$('#c_pass').val(),'album_id':$('#album_id').val(),'post':'add_photo'}
		});
	};
	/* uploading photos */
	
	/* photo comments */
	$('#photo-comments h6 a').click( function() {
		if ($('#photo-comments-inner').css('display')!='block') {
			$('#photo-comments-inner').slideDown();
			$(this).text('Hide comments') .attr('title','Hide comments');
		} else {
			$('#photo-comments-inner').slideUp();
			$(this).text('Show comments') .attr('title','Show comments');
		};
		return false;
	});
	/* photo comments */
	
	/* photo «like» & «dislike» buttons» */
	$('#photo-like, #photo-dislike').live('click',function() {
		var cookies = document.cookie.toString().indexOf('photorate');
		if (cookies==-1) {
			$.ajax({
				url : '/?action=vote',
				type : 'post',
				dataType : 'json',
				data : {'object':'photo', 'object_id':$(this).attr('oid'), 'vote':$(this).attr('vote'), 'post':'vote'},
				error : function() { boxr.error('An unknown error has occured. Please, try later...');},
				success : function(data) {
					if (data.status=='ok') {
						var date = new Date();
						date.setYear(date.getFullYear() + 1);
						document.cookie = 'photorate=true; expires=' + date.toUTCString();
						$.ajax({
							url : location.href + '#ajax',
							error : function() { boxr.error('Error while retrieving photo rating.<br />Please, refresh the page...');},
							success : function(data) { $('#photo-actions').html($('#photo-actions', data).html());}
						});
					} else boxr.error(data.data);
				}
			});
		}
		else { boxr.error('You have already voted for this photo recently');};
		return false;
	});
	/* photo «like» & «dislike» buttons» */
	
	/* photo and albums preview loading */
	function loadPictures()  {
		$('p.photo-picture').each( function() {
			var image = $(this).find('img').get();
			var intl = setInterval( function() {
				if ($(image).height()) {
					$(image).css({'margin-left':'-' + $(image).width()/2 + 'px', 'margin-top':'-' + $(image).height()/2 + 'px', 'visibility':'visible'});
					clearInterval(intl);
				};
			}, 10);
		});
	};
	loadPictures();
	/* photo and albums preview loading */
	
	/* photo and albums pseudo-hover */
	$('p.photo-picture img, p.photo-picture span').live('mouseover mouseout', function(event) {
		if (event.type=='mouseover') $(this).parents('p').next('p').children('a:eq(0)').addClass('hover');
		else $(this).parents('p').next('p').children('a:eq(0)').removeClass('hover');
	});
	/* photo and albums pseudo-hover */
	
	/* user widget pseudo-hover */
	$('a.user_pic_link').live('mouseover mouseout', function(event) {
		if (event.type=='mouseover') $(this).parent('div').next('div').children('h6').children('a').addClass('hover');
		else $(this).parent('div').next('div').children('h6').children('a').removeClass('hover');
	});
	/* user widget pseudo-hover */
	
	/* IE6 hacks */
	if ($.browser.msie&&$.browser.version==6) {
		$('#header_menu_categories').hover( function() { $('#header_menu_categories_submenu').show();}, function() { $('#header_menu_categories_submenu').hide();});
		$('#header_menu_top').hover( function() { $('#header_menu_top_submenu').show();}, function() { $('#header_menu_top_submenu').hide();});
		$('#header-user-menu').hover( function() { $(this).find('ul').show();}, function() { $(this).find('ul').hide();});
		$('a.contents_gallery_remove').hover( function() { $(this).css('background-position','0px -32px');}, function() { $(this).css('background-position','0px 0px');});
	};
	/* IE6 hacks */
	
	/* All-IE hacks for checkboxes and radios */
	if ($.browser.msie) {
		$('#contents_edit_sex_male, #contents_edit_sex_female').each( function() {
			if ($(this).attr('checked')=='checked'||$(this).attr('checked')==true) $(this).next('label').css('background-position','-375px -140px');
		});
		$('#contents_edit_sex_male, #contents_edit_sex_female').change( function() {
			$(this).parent('div').find('label.contents_edit_checkbox_label').css('background-position','-375px -126px');
			$(this).next('label').css('background-position','-375px -140px');
		});
		
		$('#contents_edit_notification_no, #contents_edit_notification_yes').each( function() {
			if ($(this).attr('checked')=='checked'||$(this).attr('checked')==true) $(this).next('label').css('background-position','-375px -140px');
		});
		$('#contents_edit_notification_no, #contents_edit_notification_yes').change( function() {
			$(this).parent('div').find('label.contents_edit_checkbox_label').css('background-position','-375px -126px');
			$(this).next('label').css('background-position','-375px -140px');
		});
		
		$('#contents_upload_categories input').each( function() {
			if ($(this).attr('checked')=='checked'||$(this).attr('checked')==true) $(this).next('label').css('background-position','-375px -140px');
		});
		$('#contents_upload_categories ul label').click( function() {
			if ($(this).prev('input:checkbox').attr('checked')=='checked'||$(this).prev('input:checkbox').attr('checked')==true) $(this).css('background-position','-375px -126px');
			else $(this).css('background-position','-375px -140px');
		});
		
		$('#contents-newalbum-privacy input').each( function() {
			if ($(this).attr('checked')=='checked'||$(this).attr('checked')==true) {
				$('#contents-newalbum-privacy label').css('background-postition','-375px -126px');
				$(this).next('label').css('background-position','-375px -140px');
			};
		});
		$('#contents-newalbum-privacy label').click( function() {
			if (!($(this).prev('input:radio').attr('checked')=='checked'||$(this).prev('input:radio').attr('checked')==true)) {
				$('#contents-newalbum-privacy label').css('background-position','-375px -126px');
				$(this).css('background-position','-375px -140px');
			};
		});
		
		$('#contents-upload-privacy input').each( function() {
			if ($(this).attr('checked')=='checked'||$(this).attr('checked')==true) {
				$('#contents-upload-privacy label').css('background-postition','-375px -126px');
				$(this).next('label').css('background-position','-375px -140px');
			};
		});
		$('#contents-upload-privacy label').click( function() {
			if (!($(this).prev('input:radio').attr('checked')=='checked'||$(this).prev('input:radio').attr('checked')==true)) {
				$('#contents-upload-privacy label').css('background-position','-375px -126px');
				$(this).css('background-position','-375px -140px');
			};
		});
	};
	/* All-IE hacks for checkboxes and radios */
	
	/* file upload in Profile Edit section with IE hack */
	$('#contents_edit_avatar_upload, #contents_edit_photo_upload, #contents_upload_file').change( function() {
		if (!$.browser.msie) $(this).nextAll('p').text($(this).val().split('\\')[$(this).val().split('\\').length-1]);
		else {
			_this = $(this).get();
			var intl = setInterval( function() {
				if ($(_this).val()) {
					$(_this).nextAll('p').text($(_this).val().split('\\')[$(_this).val().split('\\').length-1]);
					clearInterval(intl);
				};
			}, 10);
		};
	});
	if ($.browser.msie) {
		$('#contents_edit_avatar_upload, #contents_edit_photo_upload, #contents_upload_file').click( function() {
			$(this).change();
		});
	};
	/* file upload in Profile Edit section with IE hack */
	
	/* community last users ajax pagination */
	$("a[act='community_last_users_pagination']").live("click", function() {
		url = $(this).attr("href");
		$.ajax({
			url : url,
			//data : {subject : 'models'},
			success : function(data) {
				var pagination = $(data).find("#community_last_users_pagination").html();
				var users = $(data).find("#latestusers").html();
				//var count = $(data).find("#models_found").html();
				if (pagination) $("#community_last_users_pagination").html(pagination);
				if (users) $("#latestusers").html(users);
				//if (count) $("#models_found").html(count);
			}
		});
		return false;
	});
	/* community last users ajax pagination */
	
	/* community last photos ajax pagination */
	$("a[act='community_last_photos_pagination']").live("click", function() {
		url = $(this).attr("href");
		$.ajax({
			url : url,
			success : function(data) {
				var pagination = $(data).find("#community_last_photos_pagination").html();
				var photos = $(data).find("#contents-latestphotos").html();
				if (pagination) $("#community_last_photos_pagination").html(pagination);
				if (photos) $("#contents-latestphotos").html(photos);
				loadPictures();
			}
		});
		return false;
	});
	/* community last photos ajax pagination */
	
	/* community last private photos ajax pagination */
	$("a[act='community_last_private_photos_pagination']").live("click", function() {
		url = $(this).attr("href");
		$.ajax({
			url : url,
			success : function(data) {
				var pagination = $(data).find("#community_last_private_photos_pagination").html();
				var photos = $(data).find("#contents-latestprivatephotos").html();
				if (pagination) $("#community_last_private_photos_pagination").html(pagination);
				if (photos) $("#contents-latestprivatephotos").html(photos);
				loadPictures();
			}
		});
		return false;
	});
	/* community last private photos ajax pagination */
	
	/* community last videos ajax pagination */
	$("a[act='community_last_videos_pagination']").live("click", function() {
		url = $(this).attr("href");
		$.ajax({
			url : url,
			//data : {subject : 'models'},
			success : function(data) {
				var pagination = $(data).find("#community_last_videos_pagination").html();
				var videos = $(data).find("#contents_gallery").html();
				//var count = $(data).find("#models_found").html();
				if (pagination) $("#community_last_videos_pagination").html(pagination);
				if (videos) $("#contents_gallery").html(videos);
				//if (count) $("#models_found").html(count);
				images = new Array();
				$('div.contents_gallery_item').each( function() {
					var tmp = $(this).find('img').attr('src').replace(/^(.*\/)(\d+)(_\d+x\d+\.jpg)$/,'$1;$2;$3').split(';');
					var image = {
						path : tmp[0],
						main : tmp[1],
						postfix : tmp[2],
						thumbs : $(this).find('img').attr('thumbs').split(';')
					};
					images.push(image);
				});
			}
		});
		return false;
	});
	/* community last videos ajax pagination */
	
	/* community last private videos ajax pagination */
	$("a[act='community_last_private_videos_pagination']").live("click", function() {
		url = $(this).attr("href");
		$.ajax({
			url : url,
			//data : {subject : 'models'},
			success : function(data) {
				var pagination = $(data).find("#community_last_private_videos_pagination").html();
				var videos = $(data).find("#contents_gallery_private").html();
				//var count = $(data).find("#models_found").html();
				if (pagination) $("#community_last_private_videos_pagination").html(pagination);
				if (videos) $("#contents_gallery_private").html(videos);
				//if (count) $("#models_found").html(count);
				images = new Array();
				$('div.contents_gallery_item').each( function() {
					var tmp = $(this).find('img').attr('src').replace(/^(.*\/)(\d+)(_\d+x\d+\.jpg)$/,'$1;$2;$3').split(';');
					var image = {
						path : tmp[0],
						main : tmp[1],
						postfix : tmp[2],
						thumbs : $(this).find('img').attr('thumbs').split(';')
					};
					images.push(image);
				});
			}
		});
		return false;
	});
	/* community last private videos ajax pagination */
});
