
var GE_config = {
  GE_baselink:"/",
  GE_offleftClass:"offleft",
  GE_flash1: {
    GE_tag: "h1",
    GE_swf: "/swf/GE_Inspira_Medium.swf",
    GE_swfVars: {
      GE_color:"333333", // Unit : Hexadecimal
      GE_leading: -1,
      GE_fontsize: 20  //Unit : pixel
    }
  },
  GE_flash2: {
    GE_tag: "h2.intro",
    GE_swf: "/swf/GE_Inspira.swf",
    GE_swfVars: {
      GE_color:"505060",  // Unit : Hexadecimal
      GE_leading: 8,
      GE_fontsize: 13   //Unit : pixel
    }
  }
}

function GE_show(elt) {
  targetNodesSize = jQuery(elt).size();
  if (targetNodesSize < 1) { return 0; }
  jQuery(elt).each(
    function() {
      if (jQuery(this).is("."+GE_config.GE_offleftClass)) {
        jQuery(this).removeClass(GE_config.GE_offleftClass);
      }
    }
  );
  return targetNodesSize;
}

function GE_hide(elt) {
  targetNodesSize = jQuery(elt).size();
  if (targetNodesSize < 1) { return 0; }
  jQuery(elt).each(
    function() {
      if (jQuery(this).not("."+GE_config.GE_offleftClass)) {
        jQuery(this).addClass(GE_config.GE_offleftClass);
      }
    }
  );
  return targetNodesSize;
}

function GE_ieHack() {
  if (!jQuery.browser.msie) { return 0; }
  jQuery("#countryChoice ul").bgiframe();
  return 1;
}

function listingTable() {
  var listingEl = jQuery(".list li");
  if (jQuery(listingEl).size() < 1) { return 0; }
  for(i=0; i<jQuery(listingEl).size(); i+=2) {
      var heightBlockMax=0;
      block1 = jQuery(".list li:eq(" + i + ")");
      block2 = jQuery(".list li:eq(" + (i+1) + ")");
      if (block2) {
        heightBlock1 = block1.height();
        heightBlock2 = block2.height();
        heightBlockMax = (heightBlock1 > heightBlock2) ? heightBlock1 : heightBlock2;
        block1.css("height",heightBlockMax);
        block2.css("height",heightBlockMax);
      }
  }
  if (jQuery(listingEl).parent().parent("div").not("#homeDE").hasClass("homeContent")) {
  	jQuery(listingEl).each( function() {
	    var listingElTextLnks = jQuery("div:eq(0)",this);
	    if (jQuery(listingElTextLnks).size() > 0) {
			var padd = Math.ceil( (jQuery(this).height() - jQuery(listingElTextLnks).height()) / 2 -1);
			jQuery(listingElTextLnks).css("padding", padd + "px 0 0 0");
	    }
	  });
  }
  return 1;
}

function GE_sIFR(targetNode, targetSwf, targetStyle) {
  if (jQuery(targetNode).size() < 1) { return 0; }
  swfWidth = jQuery(targetNode).width();
  swfHeight = jQuery(targetNode).height() + 6;
  return  jQuery(targetNode).flash({src:targetSwf, width:swfWidth, height:swfHeight, flashvars: {gridfittype:"pixel", zoomsupport:"false", opacity:"100", kerning:"true", lines:"1", zoom:"100", selectable:"true", content:jQuery(targetNode).get(0).innerHTML, width:swfWidth, height:swfHeight, css:targetStyle[0], size:targetStyle[1] }});
}


