// 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(){
	
	// Popup links
	$("a.popup").each(tictoc.website.popup);
	
	// Admin links
	$("#body a.adminedit").click(tictoc.admin.edit);
	
	if ($("#request_mediation").val() != "true") $("#mediation").addClass("hide");
	$("#toggle_mediation").click(tictoc.website.toggle_mediation);
	
	$("#add_party").click(tictoc.website.add_party);
});

// General website functions
tictoc.website = {
    
    // 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;
        } 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."
    },

	toggle_mediation: function() {
		$("#mediation").toggleClass("hide");
		$("#normal_request").toggleClass("hide");
		if ($("#request_mediation").val() != "true")
		{
			$("#request_mediation").val("true");
		}
		else
		{
			$("#request_mediation").val("");
		}
	},
	
	add_party: function() {
		var x = $("#mediation > .left_column > ul").length;
		
		var party_title = $.create('h3',{}, ["Party " + (x+1)]);
		
		var party_block = $.create(
			'ul', {}, [
				'li', {}, [
					'label', {"for":"request_party_"+x+"_name"}, ["Name"],
					'input', {"type":"text", "name":"request[party]["+x+"][name]", "id":"request_party_"+x+"_name", "class":"full"}, []
				],
				'li', {}, [
					'label', {"for":"request_party_"+x+"_company"}, ["Company"],
					'input', {"type":"text", "name":"request[party]["+x+"][company]", "id":"request_party_"+x+"_company", "class":"full"}, []
				],
				'li', {}, [
					'label', {"for":"request_party_"+x+"_phone"}, ["Phone"],
					'input', {"type":"text", "name":"request[party]["+x+"][phone]", "id":"request_party_"+x+"_phone", "class":"full"}, []
				],
				'li', {}, [
					'label', {"for":"request_party_"+x+"_adviser"}, ["Advisers firm (if any)"],
					'input', {"type":"text", "name":"request[party]["+x+"][adviser]", "id":"request_party_"+x+"_adviser", "class":"full"}, []
				],
			]			
		);
		
		$("#add_party").before(party_title);
		$("#add_party").before(party_block);
		return false;
	}
};


// 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;
    }
};
