$(document).ready(function(){

	$("a[href^='http://']").attr("rel", "nofollow").attr('target', '_blank');
	
	$("a.fancybox").fancybox({
		'zoomOpacity' : true,
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': false
	});

	$("form").each(function(){
		var $that = $(this);
		$that.submit(function(){
			$that.find("input[type='image'],input[type='submit']").attr("disabled", "disabled")
		});
	});

	$("#header .message").ajaxStart(function(){
		$(this).stop(true, true).text("Molim sačekajte.").fadeIn("fast")
	});

	$("#header .message").ajaxStop(function(){
		$(this).stop(true, true).fadeOut("normal")
	});

	
	$("a, span").hover(function(){
		$("#header .message").text($(this).attr("alt"));
		$("#header .message").stop(true, true).fadeIn("fast")
	}, function(){
		$("#header .message").text("");
		$("#header .message").stop(true, true).fadeOut("normal")
	});

	$("#header").data("up", 0);

	$("div.toggle-header").click(function () {
		var $header = $("#header")
		var $body_title = $("#body_title")
		if ($header.data("up") == 0) {
			$header.stop(true, true).animate({top: "-130px"}).data("up", 1);
			$body_title.stop(true, true).animate({marginTop: "10px"});
			$(this).css("background-position", "top center");
		} else {
			$header.stop(true, true).animate({top: "0px"}, "fast").data("up", 0);
			$body_title.stop(true, true).animate({marginTop: "110px"}, "fast");
			$(this).css("background-position", "bottom center");
		}
	});

    $('html').ajaxSend(function(event, xhr, settings) {
        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
        if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
            // Only send the token to relative URLs i.e. locally.
            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        }
});

});