// fontion pour cacher input:img et faire marcher les selects au onChange
function accessibleSelects() {
  if (jQuery(".accessibleForm").size() < 1) { return 0; }
  additionnalOptions = jQuery(".accessibleForm").find(".additionalOption");
  inputs = jQuery(".accessibleForm").find("input:image");
  selects = jQuery(".accessibleForm").find("select");
  // cacher les inputs
  jQuery(inputs).each( function() {
  	if (!jQuery(this).hasClass('normalImgInput')) jQuery(this).addClass("offleft");
  } );
  // cacher les selects d'options supp.
  jQuery(additionnalOptions).each( function() { jQuery(this).addClass("offleft"); } );
  // modifier les selects pour 1.affichage de nouveau(x) select(s) ou 2.redirection vers url
  jQuery(selects).each(
    function() {
      jQuery(this).addClass("alone");
      jQuery(this).change(
        function() {
          curIndex = jQuery(this).get(0).selectedIndex;
          curValue = jQuery(this).get(0).options[curIndex].value;
		  currentAdditionnalOptions = jQuery(this).parent('p').children('.additionalOption');
          if ((curIndex > 0) || (curValue != "")) {
            switch(curValue.substr(0,1)) {
              case "#" :
	              jQuery(currentAdditionnalOptions).each( function() { jQuery(this).addClass("offleft"); } );
				  jQuery(curValue).removeClass("offleft");
              break;
              case "h" :
	              document.location.href = curValue;
              break;
              default :
			      jQuery(currentAdditionnalOptions).each( function() { jQuery(this).addClass("offleft"); } );
              break;
            }
          }
        }
      );
    }
  );
  jQuery('.withMore').each( function() {
    jQuery(this).click( function() {
	  curValueRadio = jQuery(this).val();
	  currentAdditionnalOptions = jQuery(this).parent().parent().parent('div').children('.additionalOption');
      switch(curValueRadio.substr(0,1)) {
		case "#" :
			jQuery(currentAdditionnalOptions).each( function() { jQuery(this).addClass("offleft"); } );
			jQuery(curValueRadio).removeClass("offleft");
			jQuery(curValueRadio).css({'padding-top':'10px'});
		break;
		default :
			jQuery(currentAdditionnalOptions).each( function() { jQuery(this).addClass("offleft"); } );
		break;
		}
	});
  });
  return 1;
}

// all superfish menus
function mySuperfish(elt) {
	if (jQuery(elt).size() < 1) { return 0; }
	jQuery(elt).superfish();
	if (jQuery(elt).attr("id") == "tools" ) {
    	jQuery("#main").css("padding-bottom","70px");
	}
	return 1;
}

// FAQs accordion
function faqAccordion() {
	if (jQuery(".faq").size() < 1) { return 0; }
	jQuery(".faq").Accordion({
    	active: false,
		alwaysOpen: false,
		selectedClass: 'active',
		animated: false
	});
	return 1;
}

// Used Car Box Detail
function usedCarDetail() {
	if (jQuery("#carouselUsedCarDetail").size() < 1) { return 0; }
	// ajout de divs dans popup pour stylage ombré
	//jQuery('#usedCarDetailPopup').prepend('<div id="topPopup"></div>').append('<div id="bottomPopup"></div>');
	// initialisation du carousel principal
    jQuery("#carouselUsedCarDetail").jcarousel({scroll:1,animation:'slow',buttonNextHTML:'<p><a href="#"></a></p>',buttonPrevHTML:'<p><a href="#"></a></p>'});
    // fonction pour controler l'img visible dans le carousel de popup
	function mycarousel_initCallback(carousel) {
	    jQuery('.detailImg .mediumImg').bind('click', function() {
			// scroller le carousel jusqu'au bon numéro
	        carousel.scroll(jQuery.jcarousel.intval(jQuery('#bigImgDetail').attr('title')));
	        return false;
	    });;
	};
    // lancement du carousel
	jQuery('#carouselUsedCarDetailPopup').jcarousel({initCallback: mycarousel_initCallback,scroll:1,animation:false,buttonNextHTML:'<p><a href="#"></a></p>',buttonPrevHTML:'<p><a href="#"></a></p>'});
    // cachage du div de popup
    jQuery('#usedCarDetailPopup').addClass('offleft').removeClass('visible');
    // au click sur un des éléments du carousel principal, on change l'img medium
	jQuery('#carouselUsedCarDetail li a').click( function() {
		// recup du href pour nouveau src et du title pour attribution n° à img pour controle du carousel de popup
        var newSrc = jQuery(this).attr("href");
		var newTitle = jQuery(this).attr("title");
		// recup de la bigimg
		var bigImg = jQuery('#bigImgDetail');
		// à laquelle on mets les nouveaux attributs
		jQuery(bigImg).attr({src: newSrc,title: newTitle});
		// on mets à off chaque lien de la liste
		jQuery(this).parent().parent().children('li').children('a').each( function() {
			jQuery(this).removeClass('on').addClass('off');
		});
		// et on mets à on le lien cliqué
        jQuery(this).removeClass('off').addClass('on');
		return false;
	});
	// au click sur l'img medium, affichage du div popup
	jQuery('.detailImg .mediumImg').click( function() {
		// ajout du lien de fermeture de popup
        jQuery('#usedCarDetailPopup').append('<p><img src="img/bt_closepopup.gif" alt="Close" /></p>');
		// affichage de la popup
		jQuery('#usedCarDetailPopup').removeClass('offleft').addClass('visible');
		// au click sur l'image de fermeture
		jQuery('#usedCarDetailPopup p img').click(function() {
			jQuery('#usedCarDetailPopup').addClass('offleft').removeClass('visible');
		});
	});
	return 1;
}

