var djpod_msg = new Array();
var check = false;

$(function()
{ 
	$('form').each(function() 
	{
		$(this).submit(function()
		{
			if(this.id != 'auth_form') 
			{
				var submitButton = $(this).find("input[type='submit']");
				
				if(this.id != 'create-form') {
					$(submitButton).attr('value',djpod_msg['loading']);
				}
				
				$(submitButton).attr('disabled','true');
				$(submitButton).attr('style','color:#000;background:#eee;cursor:wait;border-color:#ccc');
			}
		});
	});
	
	$('#message_hide').click(function(){
		$('#message_box').slideUp();
	});
	
	$('.error div').effect('pulsate', { times: 3, duration: 50 });
	$('.confirm div').effect('pulsate', { times: 3, duration: 50 });
	$('.warning div').effect('pulsate', { times: 3, duration: 50 });
	
	/*
	if($.cookie('djpod_locale') != null) {		
		toggleLang($.cookie('djpod_locale'),'cookie');
	}
	else {
		$.post('/locale.php', { action:'locale' }, function(data) {
			if(data.indexOf('<!-- LOCALE //-->') != -1) {
				toggleLang(data.substr(18,data.length),'new');
			}
		});
	}
	*/
});

// Insertion du bandeau langue
function toggleLang(data,type)
{
	if(data == '' || data == 'null') {
		return;
	}
	
	var cook = data.split('|');
	
	var _lang = cook[0];
	var _country = cook[1];
	var _ext = cook[2];
	
	if(_lang == 'fr' || _lang == 'be' || _lang == 'ch') {
		var lang_sentence_1 = 'Djpod est également disponible pour la '+_country+' !';
		var lang_sentence_2 = 'Cliquez ici pour aller sur Djpod '+_country;
	}
	if(_lang == 'uk' || _lang == 'us') {
		var lang_sentence_1 = 'Djpod is also available in your language!';
		var lang_sentence_2 = 'Click here to go to Djpod '+_country;
	}
	if(_lang == 'de') {
		var lang_sentence_1 = 'Djpod gibt es auch in Ihrer Sprache!';
		var lang_sentence_2 = 'Klicken Sie hier, um Djpod '+_country;
	}
	if(_lang == 'es') {
		var lang_sentence_1 = 'Djpod también está disponible en su idioma!';
		var lang_sentence_2 = 'Haga clic aquí para ir a Djpod '+_country;
	}	
	
	var lang_html = '<div class="flag_'+_lang+'">'+
		lang_sentence_1+'<br />'+
		'<a href="http://www.djpod.'+_ext+'">'+lang_sentence_2+'.</a>'+
		'</div>';
	
	$('#available').html(lang_html);
	$('#available').show();
	
	if(type == 'new') {
		$('#available').effect('pulsate', { times: 3 }, 800);
	}
}		
		

// Insertion d'un message en ajax, généralement
// suite à une requête $.get ou $.post
function prependMsg(type,msg)
{
	// On checke quel type de message on veut ajouter,
	// erreur ou confirmation
	switch(type) {
		case 'error': var title = djpod_msg['error']; break;
		case 'confirm': var title = djpod_msg['success']; break;
	}
	
	// On récupère la boite de messages
	var message_box = document.getElementById('message_box');
	
	// Le contenu du dialog
	var content = '<p id="message_hide"><a href="javascript:void(0);">'+
		'<img src="/img/blank.png" width="14" height="14"></a></p><strong>'+title+' :</strong>'+
		'<ul><li>'+msg+'</li></ul>';
	
	// Si la boîte de messages existe déjà dans la page
	if(message_box) {
		message_box.className = type;
		message_box.innerHTML = content;
	}
	
	// Sinon on l'insère
	else {	
		if($('#manage').is('div')) {
			$('#manage').prepend('<div id="message_box" class="'+type+'"><div>'+content+'</div></div>');
		}
		else {
			$('#container').prepend('<div id="message_box" class="'+type+'"><div>'+content+'</div></div>');
		}
	}
	
	$('#message_hide').click(function(){
		$('#message_box').slideUp();
	});
}

// Ouverture d'un dialog modal
function dialog(d_id,d_title,d_content,d_width,d_height,footer,d_class)
{
	// Si jamais le dialog qu'on veut ouvrir existe déjà,
	// on le supprime
	if($('#dialog-'+d_id).is('div')) {
		$('#dialog-'+d_id).remove();
	}
	
	// On rajoute le footer de fermeture
	d_content += '<div id="dialog-footer">'+(footer == true ? '<input type="submit" value="OK" />' : '')+'</div>';
	
	// On insère le dialog dans la colonne gauche
	$('#container').prepend('<div id="dialog-'+d_id+'"></div>');
	
	// Et on lui insère le contenu HTML demandé
	$('#dialog-'+d_id).html(d_content);	
	
	// Ouverture du dialog modal
	$('#dialog-'+d_id).dialog(
	{
    width: d_width,
    height: d_height,
		modal: true, 
    resizable: false,
    title: d_title,
    dialogClass: d_class
	});
	
	// Action de fermeture via le bouton dans le footer
	$('#dialog-footer').click(function() {
		$('#dialog-'+d_id).dialog('close');
	});
}

function dialog_close(d_id)
{
	$('#dialog-'+d_id).dialog('close');
}

function confirm(msg, callback)
{
	var content = '<div style="width:390px; text-align:center">'+msg+'<br /><br />'+
	'<input type="submit" value="'+djpod_msg['cancel']+'" class="cancel-button">&nbsp;'+
	'<input type="submit" value="'+djpod_msg['confirm']+'" class="confirm-button"></div>';
	
	dialog('confirm',djpod_msg['confirm_title'],content,420,(75+(msg.length/60)*25));
	
	$('#dialog-confirm').find('.confirm-button').click(function() {
		callback.apply();
		$('#dialog-confirm').dialog('close');
		return false;
	});
	
	$('#dialog-confirm').find('.cancel-button').attr("style", "color:#000;background:#eee;border-color:#ccc");
	
	$('#dialog-confirm').find('.cancel-button').click(function() {
		dialog_close('confirm');
		return false;
	});
}

function alert(msg)
{
	var content = '<div style="width:390px; text-align:center">'+msg+'<br /><br />'+
	'<input type="submit" value="'+djpod_msg['close_window']+'" class="cancel-button"></div>';
	
	dialog('alert',djpod_msg['warning'],content,410,(75+(msg.length/60)*20));
	
	$('#dialog-alert').find('.cancel-button').click(function() {
		dialog_close('alert');
		return false;
	});
}

function checkBoxes(elem)
{
	$("#check-all").click(function() {
		$(elem).find("input[type='checkbox']").each(function(){
			if(check == false) {
				if(this.disabled != true) {
					this.checked = true;
				}
			} else {
				this.checked = false;
			}
		});
		if(check == false) {
			check = true;
		} else {
			check = false;
		}
	});
}

function uncheckBoxes(elem)
{
	$(elem).find("input[type='checkbox']").each(function(){
		this.checked = false;
	});
}