2 site.js - Javascript containing the main site functions and effects.
5 Copyright: 2011 Cidadania S. Coop. Galega
6 Author: Oscar Carballal Prego <info@oscarcp.com>
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));
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));
38 function safeMethod(method) {
39 return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
42 if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
43 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
50 // Show the messages slowly so the user can notice it.
51 $("ul.messages").slideDown(2000);
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();
58 $("ul.messages").slideUp('slow');
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() {
73 $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
76 //Following events are applied to the trigger (Hover events for the trigger)
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"
85 function sitestart() {
88 // This function activates the datepicker sitewide
89 $.datepicker.setDefaults({dateFormat:'dd/mm/yy'});
90 $( ".hasDatePicker, #datepicker" ).datepicker();
93 $(document).ready(function(){
95 $(".alert-message").alert();