// Tabbed Forms
function tabbedForms() {
	if (jQuery(".formTabs").size() < 1) { return 0; }
	var tabsQuantity = jQuery(".formTabs li").size();
    switch(tabsQuantity) {
		case 1 :
        	jQuery(".formTabs").addClass('oneTabs');
		break;
		case 2 :
            jQuery(".formTabs").addClass('twoTabs');
		break;
		case 3 :
            jQuery(".formTabs").addClass('threeTabs');
		break;
		case 4 :
            jQuery(".formTabs").addClass('fourTabs');
		break;
		case 5 :
            jQuery(".formTabs").addClass('fiveTabs');
		break;
		default :
			jQuery(".formTabs").addClass('oneTabs');
		break;
		}
	return 1;
}


// Finance Assistant Popup
function financeAssistantPopup() {
	if (jQuery("#morePopup").size() < 1) { return 0; }
	// ajout de divs dans popup pour stylage ombré
	// jQuery('#morePopup').prepend('<div id="topPopup"></div>').append('<div id="bottomPopup"></div>');
    // passage en pos absolute pour la popup
	jQuery("#morePopup").css('position','absolute');
	// ajout du lien de fermeture de popup
    jQuery('#morePopup').append('<p id="closeMorePopup"><img src="img/bt_closepopup.gif" alt="Close" /></p>');
	// cachage du div de popup
    jQuery('#morePopup').addClass('offleft');
    // au click sur l'img medium
	jQuery('.txtAssistant .nota a').click( function() {
		// affichage de la popup
		jQuery('#morePopup').removeClass('offleft').addClass('visible');
		// cachage de la photo de l'assistant
        jQuery('#imgAssistant').hide();
		return false;
	});
	// au click sur l'image de fermeture
	jQuery('#closeMorePopup').click(function() {
		// cachage popup
		jQuery(this).parent('#morePopup').removeClass('visible').addClass('offleft');
		// affichage image assistant
		jQuery('#imgAssistant').show();
	});
	return 1;
}

jQuery(document).ready(
  function() {

    // Initialize Internet Explorer Hack (bgiframe)
    ie6 = GE_ieHack();

	// mainNav
   	initMainNav = mySuperfish("ul#mainNav");

    // Country Choixe
	initCountryChoice = mySuperfish("ul#countryChoice");

    // sIFR
    //headerTitle = GE_sIFR(GE_config.GE_flash1.GE_tag, GE_config.GE_flash1.GE_swf, Array(".sIFR-root%7Bfont-size%3A" + GE_config.GE_flash1.GE_swfVars.GE_fontsize + "px%3Bcolor%3A#" + GE_config.GE_flash1.GE_swfVars.GE_color + "%3Bleading%3A" + GE_config.GE_flash1.GE_swfVars.GE_leading + "%3B%7B",GE_config.GE_flash1.GE_swfVars.GE_fontsize));
    //headerTitleDescription = GE_sIFR(GE_config.GE_flash2.GE_tag, GE_config.GE_flash2.GE_swf, Array(".sIFR-root%7Bfont-size%3A" + GE_config.GE_flash2.GE_swfVars.GE_fontsize + "px%3Bcolor%3A#" + GE_config.GE_flash2.GE_swfVars.GE_color +"%3Bleading%3A" + GE_config.GE_flash2.GE_swfVars.GE_leading + "%3B%7D",GE_config.GE_flash2.GE_swfVars.GE_fontsize));

    // selects that work without input buttons
    noInputSelect = accessibleSelects();

	// 2 cols listing, vertical centering of text
    tables  = listingTable();

    // "bookmark and share" tool
	bookmarkAndShare = mySuperfish("ul#tools");

	// FAQ list
	showAndHideFAQs = faqAccordion();

    // Used Car Box Detail
	usedCarDetailCarouselAndPopup = usedCarDetail();

    // Tabbed Forms
	tabsWidth = tabbedForms();

	// Finance Assistance Popup
	showAndHideMorePopup = financeAssistantPopup();

  }
);

