// 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");
	$(".pagebody .image-surround-left").each(function() {
		$(this).width($(this).children("img").width());
	});
	$(".pagebody .image-surround-right").each(function() {
		$(this).width($(this).children("img").width());
	});
	
	// Popup links
	$("a.popup").each(tictoc.website.popup);	
	$("#left_column a").each(function() {
		_this = this;
		val = $(_this).attr("href");
		test = /^http:\/\//
		if (test.exec(val)) {
			$(_this).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);
	
	// show/hide effect for staff profiles
	$("div.staff_profile").addClass("inactive");
	$("div.staff_profile h3").attr("title", "Click to find out more about this member of staff.");
	$("div.staff_profile h3").click(function() {
		$("div.staff_profile").addClass("inactive");
		$(this).parent(".staff_profile").toggleClass("inactive");
	});
	
	if ($.browser.msie && $.browser.version == "6.0") {
		$(".fauxhover").hover(function() {
			$(this).css("text-decoration", "underline");
		}, function() {
			$(this).css("text-decoration", "none");
		});
	}
	
	// homepage banner
	if ($("#home #banner").is("div")){
		$("#home #banner").flash({src: '/flash/banner_preloader.swf', width: 690, height: 247, wmode: 'opaque'}, {version: '8'});			
	} else {
		$("#banner").flash({src: '/flash/banner_preloader.swf', width: 500, height: 180, wmode: 'opaque'}, {version: '8'});	
	}
	
	// filtering form for case studies
	$("form#filter input[@type='submit']").hide();	
	$("#filter_building_type, #filter_policy_theme, #filter_meeting_type").change(function() {		
		if($(this).val() == "all") {
			location = $("form#filter #base_path").val();
		} else {
			location = $("form#filter #base_path").val()+"/tag/"+$(this).val();
		}
		return false;
	});
	$("#filter_location").change(function() {		
		location = $("form#filter #base_path").val()+"/location/"+$(this).val();
		return false;
	});
	
	
	$('#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();
	        }
	    );
	
	// Date pickers
	$.datePicker.setDateFormat("dmy", "/");
	$("input.date").datePicker({startDate:"01/01/2000"});
	$("input.date").change(tictoc.website.change_date);
});


// 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."
    },
    
    // Change date hook
    // This listens to changes in input.date fields to update the hidden rails selects
    change_date: function() {
        var div = $(this).parent().parent()[0]; // Parent div that contains the selects
        var date = $(this).val();
        date = date.replace(/[,\.:\-\|]/g,"/");
        date = date.replace(/[^0-9\/]/gi,"");
        date = date.split("/");
        
        if (date.length == 3) {
            $("input.day", div).val(date[0]);
            $("input.month", div).val(date[1]);
            $("input.year", div).val(date[2]);
        } else {
            $("input[@type=hidden]", div).val("");
        }
    }
};


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