$(document).ready(function() {
	$('#grid').sortable({ placeholder: 'ui-state-highlight' });
	$('#grid').bind('sortupdate', function(event, ui) {
		setCookieChannels();
	});
	$('#grid').disableSelection();
	$(this).find('#dropdown').bind("click", function(){$(document).find('#channels').toggle();});
	$(this).find('#channels').bind("mouseleave", function(){$(this).hide("slow");});
	$(this).find('#channels li').bind("click", function(){
		var c = $(this).find('img').attr('id').split('_')[1];
		var d = $(document).find('#listitem_' + c);
		if (d.attr('id') != undefined){
			d.toggle();
			setCookieChannels();
		} else {
			$.get("todaychannel.php", {channel:c}, function(data){
				$(document).find('#grid').append(data);
				setCookieChannels();
			}, "html");
		}
		$(this).find('img').toggle();
	});
	$(this).find('.closechannel').bind("click", function(){
		var p = $(this).parent().parent();
		p.toggle();
		var c = p.attr('id').split('_')[1];
		$(document).find('#choose_' + c).hide();
		setCookieChannels();
	});
	$(this).find('.ititle a').bind("mouseenter", function(e){
		$(document).find('#desc').html($(this).parent().next().html());
		$(document).find('#desc').css('top', e.pageY + 10);
		$(document).find('#desc').css('left', e.pageX - 240);
		$(document).find('#desc').show();
	});
	$(this).find('.ititle a').bind("mouseleave", function(e){
		$(document).find('#desc').hide();
		$(document).find('#desc').html('');
	});
	if (document.all){$(this).find('#home').show();}
	
	var c = $.cookie('MPTV_Channels');
	if (c != null && c != ''){
		c = c.split('[channel]');
		$(this).find('#channels li img').each(function(i){
			for (var j=0; j<c.length; j++){
				if($(this).attr('id') == "choose_" + c[j]){
					$(this).show();
				}
			}
		});
	} else {
		$(this).find('#channels li img').each(function(i){ $(this).show(); });
	}
});

var setCookieChannels = function()
{
	$.cookie('MPTV_Channels', '');
	var cookieStr = '';
	$(document).find('#grid .listitem:visible').each(function(){
		var b = $(this).attr('id').split('_')[1];
		if (cookieStr == ''){
			cookieStr = b;
		} else {
			cookieStr = cookieStr + "[channel]" + b; 
		}
	});
	$.cookie('MPTV_Channels', cookieStr, { expires: 1825 });
}

var addFav = function(title,url){
	function trace(key){
		alert("Utiliser '" + key + "'\npour ajouter votre Programme TV dans vos favoris");
	}
	if(navigator.userAgent.indexOf('Konqueror')>=0){trace("CTRL + B");}
	else if(window.external&&typeof(window.externalAddFavorite)=='function'){window.external.AddFavorite(url,title);}
	else if(document.all&&(navigator.userAgent.indexOf('Win')<0)){trace("POMME + D");}
	else if(navigator.userAgent.indexOf('Chrome')){trace("CTRL + D");}
	else if(window.chrome&&window.print){trace("CTRL + T");}
	else if(window.sidebar){window.sidebar.addPanel(title,url,"");}
	else if(document.layers){trace("CTRL + D");}
	else alert("Cette fonction n'est pas disponible pour votre navigateur.");
}
