﻿/* 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});
	$(".add_load_style").clickAndWait({animate:true});
	$(".clickandwait_save").clickAndWait({loadingValue:"Saving"});
	$(".clickandwait_confirming").clickAndWait({loadingValue:"Confirming"});
	$(".lightbox").lightBox();
});

// document load 
$(window).load(function() {
	$("a[href^='http://'], a[href^='www.']").oinw();
});

/* 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();
}