// Javascript general pour le Portail Santé Montréal
// Creation : 2009/05/20 par Hugo Laporte
// Agence de la sante et des services sociaux de Montreal


// Pour la petite carte qui change de CSSS -----------------------------------------------------------------

function petitecarte(chemin) {document.getElementById("petitecarte1").src = chemin;}

// Pour le drop down "Par catégorie" ------------------------------------------------------------------
function JumpToIt(list) {
    var newPage = list.options[list.selectedIndex].value
    if (newPage != "none") {
        location.href=newPage
    }
}

//Debut du onload JQuery -----------------------------------------------------------------------------------

jQuery(document).ready(function(){

  
// Commentaires  -----------------------------------------------------------------------------------
//  $("#c2596 .tx-comments-pi1").hide();
//  $('.vers-commentaires').click(function(){
//    $(".tx-comments-pi1").slideDown('slow');
//  });
  
// Accordéon  -----------------------------------------------------------------------------------  
  $( "#accordeon" ).accordion({ header: 'h3' });
  
// lightbox -----------------------------------------------------------------------------------
  jQuery('.lightbox').lightbox();
  $("#survey").trigger('click');

// organismes -----------------------------------------------------------------------------------  
  $organisme = $("#organisme-content").height();
  $("div#organisme-content").animate({
      height: "180px"
  }, "fast")
  $("div#display_button").click(function(){
    $("div#organisme-content").animate({
      height: $organisme + "px"
    }, "4000")
    $("div.panel_button").toggle();
    return false;
  });  
  
     $("div#hide_button").click(function(){
    $("div#organisme-content").animate({
      height: "180px"
    }, "1000");
    $("div.panel_button").toggle();
    return false;  
  
     });
     
//Galerie photo -----------------------------------------------------------------------------------
    $('#photos').galleryView({
      panel_width: 677,
      panel_height: 202,
      frame_width: 170,
      frame_height: 43,
      border: '0'
    });
    $('#mycarousel').jcarousel({
    scroll: 1
    });
     
     

//Rapporter une erreur -----------------------------------------------------------------------------------     
    
  var erreur = {
    container: null,
    init: function () {
      $("a.rapp").click(function (e) {
        e.preventDefault();  

        $("#rapp-erreur").modal({
          overlayId: 'rapp-erreur-overlay',
          containerId: 'rapp-erreur-ct',
          closeHTML: null,
          minHeight: 80,
          opacity: 65,
          position: ['0',],
          overlayClose: true,
          onOpen: erreur.open,
          onClose: erreur.close,
          zIndex: 9999
        });
      });
    },
    open: function (d) {
      var self = this;
      self.container = d.container[0];
      d.overlay.fadeIn('slow', function () {
        $("#rapp-erreur", self.container).show();
        var title = $("#rapp-erreur-tt", self.container);
        title.show();
        d.container.slideDown('slow', function () {
          setTimeout(function () {
            var h = $("#rapp-erreur-data", self.container).height()
              + title.height()
              + 20; // padding
            d.container.animate(
              {height: h},
              200,
              function () {
                $("div.close", self.container).show();
                $("#rapp-erreur-data", self.container).show();
              }
            );
          }, 300);
        });
      })
    },
    close: function (d) {
      var self = this; // this = SimpleModal object
      d.container.animate(
        {top:"-" + (d.container.height() + 20)},
        500,
        function () {
          self.close(); // or $.modal.close();
        }
      );
    }
  };
  
  var questionjour = {
    container: null,
    init: function () {
      $("#question-jour").click(function (e) {
        e.preventDefault();  

        $("#rep-question").modal({
          overlayId: 'rep-question-overlay',
          containerId: 'rep-question-ct',
          closeHTML: null,
          minHeight: 80,
          opacity: 65,
          position: ['0',],
          overlayClose: true,
          onOpen: questionjour.open,
          onClose: questionjour.close,
          zIndex: 9999
        });
      });
    },
    open: function (d) {
      var self = this;
      self.container = d.container[0];
      d.overlay.fadeIn('slow', function () {
        $("#rep-question", self.container).show();
        var title = $("#rep-question-tt", self.container);
        title.show();
        d.container.slideDown('slow', function () {
          setTimeout(function () {
            var h = $("#rep-question-data", self.container).height()
              + title.height()
              + 40; // padding
            d.container.animate(
              {height: h},
              200,
              function () {
                $("div.close", self.container).show();
                $("#rep-question-data", self.container).show();
              }
            );
          }, 300);
        });
      })
    },
    close: function (d) {
      var self = this; // this = SimpleModal object
      d.container.animate(
        {top:"-" + (d.container.height() + 20)},
        500,
        function () {
          self.close(); // or $.modal.close();
        }
      );
    }
  };
  
  var parnom = {
    container: null,
    init: function () {
      $("a.parnom").click(function (e) {
        e.preventDefault();  

        $("#rech-nom").modal({
          overlayId: 'rech-nom-overlay',
          containerId: 'rech-nom-ct',
          closeHTML: null,
          minHeight: 80,
          opacity: 65,
          position: ['0',],
          overlayClose: true,
          onOpen: parnom.open,
          onClose: parnom.close,
          zIndex: 9999
        });
      });
    },
    open: function (d) {
      var self = this;
      self.container = d.container[0];
      d.overlay.fadeIn('slow', function () {
        $("#rech-nom", self.container).show();
        var title = $("#rech-nom-tt", self.container);
        title.show();
        d.container.slideDown('slow', function () {
          setTimeout(function () {
            var h = $("#rech-nom-data", self.container).height()
              + title.height()
              + 20; // padding
            d.container.animate(
              {height: h},
              200,
              function () {
                $("div.close", self.container).show();
                $("#rech-nom-data", self.container).show();
              }
            );
          }, 300);
        });
      })
    },
    close: function (d) {
      var self = this; // this = SimpleModal object
      d.container.animate(
        {top:"-" + (d.container.height() + 20)},
        500,
        function () {
          self.close(); // or $.modal.close();
        }
      );
    }
  };
  
  var territoire = {
    container: null,
    init: function () {
      $("a.chan-terr").click(function (e) {
        e.preventDefault();  

        $("#territoire").modal({
          overlayId: 'territoire-overlay',
          containerId: 'territoire-ct',
          closeHTML: null,
          minHeight: 80,
          opacity: 65,
          position: ['0',],
          overlayClose: true,
          onOpen: territoire.open,
          onClose: territoire.close,
          zIndex: 9999
        });
      });
    },
    open: function (d) {
      var self = this;
      self.container = d.container[0];
      d.overlay.fadeIn('slow', function () {
        $("#territoire", self.container).show();
        var title = $("#territoire-tt", self.container);
        title.show();
        d.container.slideDown('slow', function () {
          setTimeout(function () {
            var h = $("#territoire-data", self.container).height()
              + title.height()
              + 20; // padding
            d.container.animate(
              {height: h},
              200,
              function () {
                $("div.close", self.container).show();
                $("#territoire-data", self.container).show();
              }
            );
          }, 300);
        });
      })
    },
    close: function (d) {
      var self = this; // this = SimpleModal object
      d.container.animate(
        {top:"-" + (d.container.height() + 20)},
        500,
        function () {
          self.close(); // or $.modal.close();
        }
      );
    }
  };

  erreur.init();
  parnom.init();
  territoire.init();
  questionjour.init();
  
  // Obtenir le ID de la page
  //$.urlParam = function(name){
  //  var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
  //  return results[1] || 0;
  //}
  //var idpage = $.urlParam('id');      
  
  
  $('#titre').val($('.actif').text());
  //$('#idpage').val(idpage);
  $('#contactform').submit(function(){
  
    var action = $(this).attr('action');
    
    $('#contactform #submit').attr('disabled','disabled').after('<img src="http://www.jardinspaquette.com/wp-content/themes/JardinsPaquette/assets/ajax-loader.gif" class="loader" />');
    
    $("#message").slideUp(750,function() {
    $('#message').hide();      
    
    $.post(action, {
      titre: $('#titre').val(),
      idpage: $('#idpage').val(),
      email: $('#email').val(),
      comments: $('#comments').val(),
      verify: $('#verify').val()
    },
      function(data){
        document.getElementById('message').innerHTML = data;
        $('#message').slideDown('slow');
        $('#contactform img.loader').fadeOut('fast',function(){$(this).remove()});
        $('#contactform #submit').attr('disabled','');
        if(data.match('success') != null) $('#contactform').slideUp('slow');
        
      }
    );
    
    });
    
    return false;
  
  });
  

// Tooltip -----------------------------------------------------------------------------------


        $('#tooltip').fadeOut(0);
        $('#tooltip').hide();
        $(".tooltip").mouseover(function(e) {               
                var tip = $(this).attr('alt');             
                $(this).attr('title','');               
                $("#tipBody").html(tip);              
                //$('#tooltip').fadeIn('10');
                $('#tooltip').show();               
        }).mousemove(function(e) {      
                $('#tooltip').css('top', e.pageY + 20 );
                $('#tooltip').css('left', e.pageX - 2 );               
        }).mouseout(function() {      
                //$('#tooltip').fadeOut('250');
                $('#tooltip').hide();
        });
         
// ---------- to top ------------------------------------------------------------------------------

  $('.csc-linkToTop a').click(function(){
    $('html, body').animate({scrollTop:0}, 'slow');
  });  
  
//Fin du onload JQuery -----------------------------------------------------------------------------------
});

// Twitter feed -----------------------------------------------------------------------------------
getTwitters('tweet', {
  id: 'santemontreal',  
  clearContents: false, // leave the original message in place
  count: 2,
  withFriends: true,
  ignoreReplies: false,
  newwindow: true,
  template: '%text% <a href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a>'
});

//Pour la taille du texte -----------------------------------------------------------------------------------

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

$(document).ready(function() {
      $("a.sizetexte").click(function(){
        $(".sizetexte").toggle();
      });
 });


function getActiveStyleSheet() {

  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {

  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;

}

function createCookie(name,value,days) {

  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";

  document.cookie = name+"="+value+expires+"; path=/";

}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// Tous les Onload de la pages doivent être insérés dans cette fonction
window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}


window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
