// 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()
{
	// Drop-down menu for IE
	if (document.all) $("#menu li").hoverClass ("sfHover");
	
	// 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)
	
	// Remove submit button from form
	// $("#search_form #search_field_submit").hide();

	// Hide Labels from particular fieldsets
	$("form.hide_labels").each(tictoc.website.hide_labels);
	
	$("#signin.hide").hide();
	$("#registration.hide").hide();	
	
	// homepage banner
	if ($("#home #banner").is("div")){
		$("#home #banner").empty();
		$("#home #banner").flash({
			src: '/flash/cloudscene2.swf', 
			width: 726, 
			height: 277}, 
			{version: '8'}
		);			
	}
	
	$("#registration_link").click( function() {
		$($(this).attr("href")).toggle();
		$("#signin").toggle();
		return false;
	});	

	// $("#body a.mov").quicktime();
	
	$('#body a[href*="youtube.com"]').flash(
	        { width: 425, height: 350, wmode: 'transparent' },
	        { version: 8 },
	        function(htmlOptions) {
	            htmlOptions.src = "http://www.youtube.com/v/" + this.href.match(/v=(.+)/)[1];
	            $(this).before($.fn.flash.transform(htmlOptions));
				$(this).remove();
	        }
	    );
	
	$('#body a[href*="vimeo.com"]').flash(
	        { width: 470, height: 353, quality:'best', allowfullscreen:'true', scale:'showAll' },
	        { version: 8 },
	        function(htmlOptions) {
	            htmlOptions.src = "http://vimeo.com/moogaloop.swf?clip_id=" + this.href.match(/(\d+)/)[1] + "&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=8FC744";
	            $(this).before($.fn.flash.transform(htmlOptions));
				$(this).remove();
	        }
	    );
	
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


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