var ls_chat;
var ls_pinned;
var last_downloads_list;
var last_releases_list;
var last_tutos_list;
var toggles;

window.addEvent('domready', function()
{
	try {
		var pageTracker = _gat._getTracker("UA-8259226-1");
		pageTracker._trackPageview();
	} catch(err) {}

	var menu = new Sortables($('left-content'),
	{
		handle: 'div.handle',
		constrain: false,
		opacity: 0.7,
		onComplete: function()
		{
			var position = new String(menu.serialize());
			position = position.replace(/,/g, ":");
			Cookie.write('logic-sunrise[positions]', position, {duration: 360});
		}
	});
	last_downloads_list = new Fx.Reveal('last_downloads_list');
	last_releases_list = new Fx.Reveal('last_releases_list');
	last_tutos_list = new Fx.Reveal('last_tutos_list');
	ls_chat = new Fx.Reveal('ls_chat');
	ls_pinned = new Fx.Reveal('ls_pinned');
	toggles = Cookie.read('logic-sunrise[togglesHome]').split(':');
	if (toggles[0] == 0)
		toggle_left_content('last_downloads_list', 'hide');
	if (toggles[1] == 0)
		toggle_left_content('last_tutos_list', 'hide');
	if (toggles[2] == 0)
		toggle_left_content('last_releases_list', 'hide');
	if (toggles[3] == 0)
		toggle_left_content('ls_chat', 'hide');
	if (toggles[4] == 0)
		toggle_left_content('ls_pinned', 'hide');

	/* settings */
	var showDuration = 3000;
	var container = $('store-slideshow');
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	/* opacity and fade */
	images.each(function(img,i){ 
		if(i > 0) {
			img.set('opacity',0);
		}
	});
	/* worker */
	var show = function() {
		images[currentIndex].fade('out');
		images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
	};
	/* start once the page is finished loading */
	window.addEvent('load',function(){
		interval = show.periodical(showDuration);
	});
});

function toggle_left_content(type, action)
{
	flag = 1;
	var reg1=new RegExp("icone-maximise", "g");
	if (type == 'ls_chat' || type == 'ls_pinned')
	{
		var reg2=new RegExp("close-hover", "g");
		if (document.getElementById('arrow_' + type).src.match(reg1) || document.getElementById('arrow_' + type).src.match(reg2))
		{
			document.getElementById('arrow_' + type).src = 'http://www.logic-sunrise.com/templates/images/onglet-icone-minimise.gif';
			$('onglet-close-' + type).addClass('onglet-close-a');
			$('onglet-close-' + type).removeClass('onglet-close-a-bis');
		}
		else
		{
			document.getElementById('arrow_' + type).src = 'http://www.logic-sunrise.com/templates/images/onglet-close-hover.gif';
			$('onglet-close-' + type).addClass('onglet-close-a-bis');
			$('onglet-close-' + type).removeClass('onglet-close-a');
			flag = 0;
		}
	}
	else
	{
		if (document.getElementById('arrow_' + type).src.match(reg1))
			document.getElementById('arrow_' + type).src = 'http://www.logic-sunrise.com/templates/images/icone-minimise.gif';
		else
		{
			document.getElementById('arrow_' + type).src = 'http://www.logic-sunrise.com/templates/images/icone-maximise.gif';
			flag = 0;
		}
	}

	if (action == 'toggle')
		eval(type).toggle();
	else
		eval(type).dissolve();
	if (type == 'last_downloads_list')
		toggles[0] = flag;
	if (type == 'last_tutos_list')
		toggles[1] = flag;
	if (type == 'last_releases_list')
		toggles[2] = flag;
	if (type == 'ls_chat')
		toggles[3] = flag;
	if (type == 'ls_pinned')
		toggles[4] = flag;
	tmp = toggles.join(':');
	Cookie.write('logic-sunrise[togglesHome]', tmp, {duration: 360});
}

function check_add_comments(plz)
{
	var errors = 0;
	var radio = document.add_comments.note;
	if (radio[0].checked == false && radio[1].checked == false && radio[2].checked == false && radio[3].checked == false && radio[4].checked == false)
	{
		document.getElementById('comments_note').style.border = '1px solid #FF0000';
		errors = 1;
	}
	else
		document.getElementById('comments_note').style.border = '1px solid #000000';

	if (FCKeditorAPI.GetInstance('post').GetData() == "" || FCKeditorAPI.GetInstance('post').GetData() == null)
	{
		document.getElementById('comments_body').style.border = "1px solid red";
		errors = 1;
	}
	else
		document.getElementById('comments_body').style.border = "";

	if (errors == 1)
	{
		scroll(0,0);
		return false;
	}
	document.add_comments.Submit1.disabled = true;
	document.add_comments.Submit1.value = plz + '...';
	return true;
}

