var encours = true;

var initUrl = function() {
	encours = false;
	$("a.link1").bind("click", function(event) {
		var url = $(this).attr("href");
		if(url.substr(0,7) != "http://") {
			if(! encours) {
				encours = true;
				$("#center").html("Chargement en cours...");
				$("#center").load($(this).attr("href")+"?cas=ajax", initUrl);
				event.preventDefault();
				}
			}
		});
	$("a.link2").bind("click", function(event) {
		var url = $(this).attr("href");
		if(url.substr(0,7) != "http://") {
			if(! encours) {
				encours = true;
				$("#center").html("Chargement en cours...");
				$("#center").load($(this).attr("href")+"?cas=ajax", initUrl);
				event.preventDefault();
				}
			}
		});
	};

 function saveplaylist(value) {
   	var date = new Date();
	date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
	$.cookie('playlist', value, { expires: date });
 }

$(".imgnews").css({opacity:"0.6"});
$(".imgnews").hover(
  function () {
    $(this).css({opacity:"1"});
  },
  function () {
    $(this).css({opacity:"0.6"});
  }
);


function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }

function sendToActionScript(type1, cat, id, title) {
     retour = thisMovie("audioplayer").addplaylist(type1, cat, id, title);
     if(retour == "full")
     	alert("liste complete");
 }


$(".itemHeader").mousedown(function(){
		$(this).parent().parent().parent().parent().parent().map(function () { $(this).css({opacity:"0.4"}); });
});
$(".itemHeader").mouseup(function(){
		$(this).parent().parent().parent().parent().parent().map(function () { $(this).css({opacity:"1"}); });
});

var toggleContent = function(e)
{
	var targetContent = $('div.itemContent', this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300, function(){serializer()});
		$(this).html('[-]');
	} else {
		targetContent.slideUp(300, function(){serializer()});
		$(this).html('[+]');
	}
	return false;
};

$('a.tip[@title]').leviTip();

function serializer(s)
{
	var j = "";
	$('td.itemHeader').each(function(){
		j = j.concat($(this).text()+";");
	});

	var i = "";
	$('div.itemContent').each(function(){
		if ($(this).css('display') == 'none') {  i = i.concat('0'); }
		else { 	i = i.concat('1'); }
	});

	var date = new Date();
	date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
	var cookie = j.concat(i);
	$.cookie('menus', cookie, { expires: date });
	return false;
};

$(document).ready(
	function () {
		initUrl();
		$('a.closeEl').bind('click', toggleContent);
		$('div.groupWrapper').Sortable(
			{
				accept: 'groupItem',
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: '.itemHeader',
				tolerance: 'pointer',
				onChange : function(ser)
				{
				},
				onStart : function()
				{
					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
				},
				onStop : function()
				{
					$(this).css({ opacity:"1" });
					$.iAutoscroller.stop();
					serializer();
					//$("#center").text($(this).html());
				}
			}
		);
	}
);
