﻿/* This file runs on every page, every time they are loaded. Do not remove it, it's a core file. */
/* Init - runs when document is ready. All functions which require the document to be ready should be called from here */
$(function() {
	$(".labeler").labeler();
	$(".afalc").afalc();
	$(".row").columns(".column");
	$("#content").moveClass("body");
	$("#content input, .dialog input").typeClass("input_type");
	$(".odd").oddEven();
	$(".tablestripe").oddEven({target:"tbody tr:not(td tr)"});
	initDatePicker();
	createTabs();
	$(".accordion").accordion({autoHeight: false, navigation: true,  active: ".ui-state-active", collapsible:true, multiple:true });
	$(".add_load_style").clickAndWait({animate:true});
	$(".clickandwait_save").clickAndWait({loadingValue:"Saving"});
	$(".clickandwait_confirming").clickAndWait({loadingValue:"Confirming"});
	$(".lightbox").lightBox({
	   imageLoading: '/images/lightbox/lightbox-ico-loading',
	   imageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
	   imageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
       imageBtnNext: '/images/lightbox/lightbox-btn-next.gif'			
	});
	$("#tweet_list").tweet({
		count: 4,
		username: "Futureversity",
		loading_text: "loading tweets...",
		refresh_interval: 15
    });

});






// document load 
$(window).load(function() {
	$("a[href^='http://'], a[href^='www.']").not(".page_tools a[href^='http://'], .page_tools a[href^='www.']").oinw();
});




// extend jquery ui accordion to allow multiple
// sections at once if the multiple option is true

$.extend($.ui.accordion.prototype.options,{multiple: false});
var _toggle = $.ui.accordion.prototype._toggle;
var _clickHandler = $.ui.accordion.prototype._clickHandler;
$.extend($.ui.accordion.prototype,{
	_toggle: function(toShow, toHide, data, clickedIsActive, down){
		if (this.options.collapsible && this.options.multiple && toShow.is(':visible')) {
			arguments[1] = arguments[0];
			arguments[3] = true;
		}
		else if (this.options.collapsible && this.options.multiple) {
			arguments[1] = $([]);			
		}
		_toggle.apply(this,arguments);
		this.active
			.removeClass( "ui-state-active ui-corner-top" )
			.addClass( "ui-state-default ui-corner-all" )
	},
	_clickHandler: function(event, target){
		if ($(target).next().is(':visible:not(:animated)')) {
			this.active = $(target);
		}
		_clickHandler.apply(this,arguments)
	}
});















/* tooltip */
function flashNotifyToolTip(hideDelay, effectDelay, message, target, className) {
	var delayTimer = null;
	var delayInSeconds = hideDelay * 1000;
	var effectDurationInSeconds = effectDelay * 1000;
	var tooltipHTMLString = "<div class=\"" + className +"\"><div class=\"" + className + "_inner\"><p>" + message +"</p></div></div>";
	var toolTipObject = $(tooltipHTMLString);
	$(target).append(toolTipObject);
	
	toolTipObject.css("opacity", 0).show().animate({
		opacity: 1
	}, effectDurationInSeconds, "swing", setTimeTooltipOut);
	
	function setTimeTooltipOut() {
		delayTimer = setTimeout(function() {
			toolTipObject.animate({
				opacity: 0
			}, effectDurationInSeconds, "swing", finishHide);
		}, delayInSeconds);
	}
	
	function finishHide() {
		toolTipObject.remove();
		clearTimeout(delayTimer);
	}
}

function initDatePicker() {
	// Datepickers for date input fields
	$("input.date").datepicker({dateFormat:"dd/mm/yy", selectOtherMonths:true});
	$("input.date_dropdown").datepicker({dateFormat:"dd/mm/yy", changeMonth:true, changeYear:true, selectOtherMonths:true, yearRange: '-30:+0'});
}
 
// Open an AJAX dialog
function openDialog(url,title,width,height) {
	// Width and height
	var w = 500;
	var h= 300;
	if(width) {
		w = width;	 
	}
	if(height) {
		h = height;	 
	}
	 // Reset the dialog
    $("#dialog").html('').dialog('destroy'); 
    // Set up dialog
    $("#dialog").dialog({
		width: w,
		height: h,
		modal: true,
		title: title,
		cache: false
    }); 
    // Fetch content
    $.ajax({
		type: "GET",
		url: url,
		data: "ajax=1",
		success: function(data){
			$("#dialog").html(data);
		}
    });
}

// Replace alrt boxes with a much nicer alternative
function ajaxAlert(msg) {
	if($('#alert').length == 0) {
		$("body").append('<div id="alert"></div>');	
	}
    $('#alert').dialog('destroy').html(msg).dialog({
		dialogClass: 'alert',
		width: 200,
		minHeight: 80,
		modal: true,
		cache: false,
		buttons: { "Ok": function() { $(this).dialog("close"); } }
    }); 
}




/* create tabs function - to allow for tabs to be called by other means */
function createTabs(){
	$(".tabs").tabs();
}
