var search = {
	title: 'Søgeord',
	onsubmit: function(elm) {
		return (elm.q.value == this.title) ? false : true;
	},
	onfocus: function(elm) {
		if (elm.value == this.title) elm.value='';
		elm.className = 'inputFocus';
	},
	onblur: function(elm) {
		if (elm.value == '') elm.value = this.title;
		elm.className = 'input';
	}
};

$(function() {
	if ($('.resultlist .pagination').length) {
		function changePage(page) {
			if (!page) {
				page = 1;
			}
			function switchList($list) {
				$('.resultlist').hide();
				$list.show();
			}
	
			$list = $('.resultlist[data-page='+page+']');
			if ($list.length) {
				switchList($list);
			} else {
				var url = '';
				if ($('.resultlist').hasClass('cover')) {
					url = 'index/coverlist?page='+page+'&category=' + $('.resultlist').attr('data-category');
				} else if ($('.resultlist').hasClass('searchresult')) {
					url = 'result?page='+page+'&q=' + $('.resultlist').attr('data-q');
				} else if ($('.resultlist').hasClass('tag')) {
					url = 'tagresult?page='+page+'&tag=' + $('.resultlist').attr('data-tag');
				} else if ($('.resultlist').hasClass('cast')) {
					url = 'castresult?page='+page+'&actor=' + $('.resultlist').attr('data-actor');
				}
				$.get(url, function(data) {
					$list = $(data);
					$('.resultlist_wrapper').append($list);
					switchList($list);
					$('.listView a[href*=parent_category], .largeView a[href*=parent_category]').each(function() {
						var href = $(this).attr('href');
						$(this).attr('href', href.replace(/(#[0-9]+)?$/, '#'+page));
					});
				});
			}
		}
		
		$('.resultlist .pagination a').live('click', function(e) {
			e.preventDefault();
			e.stopPropagation();
			
			var page = $(this).attr('data-page');
			$.historyLoad(page);
		});
		
		$.historyInit(changePage);
	}
	var voteUrl = 'index/register-vote?media_id='+$('#vote-media').val()+'&vote=';
	function voted() {
		$('#vote-yes').parent().addClass('inactive');
		$('#vote-yes').attr('src', $('#vote-yes').attr('src').replace(/dildo_stor.png/, 'dildo_stor_inactive.png'));
		$('#vote-no').attr('src', $('#vote-no').attr('src').replace(/dildo_lille.png/, 'dildo_lille_inactive.png'));
		$('#vote-yes').attr('src', $('#vote-yes').attr('src').replace(/dildo_stor_mouseover.png/, 'dildo_stor_inactive.png'));
		$('#vote-no').attr('src', $('#vote-no').attr('src').replace(/dildo_lille_mouseover.png/, 'dildo_lille_inactive.png'));
	}
	$('#vote-yes').click(function(e) {
		$(this).blur();
		e.preventDefault();
		e.stopPropagation();
		if ($(this).parents('.inactive').length) {
			return;
		}
		$.get(voteUrl+'1', voted);
	});
	$('#vote-no').click(function(e) {
		$(this).blur();
		e.preventDefault();
		e.stopPropagation();
		if ($(this).parents('.inactive').length) {
			return;
		}
		$.get(voteUrl+'0', voted);
	});
	$('#vote-yes').hover(function() {
		$('#vote-yes').attr('src', $('#vote-yes').attr('src').replace(/dildo_stor.png/, 'dildo_stor_mouseover.png'));
	}, function() {
		$('#vote-yes').attr('src', $('#vote-yes').attr('src').replace(/dildo_stor_mouseover.png/, 'dildo_stor.png'));
	});
	$('#vote-no').hover(function() {
		$('#vote-no').attr('src', $('#vote-no').attr('src').replace(/dildo_lille.png/, 'dildo_lille_mouseover.png'));
	}, function() {
		$('#vote-no').attr('src', $('#vote-no').attr('src').replace(/dildo_lille_mouseover.png/, 'dildo_lille.png'));
	});
	
	$(".top_rated").ready(function() {
		$(".top_rated:not(.selected)").live('click', function(e) {
			e.preventDefault();
			e.stopPropagation();
			$(this).parents('.box').load('index/top-rated?category='+$(this).parent().attr('data-category'));
			$.cookie('rating_module', 'top_rated');
		});
		$(".top6:not(.selected)").live('click', function(e) {
			e.preventDefault();
			e.stopPropagation();
			$(this).parents('.box').load('index/top6?category='+$(this).parent().attr('data-category'));
			$.cookie('rating_module', 'top6');
		});
		$(".top30:not(.selected)").live('click', function(e) {
			e.preventDefault();
			e.stopPropagation();
			$(this).parents('.box').load('index/top30?category='+$(this).parent().attr('data-category'));
			$.cookie('rating_module', 'top30');
		});
		if ($.cookie('rating_module')) {
			$('.'+$.cookie('rating_module')+':not(.selected)').click();
		}
	});
});
