//reload de la page

function pageReload() {
	window.location.reload();
}


function launchPlayer(link , quality) {
	if(quality)
		link += "?q=2";
	else
		link += "?q=1";

	var player	=	window.open(link , 'player' , 'width=920,height=571,resizable=false,scrollbars=no,status=no' , false);
}


function initVideoBarEffect() {

	var bars = $(".video_launchbar");
		var imgs;
		var i;

		for(i=0;i<bars.length;i++) {
			imgs = bars.eq(i).find("img:lt(2)");

			imgs.mouseover(
				function() {
					$(this).attr("src" , $(this).attr("src").replace(".jpg","_over.jpg"));
				}
			);

			imgs.mouseout(
				function() {
					$(this).attr("src" , $(this).attr("src").replace("_over.jpg",".jpg"));
				}
			);
		}

}


//Livegen TV video thumb effect

function initThumbEffect(){
	var imgs = $('.thumb_effect');
	imgs.removeClass('thumb_effect');
	imgs.addClass('thumb_effect_done');
	$.each( imgs , function(i, n){
		var img = $(n);
		img.css('background','url('+img.attr('src')+') no-repeat center center');
	});

	if (($.browser.msie && $.browser.version == '7.0') || $.browser.opera || $.browser.mozilla || $.browser.safari) {
		imgs.attr('src','http://www.livegen.fr/ressources/commons/images/thumb/thumb_bg.png');
	} else {
		imgs.attr('src','http://www.livegen.fr/ressources/commons/images/thumb/thumb_bg.gif');
	}
}

//Livegame thumb effect

function initLivegameThumbEffect(){
	var imgs = $('.livegame_thumb_effect');
	imgs.removeClass('livegame_thumb_effect');
	imgs.addClass('livegame_thumb_effect_done');
	$.each( imgs , function(i, n){
		var img = $(n);
		img.css('background','url('+img.attr('src')+') no-repeat center center');

	});

	imgs.attr('src','http://www.livegen.fr/ressources/commons/images/thumb/thumb120_bg.gif');
}

function initPlayThumbEffect(){
	var imgs = $('.playthumb_effect');
	imgs.removeClass('playthumb_effect');
	imgs.addClass('playthumb_effect_done');
	$.each( imgs , function(i, n){
		var img = $(n);
		img.css('background','url('+img.attr('src')+') no-repeat center center');
	});

	if (($.browser.msie && $.browser.version == '7.0') || $.browser.opera || $.browser.mozilla || $.browser.safari) {
		imgs.attr('src','http://www.livegen.fr/ressources/commons/images/thumb/thumb_play.png');
	} else {
		imgs.attr('src','http://www.livegen.fr/ressources/commons/images/thumb/thumb_play.gif');
	}
}


//Livegen TV switch thumb , simulate playout

function initSwitchThumb() {
	var timeInterval = 700;
	var imgCollection;
	var imgCollectionIndex;
	var currentImg;
	var defaultStyle;
	var switchTimer;

	var imgs = $('.switch_thumb');

	function changeThumb() {
		imgCollectionIndex++;
		imgCollectionIndex %= imgCollection.length;
		currentImg.css('background' , 'url('+ imgCollection[imgCollectionIndex] +')');
	}

	imgs.mouseover(function(event) {
		var id = $(this).attr('id');
		imgCollection = jswitcher[id];

		if(typeof imgCollection != "undefined") {
			currentImg = $(this);
     		defaultStyle = currentImg.css('background');
     		imgCollectionIndex = 0;
     		changeThumb();
			switchTimer = setInterval(changeThumb,timeInterval);
		}
	});

	imgs.mouseout(function(event) {
		clearInterval(switchTimer);
		$(this).css('background' , defaultStyle);
	});
};


//	HalfList

function displayHalflistHeading(caller,id) {
	var displayObject = $("#"+id);
	displayObject.siblings(".heading").slideUp(300);
	displayObject.slideDown(300);

	var link = $(caller);
	link.siblings("a").addClass("disabled");
	link.removeClass("disabled");
}


//	Smiley

function addSmiley(target , code)
{
	code	=	" " + code + " ";

	var el	=	document.getElementById(target);

	//IE support
	if (document.selection)
	{
		el.focus();
		var sel = document.selection.createRange();
		sel.text = code;
		el.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (el.selectionStart || el.selectionStart == "0")
	{
		var startPos = el.selectionStart;
		var endPos = el.selectionEnd;
		var chaine = el.value;

		el.value = chaine.substring(0, startPos) + code + chaine.substring(endPos, chaine.length);

		el.selectionStart = startPos + code.length;
		el.selectionEnd = el.selectionStart;
		el.focus();
	}
	else
	{
		el.value += code;
		el.focus();
	}
}



//	Hide Comment

function swapComment(button){

	var but	=	$(button);
	var el	=	but.next().children(":first-child");

	if(el.css("display") == "block") {
		el.slideUp(300);
		but.val("Montrer");
	}
	else {
		el.slideDown(300);
		but.val("Cacher");
	}
}

//	Abuse

var abuse_area = null;
function openAbuse(caller){

	if(abuse_area == null) {
		var uri = window.location.href.substr(7);
		abuse_area	=	$('<div class="abuse_report"><form method="post">'+
						'<textarea name="reason"></textarea>'+
						'<input type="hidden" name="uri" value="'+uri+'"/>'+
						'</form>'+
						'<a class="generic_button validation"><span>Valider</span></a>'+
						'<a class="generic_button close"><span>Fermer</span></a>'+

						'</div>');

		$("body").append(abuse_area);

		abuse_area.children(".validation").click(function(){abuse_area.children("form").submit()});
		abuse_area.children(".close").click(function(){abuse_area.fadeOut(500)});
	}

	abuse_area.hide();
	abuse_area.children("form").attr("action" , $(caller).attr("href"));


	var page = $.MyTools.page;
	abuse_area.css({
		left:page.x() + page.width()/2,
		top:page.y() + page.height()/2,
		position:"absolute"
	});
	abuse_area.fadeIn(1000);

	return false;
}


//Entry Point

$(document).ready(
	function() {

		initVideoBarEffect();
		initLivegameThumbEffect();

	}
);