/**
 * VRP/RAC : common
 *
 * Copyright (c) 2009-2010 Julien Le Bren (julien.lebren@createurdimage.fr)
 * Créateur d'Image - http://www.createurdimage.fr
 *
 * Version: 1.0
 * Création: 2010-10-14
 * Modification : 2010-10-14
 */

var check = false;
var adv_next_li = 1;
var adv_nb_li;
var adv_li = new Array();
var adv_freeze = false;

$(function()
{ 
	adv_nb_li = $('#entreprises li').length;
	
	for(i=0; i < adv_nb_li; i++) {
		adv_li.push($('#entreprises li:eq('+i+')').html());
	}
	$('#entreprises li').each(function() { $(this).remove(); });
	
	for(i=0; i < 1; i++) {
		$('#entreprises ul').append('<li>'+adv_li[i]+'</li>');
	}
	
	$('#entreprises ul').fadeIn('fast');
	setTimeout('moveLi();',4000);
	
	$('#entreprises ul').mouseover(function() {
		adv_freeze = true;
	});
	
	$('#entreprises ul').mouseout(function() {
		adv_freeze = false;
	});
	
	//columnsToBottom();
	
	$('#menu_home').hover(function() {
		$('#menu-main').css({backgroundPosition: '0px -49px'});
		$(this).find('a').css('color','white');
	}, function() { 
		$('#menu_home').find('a').css('color','black');
		initPosition(); 
	});
	
	$('#menu_entreprise').hover(function() {
		$('#menu-main').css({backgroundPosition: '0px -98px'});
		$(this).find('a').css('color','white');
	}, function() { 
		$('#menu_entreprise').find('a').css('color','black');
		initPosition(); 
	});
	
	$('#menu_vrp').hover(function() {
		$('#menu-main').css({backgroundPosition: '0px -148px'});
		$(this).find('a').css('color','white');
	}, function() { 
		$('#menu_vrp').find('a').css('color','black');
		initPosition(); 
	});
	
	$('#menu_annonces').hover(function() {
		$('#menu-main').css({backgroundPosition: '0px -197px'});
		$(this).find('a').css('color','white');
	}, function() { 
		$('#menu_annonces').find('a').css('color','black');
		initPosition(); 
	});
	
	$('#menu_contact').hover(function() {
		$('#menu-main').css({backgroundPosition: '0px -246px'});
		$(this).find('a').css('color','white');
	}, function() { 
		$('#menu_contact').find('a').css('color','black');
		initPosition(); 
	});
	
	function initPosition(){
		$('#menu-main').css({backgroundPosition: '0px 0px'});
	}
});

function moveLi()
{
	if(adv_freeze == true) {
		setTimeout('moveLi();',4000);
		return;
	}
	
	$('#entreprises li:eq(0)').fadeOut('slow', function()
	{
		$('#entreprises li:eq(0)').remove();
		$('#entreprises ul').html('<li style="display:none">'+adv_li[adv_next_li]+'</li>');
		
		$('#entreprises li:eq(0)').fadeIn('slow', function()
		{
			adv_next_li = adv_next_li+1;
			
			if(adv_next_li == adv_nb_li) {
				adv_next_li = 0;
			}
			
			setTimeout('moveLi();',4000);
		});
	});
}

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;
	});
}

function prependMsg(type,msg)
{
	switch(type)
	{
		case 'error': var title = 'Erreur'; break;
		case 'warning': var title = 'Avertissement'; break;
		case 'confirm': var title = 'Action rÃ©ussie'; break;
	}
	
	var message_box = document.getElementById('message');
	
	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>';
	
	if(message_box) {
		message_box.className = type;
		message_box.innerHTML = content;
	}
	else {	
		$('#middle').prepend('<div id="message" class="'+type+'">'+content+'</div>');
	}
	
	$.scrollTo(0,800);
	
	$('.'+type).effect("pulsate", { times: 3, duration: 50 });
	
	$('#message_hide').click(function() {
		$('#message').fadeOut('slow');
	});
}

function columnsToBottom()
{
	if($('#col_right').height() > $('#col_left').height())	{	
		$('#col_left').height($('#col_right').height());
	}
	else {
		$('#col_right').height($('#col_left').height());
	}
}

function dialog(d_id,d_title,d_content,d_width,d_height)
{
	if($('#dialog-'+d_id).is('div')) {
		$('#dialog-'+d_id).remove();
	}
	
	$('#middle').prepend('<div id="dialog-'+d_id+'"></div>');
	$('#dialog-'+d_id).html(d_content);	
	
	$('#dialog-'+d_id).dialog(
	{
		width: d_width,
		height: d_height,
		modal: true, 
		resizable: false,
		title: d_title,
		show: 'slow'
	});
}

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="Annuler" class="cancel-button">&nbsp;'+
		'<input type="submit" value="Confirmer" class="confirm-button"></div>';
	
	dialog('confirm','Demande de confirmation',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="Fermer cette fenÃªtre" class="cancel-button"></div>';
	
	dialog('alert','Avertissement',content,410,(75+(msg.length/60)*20));
	
	$('#dialog-alert').find('.cancel-button').click(function() {
		dialog_close('alert');
		return false;
	});
}




