var ada = {

    activate: function() {
        var url = window.location.pathname;
        var nav  = $('ul.nav');
        var path = url.split('/').slice(1,-1);
        function act(p) {
            /* seems some use the li and some the a */
            nav.find('li>a[href="/'+p+'"]').parent().addClass('active');
            nav.find('li>a[href="/'+p+'"]').addClass('active');
        }
        if (path.length)
            for(; path.length; path.pop())
                act(path.join('/')+'/');
        else
            act('');
        
        var sub = $('#sub-nav li');
        /* to make news work */
        if (url.match(/^\/$/)) {
            sub.find('a').addClass('active'); 
        }
        /* to make postits work */
        if (url.match(/^\/postits\/(new|edit|delete)\/$/)) {
            sub.find('a[href="/postits/"]').parent().removeClass('active');
        }
        /* to make events work */
        if (url.match(/^\/events\/(list|new)\/$/)) {
            sub.find('a[href="/events/"]').parent().removeClass('active'); 
        }
        /* to make pages */
        if (url.match(/^\/om-ada\/.+\/$/)) {
            sub.find('a[href="/om-ada/"]').parent().removeClass('active'); 
        }
    },
    
    parseVideo : function(url) {
        
        var provider;
        var id;
        if(/https?:\/\/(?:[a-zA_Z]{2,3}.)?(?:youtube\.com\/watch\?)/.test(url)) {
            provider = "http://www.youtube.com/v/";
            id = /https?:\/\/(?:[a-zA_Z]{2,3}.)?(?:youtube\.com\/watch\?)((?:[\w\d\-\_\=]+&amp;(?:amp;)?)*v(?:&lt;[A-Z]+&gt;)?=([0-9a-zA-Z\-\_]+))/i.exec(url)[2];
        }
        else {
            provider = "http://vimeo.com/moogaloop.swf?clip_id=";
            id = url.match(/https?:\/\/(?:[a-zA_Z]{2,3}.)?vimeo.com\/(\d*)/)[1];
        }
        swfobject.embedSWF(provider + id,"art-video", "467", "320", "9.0.0");
    },

    init: function() {
        ada.activate();
        
        $('a').filter(function() {
            return /http\:\/\//.test($(this).attr('href'));
        }).attr('target','_blank');
        
        var test = $("#follow-us .title");
       
        $("#follow-us a").hover(
          function (e) {
            test.stop().removeAttr('style').show();
            test.text($(this).attr("title"));
            e.preventDefault();
          },
          function () {
            test.fadeOut("fast",function(){test.text("" );})
          }
        );
        
        $('#postits, body.category #primary,#gallery-container').css('visibility','hidden');
        
        var cat = $('body.category #primary');
        $(window).bind('load', function() {
            $('#postits').masonry().css('visibility','visible');
            if(cat.children().length > 1) {
                cat.masonry().css('visibility','visible');
            }
            $('#gallery-container').masonry().css('visibility','visible');
            $('#footer-categories').masonry();
        });
        
        $("#keyword").labelify({ text: "label" });
        $("#email").labelify({ text: "label" });
        $("#postits .postit").click(function() {
            window.location=$(this).find("a").attr("href"); return false;
    	});	
    	$.extend(DateInput.DEFAULT_OPTS, {
          stringToDate: function(string) {
            var matches;
            if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
              return new Date(matches[1], matches[2] - 1, matches[3]);
            } else {
              return null;
            };
          },
          dateToString: function(date) {
            var month = (date.getMonth() + 1).toString();
            var dom = date.getDate().toString();
            if (month.length == 1) month = "0" + month;
            if (dom.length == 1) dom = "0" + dom;
            return date.getFullYear() + "-" + month + "-" + dom;
          }
        });
        $("#id_date").date_input();
        
        // Subscribe box
        var subscribe = $("#subscribe");

        
        if(readCookie('subscribe') == "hidden") {
            
            subscribe.hide();
        }
        subscribe.find(".close").click(function(e) {
            e.preventDefault();
            subscribe.slideUp();
            createCookie('subscribe','hidden',365)
        });
    }
}

// Cookie helper functions by PKK / http://www.quirksmode.org/js/cookies.html
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 var 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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

