var searchTimer = 0;
var keyPoint = 0;

(function($) {
	$.doSearch = function() {
/*
		$.ajax({
			'data' : {'_search' : $('#search_field').val(), '_lang': $('#lang_field').val() },
			'dataType': 'html',
			'cache': false,
			'success': function(htmlBuffer,status,obj) {
				if(htmlBuffer != '') {
					$('div.search_result div.result.').html(htmlBuffer);
					$('div.search_result').show();
					} else {
						$('div.search_result').hide();
					}
			},
			'error': function(obj,status,error) {
				alert('Search could not be performed.' + error);
			},
			'type': 'post',
			'url' : 'http://www.24.tv/search.php',
			'error' : function() {}
		});
*/
	};
})(jQuery);

function waitHere() {
	$('div.search_result div.result').load('http://www.24.tv/search.php', {'_search' : $('#search_field').val(), '_lang': $('#lang_field').val() }, function() {
		if($('div.search_result div.result').html().length > 0) {
			$('div.search_result').show();
		} else {
			$('div.search_result').hide();
		}
	});
}

$(function() {
	$('div.userlogin button.login').click(function() {
		$(document.createElement('div'))
			.attr({'id':'loginBox', 'title': 'Login'})
			.html('')
			.dialog({
				modal: true,
				buttons: {
					'Cancel': function() {
						$(this).dialog('close');
					},
					'OK': function() {
						$('div.userlogin').html('<button class="reorg">Sort channels</button>');
						$(this).dialog('close');
					}
				}
			});
	});
	$('div.userlogin button.reorg').live('click', function() {
		$('div#tables div.channel ul li').hide().parent().append('<li class="handle">DRAG ME.</li>');
		$('div#tables div.clear').remove();
		$('div#tables div.channel').addClass('channel_clear');
		$('div#tables').sortable({
			handle: '.handle'
		});
		$(this).text('Save');
	});
	$('#search_field').focus();
	$('#channelDropList').change(function() {
		var url = $('option:selected', this).val().replace('./','');
		location.href = url;
			
	});
	$('div#tables div.channel li,div#tables div.channel2 li').mouseover(function() {
		$('a:eq(1)', this).addClass('stroked');
	}).mouseout(function() {
		$('a:eq(1)', this).removeClass('stroked');
	});

	$('div.channel ul li a,div.channel2 ul li a').click(function() {
		var win = window.open($(this).attr('href'), 'popupWindow', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=885,height=575,left = 100,top = 60');
		// win.document.write('Hello');
		// win.document.close();
		win.focus();
		return false;
	});
	$('div#search div.search_result div.result ul li a.popup').live('click',function() {
		var win = window.open($(this).attr('href'), 'popupWindow', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=885,height=575,left = 100,top = 60');
		// win.document.write('Hello');
		// win.document.close();
		win.focus();
		return false;
	});
	$('#search_field').keyup(function() {
		clearTimeout(searchTimer);
		searchTimer = setTimeout("waitHere()", 800);
	});
	$('div#menu ul li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

	$('.hidden-form').dialog({
			autoOpen: false,
			bgiframe: true,
			height: 140,
			width: 300,
			modal: true,
			resizable: false,
			title: 'Tip a friend'
		});

	$('a.tipafriend').click(function() {
		$('.hidden-form').dialog('open');
	});
	$('div.commentHead input:not(.submit)').focus(function() {
		$(this).parent().parent().animate({'height': '148px'}, 1000);
		$(this).val('');
	});
	$('div.commentHead input#checkThis').keyup(function() {
		if($(this).val().match(/[^ ]+@[^ ]+\.[^ ]+/)) {
			$('div.commentHead input#submitButton').removeAttr('disabled');
		} else {
			$('div.commentHead input#submitButton').attr('disabled','disabled');
		}
	});
	$('div.commentHead p input#submitButton.submit').click(function() {
		if($(this).attr('disabled') != 'disabled' && keyPoint == 0) {
			var whatToPost = {};
			$('div.commentHead input:not(.submit)').each(function() {
				var temp = "({'" + $(this).attr('name') + "':'" + escape($(this).val()) + "'})";
				temp = eval(temp);
				whatToPost = $.extend(temp, whatToPost);
			});
			$.ajax({
				'url':'json.php',
				'data': whatToPost,
				'type': 'POST',
				'dataType': 'json',
				'beforeSend': function() {
					keyPoint = 1;
				},
				'success': function(json) {
					if(json.message == 'success') {
						location.reload();
					} else {
						alert('Could not post comment');
					}
					keyPoint = 0;
				},
				'error': function() {
					if($.browser.msie) { location.reload(); } else {
						alert('Could not post comment');
					}
					keyPoint = 0;
				}
			});
		} else { document.title = 'Ajax call in progress....'; }
	});
	
});

