// Namespace function
function namespace(ns) {
    ns = ns.split('.');
    var cur = window, i;
    while ( i = ns.shift() ) {
        if ( !cur[i] ) cur[i] = {};
        cur = cur[i];
    }
}
// Put all tictoc functions into the tictoc object like:
// quartz.test = function() { alert("Test"); }
namespace("tictoc");

// Setup JS events when the DOM is ready
$(document).ready(function(){	
	// Content area images
	$(".pagebody img[@align='left']").addClass("left");
	$(".pagebody img[@align='right']").addClass("right");
	
	// Popup links
	$("a.popup").each(tictoc.website.popup);
	
	// Admin links
	$("a.adminedit").click(tictoc.admin.edit)
		 
	// Hide Labels from particular fieldsets
	$("form.hide_labels").each(tictoc.website.hide_labels);
	
	$(".callout-centre .home_signpost").wrap("<div class='container'></div>");
	$(".callout-centre").addClass("callout-js");
	if ($(".callout-centre .home_signpost").length == 0) {
		$(".callout-centre img").wrap("<div class='container signpost'></div>");
	}
	
	// Little fix to catch any weirdness from the body editor
	$("span.callout-centre").each(function() { $(this).parent().css({ float: "left", width: "100%" }); });
});


// General website functions
tictoc.website = {
	// Hide labels, set text to targets value
	hide_labels: function() {
		$(this).find("label").each(function() {
			$('#' + this.htmlFor).val(this.innerHTML);
			$(this).hide();
			$('#' + this.htmlFor).click(tictoc.website.clearbox);
		})
	},

    // Clear default text in an input box
    clearbox: function() {
        if (!this.default_value) this.default_value = this.value;
    
        if (this.value == '') {
            this.value = this.default_value;
			this.select();
        } else if (this.value == this.default_value) {
            this.value = '';
        }
    },
    
    // Jump to URL
    jump_to_url: function(url) {
        if (url == "") return false;
	    location.href = "/" + url;
    },
    
    // Popup link
    popup: function() {
        this.target = "_blank";
        this.title =  this.title ? this.title += ". " : "";
        this.title += "Link opens in a new window."
    }
};


// Admin functions
tictoc.admin = {
    popup_width: 675,
    popup_height: 650,
    
    edit: function() {
        var win = window.open(this.href, "_adminedit","height=" + tictoc.admin.popup_height + ",width=" + tictoc.admin.popup_width + ",resizable=yes,dependent,scrollbars=yes");
	    win.focus();
	    return false;
    }
};
