tTimer = false;
gID = false; tImage = false; tSrc = false;
tStep = false; bAdded = false;

$(document).ready(function() {
	if (document.cookie && document.cookie != '') {
		var bookmarks = readCookie('bookmark');
		if (bookmarks) {
			bookmarks = bookmarks.replace(/%2C/g, ',').replace(/,$/, '').split(',');
			bAdded = bookmarks.length;
			for (var i = 0; i < bAdded; i++) {
				//$('p.debug').text($('p.debug').text() + " " + i + " = " + bookmarks[i]);
				obj = $("#"+bookmarks[i]).parent().prev().removeClass('ba').addClass('br');
			}
		}
	}
});

jQuery(function($) {
	$('li img.t, li img.ts').hover(function() {
		gID = this.id; tImage = this; tSrc = tImage.src;

		if ($(this).hasClass('t')) {
			tStep = 0;
			tImage.style.background = 'url('+tImage.src.replace('.jpg', '_.jpg')+')';
			tTimer = window.setTimeout('slide()',500);
		}
		btn = $(this).parent().prev();
		btn.addClass('stable').attr('title', btn.hasClass('ba') ? 'Add to Favorites' : 'Remove from Favorites');
	},function() {
		if ($(this).hasClass('t')) {
			tStep = false;
			window.clearTimeout(tTimer);
			tImage.src = tSrc;
		}
		$(this).parent().prev().removeClass('stable');
	});

	$('span.ba, span.br').hover(function() {
		$(this).addClass('stable');
		$(this).css('background-position',$(this).hasClass('ba') ? '-15px 0px' : '-45px 0px');
	},function() {
		$(this).removeClass('stable');
		$(this).css('background-position',$(this).hasClass('ba') ? '0px 0px' : '-30px 0px');
	});

	$('span.ba, span.br').click(function() {
		if ($(this).hasClass('ba')) {
			if (bAdded >= 100) {
				alert('The maximum of 100 favorites is exceeded');
				return;
			}
			bAdded++;
			$.ajax({type: 'POST',	url: 'http://www.porn-spot.net/tr/bookmarks.php',data: {add: gID}});
			$(this).css('background-position','-45px 0px').removeClass('ba').addClass('br');
			$(this).parent().css('opacity','1');

			var offset = $(this).offset();
			var fclone = $(this).next().children();

			fclone.clone(true).attr('name',fclone.attr('id')).attr('id','fly').css('border','1px solid #63A9FF').css('position','absolute').insertAfter(this).animate({marginTop: -offset.top+360,	marginLeft: $(window).width()-offset.left-120, width: '50%', height: '50%'}, 600 ,function(){$(this).remove();});
		} else {
			bAdded--;
			$.ajax({type: 'POST',	url: 'http://www.porn-spot.net/tr/bookmarks.php',data: {del: gID}});
			$(this).css('background-position','-15px 0px').attr('title','Add to Favorites').removeClass('br').addClass('ba');
	    if (window.top.location.href.indexOf('http://www.porn-spot.net/Favorites/')!=-1) {
				$(this).parent().css('opacity','0.2');
			}
		}
	});
});

function slide() {
	if (tStep > 2400 || tStep == 0) {
		tImage.src = 'http://www.porn-spot.net/i/thumb.gif';
		tStep=0;
	}
	tImage.style.backgroundPosition = '-'+tStep+'px';
	tStep = tStep + 240;
	tTimer = window.setTimeout('slide()', 500);
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
