﻿var app = app || {
   context: {
      culture: 'fr', // en | fr
      path: '' // application path
   },
   cache: undefined,
   config: {},
   flash: function(id, src, width, height, flashvars) {
      if (swfobject.hasFlashPlayerVersion("9.0.0") && src != '') {
         if (typeof (flashvars) == 'undefined')
            flashvars = {};
         if (typeof (width) == 'undefined')
            width = 480; ;
         if (typeof (height) == 'undefined')
            height = 360;
         var params = { allowfullscreen: "false", allowScriptAccess: "always", wmode: "opaque" };
         swfobject.embedSWF(src, id, width + "px", height + "px", "9.0.0", "", flashvars, params, {});
      }
   },
   changeTitleImage: function(index) {
      var titleImage = undefined;
      var animspeed = ($.browser.msie ? 0 : 'normal');
      if (app.cache['#titleImages'] != undefined)
         titleImage = app.cache['#titleImages'];
      else
         titleImage = $('#titleImages');
      if (titleImage.length) {
         app.cache['#titleImages'] = titleImage;
         var current = titleImage.find('img:visible');
         if (current.length)
            current.fadeOut(animspeed, function() {
               var next = titleImage.find('img:eq(' + index + ')');
               if (next.length)
                  next.fadeIn(animspeed);
            });
         else {
            var next = titleImage.find('img:eq(' + index + ')');
            if (next.length)
               next.fadeIn(animspeed);
         }


      }
   },
   init: function() {
      this.cache = new Array();
      this.initHideButtons();

      $('.main-nav > .container > ul > li').children('a.expand').click(function() {
         var self = $(this);
         $('.main-nav > .container > ul > li').children('a.expand').next().attr('style', '');
         self.next().show();
         self.parent().addClass('active').mouseleave(function() { $(this).removeClass('active').children('ul').attr('style', ''); }); ;
      });

      $('a[rel=external]').attr('target', '_blank');
      // Initialisation des bannières / rotation / petits points cliquables
      $('#views-links').banners({
         target: '#views',
         onLoad: $.fullsize.me,
         onChange: app.changeTitleImage,
         autoStartSlideshow: true,
         targetContent: '#banners-content',
         slideshowSpeed: 8000
      });
      // Initialisation des images fullsize
      $('#views .banner-img').fullsize({ 'target': '#wrap-body' });
   },
   initHighlights: function() {
      var highlights = $('#highlights > ul');
      var items = highlights.find('.highlight');
      var highlightIndex = 0;
      var highlightCount = items.length;
      if (highlightCount > 1) {
         items.each(function(i) { $(this).css('left', (i * 280)); });
         $('#highlights > .next').click(function() {
            if (highlights.children(':animated').length == 0) {
               if (parseInt(highlights.children('li:last').css('left'), 10) < 280) {
                  var first = highlights.children('li:first').remove();
                  first.css('left', '280px');
                  first.appendTo(highlights);
               }
               highlights.find('li').animate({ 'left': '-=280px' });
            }
         });
         $('#highlights > .prev').click(function() {
            if (highlights.children(':animated').length == 0) {
               if (parseInt(highlights.children('li:first').css('left'), 10) >= 0) {
                  var first = highlights.children('li:last').remove();
                  first.css('left', '-280px');
                  first.prependTo(highlights);
               }
               highlights.find('li').animate({ 'left': '+=280px' });
            }
         });
      } else
         $('#highlights').children('.next, .prev').hide();
   },
   initSlider: function(selector, target) {
      $(selector).each(function() {
         var obj = $(this);
         var t = obj.find(target);
         t.data('to_top', parseInt(t.css('top'), 10) - t.outerHeight() + $('.title', t).outerHeight() + 'px');
         t.data('ori_top', t.css('top'));
         var a = obj.find('a');
         if (a.length) {
            var url = a.attr('href');
            var rel = a.attr('rel');
            obj.css('cursor', 'pointer');
            obj.click(function() {
               if (rel == 'external')
                  window.open(url, '_blank');
               else
                  document.location = url;
            });
            a.attr('href', 'javascript:;').removeAttr('target');
         }
         obj.mouseenter(function() {
            var obj = $(this);
            var t = obj.find(target);
            t.stop().animate({ 'top': t.data('to_top') }, 'fast', 'swing');
         });
         obj.mouseleave(function() {
            var obj = $(this);
            var t = obj.find(target);
            t.stop().animate({ 'top': t.data('ori_top') }, 'fast', 'swing');
         });
      });
   },
   initHideButtons: function() {
      /* fix pour la hauteur, pour que lorsqu'on masque la colone, le footer ne remonte pas*/
      var wrap_body = $('#wrap-body');
      wrap_body.css('min-height', wrap_body.height());
      var container = wrap_body.children('.container')
      container.css('min-height', container.height());
      /* le petit bouton masquer fait remonter la zone de contenu */
      $('.hide-ctn').toggle(function() {
         var content = $(this).parent();
         content.data('padding-bottom', content.css('padding-bottom')).css('padding-bottom', '0px');
         $(this).removeClass('hide-ctn').addClass('show-ctn').text(app.Resources.Repository.Instance().get('Expand'));
         content.find('img').hide();
         $(this).siblings('div').slideUp(function() { $.fullsize.resize(true); });
      }, function() {
         var content = $(this).parent();
         content.css('padding-bottom', content.data('padding-bottom'));
         $(this).removeClass('show-ctn').addClass('hide-ctn').text(app.Resources.Repository.Instance().get('Hide'));
         $(this).siblings('div').slideDown(function() { $.fullsize.resize(true); content.find('img').show(); });
      });
   },
   initDevis: function(url) {
      var open = function() {
         var div = $('#devis-form');
         if (div.length >= 1)
            div.dialog('open');
         else {
            div = $('<div id="devis-form" class="popup-form"><iframe src="' + url + '" frameBorder="0" style="width:100%;height:437px;"></iframe></div>').appendTo('body').hide();
            div.dialog({
               'title': app.Resources.Repository.Instance().get('FormQuotationPageName'),
               'modal': true,
               'draggable': false,
               'resizable': false,
               'height': 500,
               'width': 620,
               'open': function() { this.style.width = '100%'; }
            });
         }
      };
      $('.link-devis').click(function() {
         open();
         return false;
      });
      if (Cossette.Url.params('quote') !== null || Cossette.Url.params('devis') !== null) {
         open();
      }
   },
   closeDevis: function() {
      $('#devis-form').dialog('close');
   },
   initReservation: function(externalUrl, url) {
      if (externalUrl != '') {

         // Replaced for GA modification 2011-07-18
         //  $('.link-reserve, .reserve').attr({ 'href': externalUrl, 'target': '_blank' });
         _gaq.push(function() {
            var pageTracker = _gat._getTrackerByName();
            var url = pageTracker._getLinkerUrl(externalUrl);
            if (url == '') { url = externalUrl; }
            $('.link-reserve, .reserve').attr({ 'href': url, 'target': '_blank' });
         });

      } else {
         $('.link-reserve, .reserve').click(function() {
            var div = $('#reserve-form');
            if (div.length >= 1)
               div.dialog('open');
            else {
               div = $('<div id="reserve-form" class="popup-form"><iframe src="' + url + '" frameBorder="0" style="width:100%;height:437px;"></iframe></div>').appendTo('body').hide();
               div.dialog({
                  'title': app.Resources.Repository.Instance().get('FormReservationPageName'),
                  'modal': true,
                  'draggable': false,
                  'resizable': false,
                  'height': 500,
                  'width': 620,
                  'open': function() { this.style.width = '100%'; }
               });
            }
            return false;
         });
      }
   },
   closeReservation: function() {
      $('#reserve-form').dialog('close');
   },
   initCorporateProgram: function(url) {
      var open = function() {
         var div = $('#corporate-form');
         if (div.length >= 1)
            div.dialog('open');
         else {
            div = $('<div id="corporate-form" class="popup-form"><iframe src="' + url + '" frameBorder="0" style="width:100%;height:437px;"></iframe></div>').appendTo('body').hide();
            div.dialog({
               'title': app.Resources.Repository.Instance().get('FormCorporateProgramPageName'),
               'modal': true,
               'draggable': false,
               'resizable': false,
               'height': 500,
               'width': 620,
               'open': function() { this.style.width = '100%'; }
            });
         }
      };

      $('.popup.corporate').click(function() {
         open();
         return false;
      });

      if (Cossette.Url.params('corporate') !== null)
         open();

   },
   closeCorporateProgram: function(url) {
      $('#corporate-form').dialog('close');
   },
   getLatestTweet: function(tweetContainer) {
      $.get(app.context.path + '/services/TwitterProxy.aspx', function(data, textStatus) {
         $(tweetContainer).find('.loader').hide();
         if (textStatus == 'success') {
            $(tweetContainer).find('.screen-name').text(data.UserScreenName);
            $(tweetContainer).find('.text').show().text(data.Text);
            $(tweetContainer).find('.date').text(data.HumanFriendlyTimeSpan);
         }
      });
   }
};

/* app.Resources
---------------------------------------------- */
app.Resources = app.Resources || {};

app.Resources.Repository = function() {
    this._rep = new Array();
};

app.Resources.Repository._instance = undefined;

app.Resources.Repository.Instance = function() {
    if (app.Resources.Repository._instance == undefined)
        app.Resources.Repository._instance = new app.Resources.Repository();

    return app.Resources.Repository._instance;
};

app.Resources.Repository.prototype.add = function(key, val) {
    this._rep[key] = val;
};

app.Resources.Repository.prototype.get = function(key) {
    return this._rep[key];
};
