var Front = new function()
{
  var options;
  
  this.init = function(opt)
  {
    Front.options = opt;
    $.facebox.settings.loadingImage = Util.img_src("../../dmsPlugin/css/facebox/loading.gif");
    $.facebox.settings.closeImage = Util.img_src("../../dmsPlugin/css/facebox/closelabel.gif");
    $.facebox.settings.classBody = 'ecommerce';
    Front.initDom($("#dms_layout_parts"));
    
  };
  
  this.initDom = function($dom)
  {
    
    /* DEBUT HOVER */
  $('.formFAQ .submit').hover(
    function(){
      $(this).attr('src','/images/faq/submitEnvoyerHover.png');
    },
    function(){
      $(this).attr('src','/images/faq/submitEnvoyer.png');
    }
  );
  $('.formContact .submit').hover(
    function(){
      $(this).attr('src','/images/installateur/btEnvoyerDemandeHover.jpg');
    },
    function(){
      $(this).attr('src','/images/installateur/btEnvoyerDemande.jpg');
    }
  );
  $('#formDevis .submit, .formProduit .submit').hover(
    function(){
      $(this).attr('src','/images/devis/btEnvoyerDemandeHover.jpg');
    },
    function(){
      $(this).attr('src','/images/devis/btEnvoyerDemande.jpg');
    }
  );
  $('.simulateurEtape6 .submit').hover(
    function(){
      $(this).attr('src','/images/simulateur/gotoNextValideOver.png');
    },
    function(){
      $(this).attr('src','/images/simulateur/gotoNextValide.png');
    }
  );
  /* FIN HOVER */
    
  $("#fb-root",$dom).each(function() {
        window.fbAsyncInit = function() {
          FB.init({
            appId  : "136939606330892",
            status : true, // check login status
            cookie : true, // enable cookies to allow the server to access the session
            xfbml  : true  // parse XFBML
          });
        };
    
        (function() {
          var e = document.createElement("script");
          e.src = document.location.protocol +   "//connect.facebook.net/fr_FR/all.js";
          e.async = true;
          document.getElementById("fb-root").appendChild(e);
        }());
      });

    $("#twitter",$dom).each(function() {
        $(this).getTwitter({
          userName: 'antoinebineau',
          numTweets: 3,
          loaderText: "Chargement des tweets...",
          slideIn: true,
          slideDuration: 750,
          showHeading: false,
          showProfileLink: false,
          showTimestamp: false
        });
      });
    $('.tabs_me',$dom).each(function() {$(this).tabs();});
    $('.facebox_me',$dom).each(function() {$(this).facebox();});
    $('.validate_me',$dom).each(function() {$(this).validate();});
    if($slider = $(".slider", $dom).orNot())
    {
      $slider.append('<ul class="cycle_pager"></ul>');
      $pager = $('.cycle_pager', $slider);
      if($('.slide_for_ie').length)
      {
        $slider.find('.cycle_slide').cycle({pager: $pager, pause: 1, speed: 100});
      }
      else
      {
    	$slider.find('.cycle_slide').cycle({pager: $pager, pause: 1});
      }
    }
    $('.empty_me',$dom).each(function() {
      $(this).prev().addClass("overlay").show();
      $(this).prev().click(function() {
        $(this).next().focus();
      });
      $(this).focus(function(){
        $(this).prev().fadeTo(200,0.5);
      }).keyup(function(){
        if($(this).val()==""){
          $(this).prev().show();
        }
        else
        {
          $(this).prev().hide();
        }
      }).blur(function(){
        if($(this).val()==""){
          $(this).prev().fadeTo(100,1);
        }
      });
    });
    if($devisSelect = $(".formDevis", $dom).orNot())
    {
      $('ul.radio_list .bouton_valid', $devisSelect).each(function(){
        $(this).css('display','none');
      });
      $('.typeDemande ul.radio_list input', $devisSelect).each(function(){
        $(this).rebind('click', function(){
          $.ajax({
            url: Util.light2href("demandeDevis/formAjax"),
            beforeSend: function(data)
            {
              var loadingMsg = "<div class='center_me'>"+Util.img('66/loader.gif','')+"</div>";
              $('.type_formulaire_contact', $devisSelect).html(loadingMsg);
            },
            data:
            {
              type_devis: $(this).attr('value')
            },
            success: function(data)
            {
              $('.type_formulaire_contact', $devisSelect).html(data);
              $(".validate_me", $devisSelect).each(function() { $(this).validate(); });
            }
          });
        });
      });
    }
  /*********** SIMULATEUR ***********/
  /* Etape 1 */
    $('.simulateur #departement_flash',$dom).each(function() {
        $('#field_wrapper').css('display','none');
    $(this).media({
      src: Front.options.base_url+"swf/carte.swf",
      width: 480,
      height: 400,
      flashvars: {'gateWayURL': Front.options.script_name+"dms/dmsTools/amf?fromFlash=1"}
    });
      });
  /* Etape 2 */
    $('.simulateur #degree_slider',$dom).each(function() {
        var metas = $(this).metadata();
        var orientations = metas.orientations;
        var defaultValue = $("#simulateur_orientation_degree").val();
		var defaultSliderValue = 0;
		if(defaultValue > 0)
			defaultSliderValue = defaultValue / 10;
        $('#field_wrapper').css('display','none');
        $(this).slider({
          min:   0,
          max:   36,
          startValue: defaultSliderValue,
		  step:  1
        }).bind("slide", function(ui, ev, val) {
          val = val || ev;
          ui = ui || ev;
          parseOrientationsEtape2(orientations, val.value*10);
        });
        // Init
        parseOrientationsEtape2(orientations, defaultValue);
      });
  /* Etape 4 */
    if($simulateur_etape_4 = $('.simulateurEtape4').orNot())
    {
      Front.calculSurface($simulateur_etape_4);
    }
  /* Etape 5 */
    
    if($simulateur_etape_5 = $('.simulateurEtape5').orNot())
    {
      Front.convertDegToPourcent($simulateur_etape_5);
    }
    
  }
  
  this.convertDegToPourcent = function($simulateur_etape_5)
  {
    $('.radio_list input', $simulateur_etape_5).each(function(){
      $(this).rebind('click', function(){
        $('.pente_field_libre input').each(function(){
          $(this).val('');
        });
      });
    });
    $('.pente_field_libre input', $simulateur_etape_5).each(function(){
      $(this).rebind('click', function(){
        $('.pente_approximative_null input').click();
      });
    });
    
    $('#simulateur_pente_degres', $simulateur_etape_5).rebind('keyup', function(){
      $.ajax({
        url: Util.light2href("simulateur/calculDegToPourcent"),
        data:
        {
          deg_value: $(this).attr('value')
        },
        success: function(data)
        {
          $('#simulateur_pente_pourcentage').val(data);
          Front.calculSurface($simulateur_etape_5);
        }
      });
    });
    $('#simulateur_pente_pourcentage', $simulateur_etape_5).rebind('keyup', function(){
      $.ajax({
        url: Util.light2href("simulateur/calculPourcentToDeg"),
        data:
        {
          pourcent_value: $(this).attr('value')
        },
        success: function(data)
        {
          $('#simulateur_pente_degres').val(data);
          Front.calculSurface($simulateur_etape_5);
        }
      });
    });
  }
  
  this.calculSurface = function($simulateur_etape_4)
  {
    $('.calcul_surface_field', $simulateur_etape_4).each(function(){
      $(this).rebind('keyup', function(){
        $check_is = null;
        $('.surface_options input:checked').each(function(){
          $check_is = $(this).val();
        });
        $.ajax({
          url: Util.light2href("simulateur/calculSurface"),
          data:
          {
            longueur_toiture: $('#simulateur_longueur_toiture').attr('value'),
            largeur_toiture: $('#simulateur_largeur_toiture').attr('value'),
            option_surface : $check_is
          },
          success: function(data)
          {
            $('.surface_toiture_field').html(data);
            Front.calculSurface($simulateur_etape_4);
          }
        });
      });
    });
    $('.help_option_surface', $dom).each(function(){
        $meta = $(this).metadata();
       $(this).qtip({
         content: $meta.content_help,
         position: {
           corner: {
              target: 'topMiddle',
              tooltip: 'bottomMiddle'
           }
         },
         show:
         {
             when:
             { 
               event: 'mouseover'
             }
         },
         hide:
         {
           when: 'mouseout'
         },
         style:
         {
           width: 230,
           background: '#E85711',
           border:
           {
             color: '#E85711',
             radius: 4
           },
           color: '#fff',
           tip: 'bottomMiddle'
         }
       });
    });
  }
  
}
function selectDepartement(code, nom) {
  $("#departementSelected .wrapper").html(nom+" <span>("+code+")</span>");
  $("#simulateur_departement").val(code);
}
function parseOrientationsEtape2(orientations, value) {
  if(!value) value = 0;
  var c = 0;
  var nomOrientation = "";
  var sigleOrientation = "";
  while(orientation = orientations[c])
  {
    if(value >= orientation.min && value <= orientation.max) {
      nomOrientation = orientation.nom;
      sigleOrientation += orientation.sigle;
    }
    c++;
  }
  $("#expositionSelected .wrapper").html(nomOrientation+" <span>("+value+"°)</span>");
  $("#orientation_rosace").attr('class','orientation_rosace_'+sigleOrientation);
  $("#orientation_exposition").attr('class','orientation_exposition_'+sigleOrientation);
  $("#simulateur_orientation").val(nomOrientation);
  $("#simulateur_orientation_degree").val(value);
  
}
Dms.registerController(Front);

