/*
*	Author:			Johnathan Gould
*	For:			Munro Consulting
*	Created:		12 April 2010
*	Description:	The common javascript functions called throughout the site.
*/

$(function() {
	
	//
	//	The navigation buttons
	//
	$("#nav-contingencies").hover(
		function(){$('#nav-contingencies').toggleClass('nav-btn-contingencies-over');},
		function(){$('#nav-contingencies').toggleClass('nav-btn-contingencies-over');}
	);
	$("#nav-maintenance").hover(
		function(){$('#nav-maintenance').toggleClass('nav-btn-maintenance-over');},
		function(){$('#nav-maintenance').toggleClass('nav-btn-maintenance-over');}
	);
	$("#nav-income").hover(
		function(){$('#nav-income').toggleClass('nav-btn-income-over');},
		function(){$('#nav-income').toggleClass('nav-btn-income-over');}
	);
	$("#nav-inflation").hover(
		function(){$('#nav-inflation').toggleClass('nav-btn-inflation-over');},
		function(){$('#nav-inflation').toggleClass('nav-btn-inflation-over');}
	);
	$("#nav-lookup-graphs").hover(
		function(){$('#nav-lookup-graphs').toggleClass('nav-btn-lookup-graphs-over');},
		function(){$('#nav-lookup-graphs').toggleClass('nav-btn-lookup-graphs-over');}
	);

	//
	//	All the button types
	//
	$(".ui-button").button();
	
	$(".ui-button-home").button({
            icons: {primary: 'ui-icon-home'}
	});
	$(".ui-button-send").button({
            icons: {primary: 'ui-icon-mail-closed'}
	});
	$(".ui-button-process").button({
            icons: {primary: 'ui-icon-disk'}
	});
	$(".ui-button-login").button({
            icons: {primary: 'ui-icon-key'}
	});
	$(".ui-button-next").button({
            icons: {primary: 'ui-icon-circle-arrow-e'}
	});
	$(".ui-button-prev").button({
            icons: {primary: 'ui-icon-circle-arrow-w'}
	});
	$(".ui-button-go").button({
            icons: {primary: 'ui-icon-check'}
	});
	$(".ui-button-back").button({
            icons: {primary: 'ui-icon-arrowreturnthick-1-w'}
	});
	$(".ui-button-add").button({
            icons: {primary: 'ui-icon-plusthick'}
	});
	$(".ui-button-minus").button({
            icons: {primary: 'ui-icon-minusthick'}
	});
	$(".ui-button-remove").button({
            icons: {primary: 'ui-icon-circle-close'}
	});
	$(".ui-button-retire").button({
            icons: {primary: 'ui-icon-arrowthickstop-1-e'}
	});
		$(".ui-button-undo").button({
            icons: {primary: 'ui-icon-arrowreturnthick-1-w'}
	});
	$(".ui-button-down").button({
            icons: {primary: 'ui-icon-triangle-1-s'}
	});
	$(".ui-button-edit").button({
            icons: {primary: 'ui-icon-pencil'}
	});
	
	
	//
	//	Loading progression dialog
	//
	$('#loading').dialog({
		modal: true,
		autoOpen: false,
		closeOnEscape: false,
		show: 'fade',
		resizable: false,
		height: 90,
		width: 150,
		open: function(event, ui) { $(".ui-dialog-titlebar").hide(); }
	});
	
	//
	//	Show loading on ajax requests
	//
	$('#loading').ajaxStart(function() {
		$('#loading').dialog('open');
	}).ajaxStop(function() {
		$('#loading').dialog('close');
	}).ajaxError(function(e, xhr, settings, exception) {
		alert(exception);
	});
	
	//
	//	Defaults for all the date fields
	//
	$.datepicker.setDefaults({
		changeMonth: true,
		changeYear: true,
		showAnim: 'blind',
		minDate: '-80y',
		maxDate: '+0',
		dateFormat: 'dd/mm/yy',
		navigationAsDateFormat: true,
		yearRange: '100',
		gotoCurrent: true,
		showOtherMonths: true,
		selectOtherMonths: true
	});
});

// Called on button press to go to home page
function goHome () {
	window.location = "index.php";
}

// Called on button press to go to custom page
function goPage (page) {
	window.location = page;
}

// Enable send button when terms are accepted in online services
function toggleAccepted(x)
{
	var s=document.getElementById(x);
	
	if(s.disabled)
		s.disabled=false;
	else
		s.disabled=true;
}

// Removing a stream from an online service
function removeStream(num)
{
	document.getElementById("str_"+num+"_deleted").value = 1;
	document.getElementById("str_"+num).style.display = 'none';
}

// Removing a once-off from an online service
function removeOnceOff(num)
{
	document.getElementById("onceOff_"+num+"_deleted").value = 1;
	document.getElementById("onceOff_"+num).style.display = 'none';
}

function showLoadOnceOff() {
	document.getElementById("onceOffLoad").style.display = "block";
}

//
//	File uploading
//
document.observe("dom:loaded", function(){
	var currentTime = new Date()
	var milliseconds = currentTime.getTime()
	
	new AjaxUpload('#uploadBtn', {

		action: 'income/inc/upload.php?mil='+milliseconds,
		data : {
			'key1' : "This data won't",
			'key2' : "be send because",
			'key3' : "we will overwrite it"
		},
		onSubmit : function(file , ext){
			//if (ext && new RegExp('^(' + allowed.join('|') + ')$').test(ext)){
			if (ext && /^(doc|docx|pdf|tiff|tif|txt|xls|xlsx|jpg|jpeg|gif|png)$/.test(ext)){
				/* Setting data */
				this.setData({
					'key': 'This string will be send with the file'
				});
				
				document.getElementById('uploadText').innerHTML = ('Uploading ' + file);
				document.getElementById('uploadStatusbar').style.display = 'inline';
				document.getElementById('uploadButtonwrapper').style.display = 'none';
				document.getElementById('sendButton').disabled = true;
			} else {
				
				// extension is not allowed
				document.getElementById('uploadText').innerHTML = "<strong>ERROR:</strong> " + file + " not allowed (only doc, docx, pdf, tiff, tif, txt, xls, xlsx, jpg, jpeg, gif, png)!";
				document.getElementById('sendButton').disabled = false;
				// cancel upload
				return false;				
			}
	
		},
		onComplete : function(file){
			var divTag = document.createElement("div");
			divTag.id = "upload_text_inner";
			divTag.setAttribute("align","right");
			divTag.style.margin = "0px auto";
			divTag.innerHTML = "<a href='uploads/" + milliseconds + "_" + file.replace(/ /g, "-") + "' target='_blank'>"+file+"</a>";
			
			document.getElementById('uploadText').innerHTML = "Successfully uploaded " + file;
			document.getElementById('uploadOutcome').appendChild(divTag);
			document.getElementById('uploadFilelist').value += milliseconds + "_" + file.replace(/ /g, "-") + ";";
			document.getElementById('uploadStatusbar').style.display = 'none';
			document.getElementById('uploadButtonwrapper').style.display = 'inline';
			document.getElementById('sendButton').disabled = false;

		}		
	});
	
});