var Rotator, ratedText = "";
var minW = 974;
var widthWindow;


function KEEEEEEZ() {
	$('#related_pornstar_videos_container').children().filter('div').each(function(key,val) {
		if( $(val).attr("id") == "related_videos_hd" ) {
			hdVidsCur = 0;
			hdVidsHook = $(val).children().filter('ul');
			hdVidsTotal = $(hdVidsHook).children().filter('li').length;
		} else {
			relVidsCur[key] = 0;
			relVidsHook[key] = $(val).children().filter('ul');
			relVidsTotal[key] = $(relVidsHook[key]).children().filter('li').length;
			$(val).prepend( $(document.createElement('a')).addClass('video-paginate-prev').attr('href', key).click(relVids_prev) );
			$(val).append(  $(document.createElement('a')).addClass('video-paginate-next').attr('href', key).click(relVids_next) );
		}
	});

	Rotator = {
		el: false,
		duration: false,
		loader: new Image(),
		id: 1,
		title: '',
		max: 16,
		delay: 350,
		length: 0,
		altMethod: false,

		prepare: function() {
			var el = $(this).children('img:first');
			if (el.data('timeout')) return false;
			el.data('oldsrc', el[0].src);
			Rotator.altMethod = false;
			Rotator.max = 16;
			el.data('newsrc', el[0].src.match(/^(.*\/[a-z]*)\d*(\.[a-z]{2,4})(\?.+)*$/));
			if( el.data('newsrc') == null ) {
				// Try another kind for SpankWire:
				el.data('newsrc', el[0].src.match(/^(.*\-)([0-9]{4})(\.[a-z]{2,4}.*)$/) );
				if( el.data('newsrc') != null ) {
					Rotator.altMethod = true;
					Rotator.max = 9;
				}
			}
			Rotator.title = el[0].parentNode.title;
			el[0].alt = el[0].parentNode.title = '';
			Rotator.id = Rotator.altMethod ? 0 : 1;
			Rotator.el = el;
			Rotator.duration = el.parent().find('.duration');
			Rotator.length = Rotator.duration.text().split(':');
			Rotator.length = parseInt(Rotator.length[0]) * 60 + parseInt(Rotator.length[1]);
			Rotator.swap();
		},

		stop: function() {
			try {
				window.clearTimeout(Rotator.el.data('timeout'));
				Rotator.el[0].alt = Rotator.el[0].parentNode.title = Rotator.title;
				Rotator.el[0].src = Rotator.el.data('oldsrc');
				Rotator.duration.text(Math.floor(Rotator.length/60) + ":" + (Rotator.length % 60 > 9 ? '' : '0') + (Rotator.length % 60));
				Rotator.el.removeData('oldsrc');
				Rotator.el.removeData('newsrc');
				Rotator.el.removeData('timeout');
				Rotator.el = false;
				Rotator.duration = false;
			} catch(e) {}
		},

		swap: function() {
			try {
				if (Rotator.id > Rotator.max) Rotator.id = (Rotator.altMethod ? 0 : 1);
				var newsrc = Rotator.el.data('newsrc');
				Rotator.loader.onload = Rotator.change;
				if( Rotator.altMethod ) {
					Rotator.loader.src = newsrc[1] + "000" + Rotator.id + newsrc[3];
				}
				else
					Rotator.loader.src = newsrc[1] + Rotator.id + newsrc[2];
				if (Rotator.loader.loaded)
					Rotator.change();
			} catch(e) {}
		},

		change: function() {
			try {
				// Prevent simultaneous multi image loading
				//if ( !Rotator.altMethod && parseInt(Rotator.loader.src.match(/^(.*\/[a-z]*)(\d*)(\.[a-z]{2,4})(\?.+)*$/)[2]) != Rotator.id) {
				//	return false;
				//}
				Rotator.el[0].src = Rotator.loader.src;
				if( Rotator.altMethod )
					Rotator.duration.text(Math.floor(Rotator.length/(Rotator.max+1)*Rotator.id/60) + ":" + (Math.floor(Rotator.length/(Rotator.max+1)*Rotator.id%60) > 9 ? '' : '0') + Math.floor(Rotator.length/(Rotator.max+1)*Rotator.id%60) + " / " + Math.floor(Rotator.length/60) + ":" + (Rotator.length % 60 > 9 ? '' : '0') + (Rotator.length % 60));
				else
					Rotator.duration.text(Math.floor(Rotator.length/Rotator.max*(Rotator.id-1)/60) + ":" + (Math.floor(Rotator.length/Rotator.max*(Rotator.id-1) % 60) > 9 ? '' : '0') + Math.floor(Rotator.length/Rotator.max*(Rotator.id-1) % 60) + " / " + Math.floor(Rotator.length/60) + ":" + (Rotator.length % 60 > 9 ? '' : '0') + (Rotator.length % 60));
				Rotator.id++;
				window.clearTimeout(Rotator.el.data('timeout'));
				Rotator.el.data('timeout', window.setTimeout(Rotator.swap, Rotator.delay));
			} catch(e) {}
		}
	};

	$('img.rotating-keez').each(function() {
		var el = $(this);
		el.parent().mouseenter(Rotator.prepare);
		el.parent().mouseleave(Rotator.stop);
	});

	function toggleAdvancedSearch() {
		var el = $('#search_pane');
		if (el.css('display') != 'block') {
			el.css('opacity', 0).css('display', 'block').fadeTo(500, 1);
		} else {
			el.css('opacity', 1).fadeTo(500, 0, function(){$(this).css('display', 'none');});
		}
		return false;
	}
	$('#adv-search-close').click(toggleAdvancedSearch);
	$('#header .search-advanced a').click(toggleAdvancedSearch);

	$('#search_form').submit(function() {
		var field = $(this).find('input[name=search]');
		if (field[0].value == '' || field[0].value == "Search...")
			return false;
	});

	$('.featuredclick').click(function() {
		var to = this.href.match(/#[a-zA-Z0-9\-\_]+/)[0];
		var el = $(this).parent();
		var next  = $(to);
		var first = el.parent().children(':first');
		var wrap  = el.parents('.featuredcats:first');
		var firstOff   = first.offset();
		var currentOff = el.offset();
		var nextOff    = next.offset();

		wrap.animate({
			scrollLeft: (nextOff.left-firstOff.left)
		}, 500);

		return false;
	});


	if ( $('div#rater_handle')[0] )
	{
		if( $.cookie('rated-'+$('input#rater_pornstar').val()) == null ) {
			$('div#rater_handle').draggable({
				axis: 'x',
				containment: 'parent',
				drag: function(event, ui) {
					$('div#rater_red').css('width', Math.round($(this).position().left / 159 * 100)+'%' );
					$('input#rater_rating').val( Math.round($(this).position().left / 159 * 100) );
					$('h3#rater_rate').html( $('input#rater_rating').val() );
				}
			});
			$('button#rater_submit').click( rater_click );
		}
	}

	if ($('#pornstar_dvds_btn')[0]) {
		$('#pornstar_dvds_btn').click(function(e) {
			if( $('#pornstar_dvds_iframe').attr('src') == "" )
				$('#pornstar_dvds_iframe').attr('src', $('#pornstar_dvds_iframe').attr('cachedsrc'));

			if( $('#pornstar_scenes').css('display') == 'block' )
			{
				$('#pornstar_scenes').hide();
				$('#pornstar_scenes_pagination').hide();
				$('#pornstar_dvds').show();
				$('#pornstar_dvds_btn').html('Watch Her: <strong>Scenes</strong>');
			}
			else if( $('#pornstar_dvds').css('display') == 'block' )
			{
				$('#pornstar_dvds').hide();
				$('#pornstar_scenes').show();
				$('#pornstar_scenes_pagination').show();
				$('#pornstar_dvds_btn').html('Purchase Her: <strong>DVDs</strong>');
			}
			return false;
		});
	}
}

function Rater(el, submit_url, video_id) {
	var dis = this, rating = 0;

	var first = el.children(':first');
	var next = el.next(':first');
	if (!next.hasClass('ratingtext'))
		next = false;

	this.rate = function(percent) {
		ratedText =  " <small>Saving...</small>";
		next.html(getRatingText(rating));
		rating = percent;
		$.post(submit_url, { id: video_id, value: (percent / 100 * 5) }, dis.rateCallback);
		$.cookie('rated-'+video_id,(percent / 100 * 5), {expires:1});
		$('button#rater_submit').unbind('click',rater_click);
	};
	this.rateCallback = function(data) {
		el.addClass('voted');
		$.data(el[0], 'ratingWidth', rating);
		first.css('width', rating + '%');

		if (next) {
			if (data == "success")
				ratedText =  " <small>(Rated: " + (''+(rating / 100 * 5)).replace(/\.5/, '&frac12;') + ")</small>";
			else
				ratedText =  " <small>Error registering your vote.</small>";
			next.html(getRatingText(rating));
		}
	};

	$.data(el[0], 'ratingWidth', parseFloat(first.css('width')));

	el.mousemove(function(e) {
		if( $.cookie('rated-'+video_id) == null )
		{
			window.clearTimeout(dis.ratingRemove);

			var percent = calculatePercent(el, e.pageX);
			first.css({ 'width': percent+'%',
						'background-position': '0px -44px',
						'cursor': 'pointer'
				});

			if (next) {
				next.html(getRatingText(percent));
			}
		}
	});
	el.mouseleave(function() {
		dis.ratingRemove = window.setTimeout(function() {
			var percent = $.data(el[0], 'ratingWidth');
			first.css({ 'width': percent+'%',
						'background-position': '0px -22px',
						'cursor': 'pointer'
				});

			if (next) {
				next.html(getRatingText(parseFloat(percent)));
			}
		}, 500);
	});
	el.click(function(e) {
		if( $.cookie('rated-'+video_id) == null )
		{
			window.clearTimeout(dis.ratingRemove);
			dis.rate(calculatePercent(el, e.pageX));
		}
		return false;
	});

	return this;
}

/* Pornstar Rater */
function rater_click(e) {
	if( $.cookie('rated-'+$('input#rater_pornstar').val()) == null )
		$.post($('input#rater_url').val(),{ name: $('input#rater_pornstar').val(), value: $('input#rater_rating').val() }, function(e) {
			$.cookie('rated-'+$('input#rater_pornstar').val(),$('input#rater_rating').val(), {expires:1});
			$('button#rater_submit').unbind('click',rater_click);
			$('button#rater_submit').attr('disabled','disabled');
			$('p#rater_instructions').html('Thank you for your rating!');
			$('button#rater_submit').html('SUBMITED');
			$('div#rater_handle').hide();
		});
}

/* magic function */
$(document).ready(KEEEEEEZ);