function comment_quote(id)
{
	var reg = new RegExp("<span id=\"quote_img-(.*)<\/span>", "gi");
	var reg2 = new RegExp("<br>", "gi");

	var quote = document.getElementById('comment_id-' + id).innerHTML;
	quote = quote.replace(reg, '');
	quote = quote.replace(reg2, '');

	var comment_area = document.getElementById('comments-textarea');
	if (comment_area.value == '')
		comment_area.value = '[quote]' + quote + '[/quote]\n';
	else
		comment_area.value = comment_area.value + '\n[quote]' + quote + '[/quote]\n';
	comment_area.focus();
}

function comment_reputation(id, action)
{
	if (action == 'bad')
	{
		var question = confirm("Attention, l'utilisation de ce lien signal un message hors sujet ou inutile, et non un message avec lequel vous n'\352tes pas d'accord. Toute utilisation de cette fonction de mani\350re abusive est passible de suspension temporaire de votre compte. Etes-vous sur de d\351clarer ce message aux mod\351rateurs ?");
		if (question == false)
			return false;
	}

	var req = new Request.HTML({
			method: 'post',
			url: 'lib/comments_reputation.php',
			data: { 'id' : id, 'action' : action},
			update: $('comments-info-rating-' + id)
			}).send();
}

function quote_img(id, action, return_url)
{
	var quote_div = document.getElementById('quote_img-' + id);

	if (action == 1)
	{
		var html = '<img src="templates/images/quote.gif" style="cursor: pointer;" onclick="comment_quote(' + id + ');" alt="R&eacute;pondre &agrave; ce commentaire" title="R&eacute;pondre &agrave; ce commentaire" />';
		quote_div.innerHTML = html;
	}
	else
		quote_div.innerHTML = '';
}

function delete_post(type, id)
{
	var question = confirm("Voulez vous vraiment supprimer ce post ?");
	if (question == true)
		window.location="lib/post_delete.php?type=" + type + "&pid=" + id;
}


function xiti(site, code)
{
	Xt_param = 's=' + site + '&p=';
	try {Xt_r = top.document.referrer;}
	catch(e) {Xt_r = document.referrer; }
	Xt_h = new Date();
	Xt_i = '<img width="39" height="25" border="0" alt="" ';
	Xt_i += 'src="http://logv'+code+'.xiti.com/hit.xiti?'+Xt_param;
	Xt_i += '&hl='+Xt_h.getHours()+'x'+Xt_h.getMinutes()+'x'+Xt_h.getSeconds();
	if(parseFloat(navigator.appVersion)>=4)
		{Xt_s=screen;Xt_i+='&r='+Xt_s.width+'x'+Xt_s.height+'x'+Xt_s.pixelDepth+'x'+Xt_s.colorDepth;}
	document.write(Xt_i+'&ref='+Xt_r.replace(/[<>"]/g, '').replace(/&/g, '$')+'" title="Internet Audience">');
}

function redimImageNelsou(path, maxWidth, maxHeight, id)
{
  var photos = new Image();
  photos.src = path + "?" + new Date().getTime();;
  photos.onload = function()
  {
    var coefX = photos.width / maxWidth;
    var coefY = photos.height / maxHeight;
    var coef = ((coefX > coefY) ? coefX : coefY);
	var width = photos.width / ((coef < 1) ? 1 : coef);
    var height = photos.height / ((coef < 1) ? 1 : coef);
    document.getElementById(id).innerHTML = "<img src='"+path+"' height="+parseInt(height)+" width="+parseInt(width)+">";
  }
}

function lastnews_ajax_scroll(total_news, offset, nb_news, action)
{
	if (offset - nb_news == 0)
		return false;
	var req = new Request.HTML({
			method: 'post',
			url: 'lib/lastnews_scroll_ajax.php',
			data: { 'total_news' : total_news, 'offset' : offset, 'nb_news' : nb_news, 'action' : action },
			update: $('header-lastnews-content-' + action),
			onComplete: function() {
				var scroll = new Fx.Tween('header-lastnews-box', {
							duration: 700,
							transition: Fx.Transitions.Cubic.easeOut,
							onComplete: function() {
								document.getElementById('header-lastnews-content').innerHTML = document.getElementById('header-lastnews-content-' + action).innerHTML;
								document.getElementById('header-lastnews-box').style.marginTop = '-110px';
							}
							});
							if (action == 'down')
								scroll.start('margin-top', '-220');
							else
								scroll.start('margin-top', '0');
				}
			}).send();
}

function members_payments(account)
{
	if (account < 30)
	{
		alert('Vous devez avoir un solde minimum de 30 euros pour pouvoir faire une demande de paiement');
		return false;
	}
	var email = prompt("Merci de saisir l'adresse mail de votre compte Paypal ?", "Email");
	if (email)
		window.location="lib/ask_payments.php?email=" + email;
}

