Fix #31: Replace notify with gritter. Also solved a problem with the div
[e_cidadania.git] / src / e_cidadania / static_files / js / site.js
blob99ad753b31791187cea0b11f04f64fa150122661
1 /*
2     site.js - Javascript containing the main site functions and effects.
3                            
4     License: GPLv3
5     Copyright: 2011 Cidadania S. Coop. Galega
6     Author: Oscar Carballal Prego <info@oscarcp.com>
7 */
9 /* Cookie Reader (jQuery Cookie Plugin) */
10 $(document).ajaxSend(function(event, xhr, settings) {
11     function getCookie(name) {
12         var cookieValue = null;
13         if (document.cookie && document.cookie != '') {
14             var cookies = document.cookie.split(';');
15             for (var i = 0; i < cookies.length; i++) {
16                 var cookie = jQuery.trim(cookies[i]);
17                 // Does this cookie string begin with the name we want?
18                 if (cookie.substring(0, name.length + 1) == (name + '=')) {
19                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
20                     break;
21                 }
22             }
23         }
24         return cookieValue;
25     }
26     function sameOrigin(url) {
27         // url could be relative or scheme relative or absolute
28         var host = document.location.host; // host + port
29         var protocol = document.location.protocol;
30         var sr_origin = '//' + host;
31         var origin = protocol + sr_origin;
32         // Allow absolute or scheme relative URLs to same origin
33         return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
34             (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
35             // or any other URL that isn't scheme relative or absolute i.e relative.
36             !(/^(\/\/|http:|https:).*/.test(url));
37     }
38     function safeMethod(method) {
39         return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
40     }
42     if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
43         xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
44     }
45 });
47 /* MESSAGES */
49 function msg() {
50     // Show the messages slowly so the user can notice it.
51     $("ul.messages").slideDown(2000);
52     
53     // On click "X" hide everything
54     $(".closemsg").click(function (event) {
55                 // Prevent hyperlink to open the link with the default behaviour
56                 event.preventDefault();
57                 // Hide
58                 $("ul.messages").slideUp('slow');
59         });
62 function dropdown() {
63     // The followg code is for the dropdown menu
64         $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
66         $("ul.topnav li span").click(function() { //When trigger is clicked...
68                 //Following events are applied to the subnav itself (moving subnav up and down)
69                 $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
71                 $(this).parent().hover(function() {
72                 }, function(){
73                         $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
74                 });
76                 //Following events are applied to the trigger (Hover events for the trigger)
77                 }).hover(function() {
78                         $(this).addClass("subhover"); //On hover over, add class "subhover"
79                 }, function(){  //On Hover Out
80                         $(this).removeClass("subhover"); //On hover out, remove class "subhover"
81         });
85 function sitestart() {
86     dropdown();
87     msg();
88     // This function activates the datepicker sitewide
89             $.datepicker.setDefaults({dateFormat:'dd/mm/yy'});
90             $( ".hasDatePicker, #datepicker" ).datepicker();
93 $(document).ready(function(){
94     sitestart();
95     $(".alert-message").alert();
96 });