/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function initUI()
{
	// -----------------------
	// Ouverture submenu
	$('#submenu ul li ul').hide();
	$('#submenu ul li ul li.selected').parent().show();

	$('#submenu ul li a[href=#]').click(function(){		
		if ($(this).parent().find('ul.child').css('display') == 'none') {
			$('#submenu ul li ul').hide(200);
			$(this).parent().find('ul.child').show(500);
		} else {
			$('#submenu ul li ul').hide(200);
		}
	});
	// -----------------------
	// Declaration colorbox
	$('a.colorBox').colorbox({
		onComplete:function(){
			$("#cboxLoadedContent img").click(function(){
				$('a.colorBox').colorbox.close();
			});
		}
	});

	$(".lien_covoiturage").each(function(){
		var afficher = $(this).attr('title');
		if(afficher!='div_1'){
			$("#"+afficher).hide();
		}
	});
	$(".lien_covoiturage").click(function(){
		var afficher = $(this).attr('title');
		if($("#"+afficher).is(':visible')){
			$("#"+afficher).hide();
		}else{
			$("#"+afficher).show();
		}
		return false;
	});
	 
	// -----------------------
	// Toogler
	toggleTabs();
	// -----------------------
	// HomeContent
	displayHomeContent();
	// -----------------------
	// Ouverture recherche
	$('#tools ul li a.search').click(function(){
			$('#spotlight').css({'display' : 'block'});
	});
}

/**
 *
 */
function checkRequired(element)
{
	var value		= $(element).val();
	var required	= $(element).attr('required');
	if(value == '' && required) {
		$(element).addClass('missing');
		return false;
	} else {
		$(element).removeClass('missing');
		return true;
	}
}

/**
 *
 */
function pulseMissing(parent)
{
	$(parent).find('[required]').each(function(){
		var element = $ (this). get (0);
		if (!checkRequired(element)) {
			$(this).effect("pulsate", {times:2}, 500);
		}
	});
}

/**
 * 
 */
function checkAllRequired(parent)
{
	var tousPresent = true;
	
	$(parent).find('[required]').each(function(){
		var element = $ (this). get (0);
		var result = checkRequired(element);
		if (!result ) {
			tousPresent = false;
		}
	});

	$(parent).find('[validation=email]').each(function() {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		if(!emailReg.test($(this).val())) {
			tousPresent = false;
			$(this).addClass('missing');
			$(this).effect("pulsate", {times:2}, 500);
		}
	})

	if (!tousPresent) {
		pulseMissing(parent);
	}
	return tousPresent;
}

/**
 *
 */
function toggleTabs()
{
	$('.toggler').live('click',function(){
		var id = $(this).attr('id');
		var tab = id.split('_')[1];
		var old_id = $(this).parent().parent().find('.selected').attr('id');
		if(old_id != '')
		{
			var old_tab = old_id.split('_')[1];
			// on masque l'onglet ouvert
			$(this).parent().parent().find('.selected').removeClass('selected');
			$('#tab_'+old_tab).removeClass('selected');
			//$('#tab_'+old_tab).hide();
		}
		// on ouvre le nouvel onglet
		$(this).addClass('selected');
		$('#tab_'+tab).addClass('selected');
		//$('#tab_'+tab).show();
		return false;
	});
}
/**
 *
 */
function egbxPrint() {
    if (document.all) {
        if (navigator.appVersion.indexOf("5.0") == -1) {
            var OLECMDID_PRINT = 6;
            var OLECMDEXECOPT_DONTPROMPTUSER = 2;
            var OLECMDEXECOPT_PROMPTUSER = 1;
            var WebBrowser = "<OBJECT ID=\"WebBrowser1\" WIDTH=0 HEIGHT=0 CLASSID=\"CLSID:8856F961-340A-11D0-A96B-00C04FD705A2\"></OBJECT>";
            document.body.insertAdjacentHTML("beforeEnd", WebBrowser);
            WebBrowser1.ExecWB(6, 2);
            WebBrowser1.outerHTML = "";
        } else {
            self.print();
        }
    } else {
        self.print();
    }
}
/**
 *
 */
function displayHomeContent()
{

	$('#submenu .infoplus').live('mouseover',function(){
		$('#home_content').css({'display' : 'block'});
		return false;
	});
	$('#submenu .infoplus').live('mouseout',function(){
		$('#home_content').css({'display' : 'none'});
		return false;
	});
	$('#submenu .infoplus').live('mouseleave',function(){
		$('#home_content').css({'display' : 'none'});
		return false;
	});
}

