var Ctrl = new function()
{
  var options;
  
  this.init = function(opt)
  {
    Ctrl.options = opt;
    
    Ctrl.initDom($("#dms_layout_parts"));

    /*
    if (typeof $.jmap != "undefined") {
      Ctrl.gmapIcon = $.jmap.createIcon({
        iconImage: "http://www.vm-materiaux.fr/images/icoGmap.png",
        iconSize:  new GSize(34, 39)
      });
    }*/
  };
  
  this.initDom = function($dom)
  {
    /* Gestion du bandeau */
  
  if($('.no_flash').length)
  {
    $('.head_page_accueil_cycle').each(function(){
        $(this).cycle({
          speed: 3000
        });
      });
     

    if($gamme_bandeau_json = $("body div.layoutGamme").orNot())
    {
      if($gamme_bandeau_json = $("body div.layoutGamme div.gamme_bandeau_json").orNot())
      {
        imgToLoad = $gamme_bandeau_json.metadata().url;
      }
      else if($("body div.layoutGamme").length > 0)
      {
        imgToLoad = Ctrl.options.relative_url_root+'images/bandeau-default-gamme.jpg';
      }
    }
    $('.flashbg.no_flash .background_me img').attr('src',imgToLoad);
  }
  else
  {
    var imgToLoad = Ctrl.options.relative_url_root+'images/bandeau-groupe.jpg';

      if($gamme_bandeau_json = $("body div.layoutGamme div.gamme_bandeau_json").orNot())
      {
        imgToLoad = $gamme_bandeau_json.metadata().url;
      }
      else if($("body div.layoutGamme").length > 0)
      {
        imgToLoad = Ctrl.options.relative_url_root+'images/bandeau-default-gamme.jpg';
      }
      else if($("body.main_page").length > 0)
      {
        imgToLoad = Ctrl.options.relative_url_root+'swf/bandeau-home.swf';
      }
      
      $("div.background_me", $dom).media({
        src: Ctrl.options.relative_url_root+'swf/bandeau.swf',
        autoplay: 1,
        width: 982,
        height: 280,
        flashVersion: '9',
        params: {allowScriptAccess: 'always',scale: "noscale"},
        flashvars: {imgToLoad: imgToLoad}
      });
  }
  
    
    /* Fin Gestion du bandeau */
    //console.log("Debug links :");
    $("div.little_home div.liste li.element, div.little_inner div.liste li.element", $dom).each(function() {
      var href = $(this).find("a").attr('href');
      //console.log(href);
      $(this).find("img").click(function(){window.location = href;});
    });
    
    /* Génération d'images des H1 */
    $(".gd_me:not(.gd_me_done)",$dom).each(function()
    {
      var texteBrut = $(this).html();
      var texte = escape(texteBrut);
      var gdwidth = $(this).width() - 10;
      var url = Util.action2href("dms/main/fakeTitre?w="+gdwidth+"&titre="+texte);
      $(this).html($("<img>").attr("src",url).attr("alt",texteBrut)).addClass("gd_me_done");
      //alert(tag.html());
    });
    
//    if ($('div.slot_for_main_listeMetier').length) {
//      $('div.lien_ancre a').each(function(){
//        $(this).rebind('click', function(){
//          dbg('test');
//          $('#content .inner').scrollTo($($(this).attr('href')), {speed:2000,axis:'x', queue:true});
//        });
//      });
//    };
    
    if ($tabs = $("div.tabs_me > ul").orNot())
    {
      $tabs.tabs({
        fx: { opacity: 'toggle', duration: 500 }
      });
    }
    if ($gmap = $("div.my_gmap").orNot())
    {
      Ctrl.launchGmap($gmap);
    }
    
    $("a.facebox_me, div.facebox_me a", $dom).each(function() {
      var $this = $(this);
      var $big_image = $("<img>").attr("src", $this.attr("href"));
      $this.rebind("click", function(){
        $.facebox($big_image);
        $("#facebox div.header span").remove();
        $("#facebox div.header").prepend(
          "<span class='copyright'>"+$this.metadata().copyright+"</span>"
//          $('<a class="download_link" href="'+$this.attr("rel")+'" target="_blank">Télecharger en grand format</a>')
        );
        return false;
      });
    });
    
    $("form.validate_me").each(function(){
      $(this).validate({
        
      });
    });
    if ($("#vm_intersticiel").length)
    {
      $.facebox($("#vm_intersticiel").show());
    }
  };
  
  this.launchGmap = function($elem)
  {
    if (!$.fn.jmap)
    {
      $elem.html(Dms.alertMsg("Rechargez la page pour activer l'API GoogleMap"));
      return;
    }
    var metas = $elem.metadata();
    
    var zoom = parseInt(metas.zoom);
    
    $elem.jmap('init', {
      'mapType':G_NORMAL_MAP,
      // Initial map center
      mapCenter: metas.center,
      // Initial zoom level
      mapZoom: zoom
    });
/*
    var icon = Mapifies.createIcon({
      iconImage: "http://www.vm-materiaux.fr/images/icoGmap.png",
      iconShadow: "http://www.vm-materiaux.fr/images/shadowGmap.png",
      iconSize:  new GSize(34, 39)
    });
    icon.imageMap = [0,0, 0,38, 33,38, 33,0];*/

    var placeAgence = function(agence)
    {
      $elem.jmap('AddMarker', { /*pointIcon: icon, */pointLatLng:[agence.lat, agence.lng], pointHTML: "<img style='vertical-align: middle; margin-right: 5px;' src='"+agence.image_url+"' /><a href='"+agence.url+"'>"+agence.text+"</a>" || null});
    }
    
    for(var it in metas.agences)
    {
      placeAgence(metas.agences[it]);
    }

  };
  
  this.launchGmapTest = function($elem)
  {
    //$elem.jmap('init', {mapCenter:[55.958858,-3.162302]});
  };
};
Dms.registerController(Ctrl);
