$(document).ready(function(){
	initAutocomplete();
	initTableDescr();
	
	$('#add').click(function(){
		if($.trim($('#showname').val()).length == 0) return;
		
		$('#showname').autocomplete('close');
		
		$.post('engine.php', {check: $('#showname').val()}, function(data){
			if(data != false){
				$('#currentlist').append('<li><span></span></li>')
								 .find('li:last-child span')
								 .text(data)
								 .parent()
								 .append(' &nbsp; <img src="cross.png" class="cross pointer" onclick="$(this).parent().remove()" />');
			}
		}, 'json');
		
		$('#showname').val('');
	});
	
	$('#emailme').change(function(){
		$('#emailwrapper').css('display', $(this).is(':checked') ? 'inline' : 'none');
	});
	
	$('#send').click(function(){
		var shows = [];
		
		$('#currentlist li span').each(function(i, li){
			shows[i] = $(li).text();
		});
		
		if(shows.length == 0) return;
		if(shows.length == 1){
			alert(onlyOneStr);
			return;
		}
		
		$('input, button').attr('disabled', true);
		
		$('#step1').css({
			fontWeight: 'normal',
			color: 'grey'
		});
		$('#step2').css({
			fontWeight: 'bold',
			color: 'black'
		});
		$('#step2 img').attr('src', 'loader.gif');
		$('#content').animate({ opacity: 0.3 }, 500);
		
		var post = {shows: shows};
		
		$.post('engine.php?lang=' + lang, post, function(data){
			document.location = '?id=' + data + '&lang=' + lang;
		});
	});
});

function initAutocomplete(){
	$('#showname').autocomplete({
		source: 'engine.php',
		delay: 0,
		minLength: 0
	});
	
	$('#showname').keypress(function(event){
		if(event.keyCode == 13) $('#add').click();
	});
}

function initTableDescr(){
	$('td:first-child').click(function(){
		var x = $(this).position().top;
		var t = $('#tblrec').width() / 2;
		if($('#tooltip').length != 1) $('body').append('<div id="tooltip" style="width: ' + (t - 2) + 'px;"></div>');
		
		$('tr').removeClass('lgbg');
		$(this).parent().addClass('lgbg');
		
		$('#tooltip').css({
			top: x + 'px',
			left: ($(this).position().left + t) + 'px'
		});
		
		$('#tooltip').html('<img src="loader2.gif" class="icon" style="padding: 5px;" />').show();
		
		$.get('engine.php?info=' + escape($(this).find('span').text()) + '&lang=' + lang, function(data){
			$('#tooltip').html(data);
		});
	});
}

function closeTooltip(){
	$('tr').removeClass('lgbg');
	$('#tooltip').remove();
}

var ad = '';

function reloadRecommendations(cache){
	if(cache == 'false' && $('#step2 img').attr('src') != 'loader.gif'){
		$('#step3').css({
			fontWeight: 'normal',
			color: 'grey'
		});
		$('#step2').css({
			fontWeight: 'bold',
			color: 'black'
		});
		$('#step2 img').attr('src', 'loader.gif');
		$('#content').animate({ opacity: 0.3 }, 500);
	}
	
	ad = $('#reklm').html().replace(/\r?\n/g, '').replace(/<script.*?>.*?<\/script>/gi, '');
	
	$.get('index.php?id=' + id + '&lang=' + lang + '&tpl=false&cache=' + cache, function(data){
		$('#content').stop().slideUp(500, function(){
			$('#step2').css({
				fontWeight: 'normal',
				color: 'grey'
			});
			$('#step3').css({
				fontWeight: 'bold',
				color: 'black'
			});
			$('#step2 img').attr('src', 'databases-relation.png');
			
			$(this).css('opacity', 1).html(data.replace('<!-- AD PLACEHOLDER -->', ad)).slideDown(500);
			initTableDescr();
		});
	});
}

function modifyList(action, id, show){
	closeTooltip();
	
	$('#content img').attr('onclick', 'return false');
	$('#content button, #content input').attr('disabled', 'disabled');
	
	$('#step3').css({
		fontWeight: 'normal',
		color: 'grey'
	});
	$('#step2').css({
		fontWeight: 'bold',
		color: 'black'
	});
	$('#step2 img').attr('src', 'loader.gif');
	$('#content').animate({ opacity: 0.3 }, 500);
	
	$.post('engine.php?' + (action == 0 ? 'remove' : action == 1 ? 'add' : action == -1 ? 'exclude' : 'include'), {id: id, show: show}, function(data){
		reloadRecommendations('false');
	});
}

function addNewShow(){
	closeTooltip();
	
	$('#content').slideUp(500, function(){
		$(this).html('<h3>' + addNewTvShowStr + '</h3><p><input type="text" id="showname" /> <button id="add" onclick="addShow()">' + addStr + '</button> <button onclick="reloadRecommendations(\'true\')">' + cancelStr +'</button></p>').slideDown(500);
		initAutocomplete();
	});
}

function addShow(){
	if($.trim($('#showname').val()).length == 0) return;
	
	$('#showname').autocomplete('close');
	
	$.post('engine.php', {check: $('#showname').val()}, function(data){
		if(data != false){
			modifyList(1, id, escape(data));
		}
	}, 'json');
}
