/** Image Object **/
function Image(id)
{
	
	if(id == undefined)
	{
		return;
	}
	
	function loadImage(src)
	{		
		return $('<img />').attr('src', globalThemeLocation + '/img/' + src);
	}
	
	this.id = id;
	this.element = $('img#' + this.id);
	
	var extension = this.element.attr('src').split('.').pop();
	
	/** For some reason i had to put this hack in as IE6 HTC fix + jQuery seems to register this png as a gif ? **/
	if(this.id == 'registerNow')
	{
		extension = 'png';
	}	

	var id_name = this.id.split('_')[0];
	
	this.normal = loadImage(id_name + '_normal.' + extension);
	this.over = loadImage(id_name + '_over.' + extension);
	this.selected = loadImage(id_name + '_selected.' + extension);
}

Image.prototype.setNormalState = function()
{
	this.element.attr('src', ((this.id.search('selected') == -1) ? this.normal : this.selected).attr('src'));
}

Image.prototype.setOverState = function()
{
	if(this.id.search('selected') != -1)
	{
		return;
	}
	this.element.attr('src', this.over.attr('src'));
}

/** Global Vars **/

var globalDemoButtonSelected;
var globalDemoButtonSelectedImage;


var globalPreload = new Array
(
	'demoController_hide.png',
	'demoController_show.png'
);

var globalPreloaded = new Array();

var globalFeedbackSubmission = false;

var globalURLLogin = 'https://www.anzmoneymanager.com/apps/login.anz.do?_flowId=login&c=csit_key_0%3AmC6TU5YooImql3LT1887ukXDPhA%3D&l=_flowId:u';
var globalURLRegister = 'https://www.anzmoneymanager.com/apps/registration.anz.do?_flowId=registration&c=csit_key_0%3ABVxqWnJLxOgvv9tlh2JkObmrIxc%3D&l=_flowId:u';

var globalTwitter = 'http://twitter.com/anzmoneymanager';

/** Anonymous Functions **/

function forceStateSwitch(id, state, image)
{
	var delimiter = '_';
	var newID = id.split(delimiter).shift() + delimiter + state;
	
	$("img#" + id).attr('id', newID);
	image.id = newID;
	image.setNormalState();
}

function preload()
{
	for(var i = 0; i < globalPreload.length; i++)
	{
		globalPreloaded.push($('<img />').attr('src', globalThemeLocation + '/img/' + globalPreload[i]));
	}
}

function getTop(element)
{
	return Number(element.css('top').split('px').shift());
}

function showHideDemoContainer()
{
	var element_demoContainer = $('div.demoContainer');
	var visibility_demoContainer = element_demoContainer.css('visibility');

	var button_demoController;
	var top_modifier = 380;
	var demo_height = 420;
	var top_homeContainer;
	var element_homeContainer = $('div.homeContainer'); 
	var element_container = $('div.container1');
	var height_container;
	
	var element_swf = $('object#homeSWF');
	var height_swf;
	var height_original_swf = 420;
	
	if(visibility_demoContainer == 'visible' || visibility_demoContainer == 'inherit')
	{
		visibility_demoContainer = 'hidden';
		button_demoController = 1;
		top_homeContainer = getTop(element_homeContainer) - top_modifier;
		height_container = element_container.height() - top_modifier;
		height_swf = 0;
	}
	else
	{
		visibility_demoContainer = 'visible';
		button_demoController = 0;
		top_homeContainer = getTop(element_homeContainer) + top_modifier;
		height_container = element_container.height() + top_modifier;
		height_swf = height_original_swf;
	}
	
	element_demoContainer.css('visibility', visibility_demoContainer);
	element_homeContainer.css('top', top_homeContainer);
	
	element_swf.css('visibility', visibility_demoContainer);	
	element_swf.height(height_swf);
	
	element_container.height(height_container);
	
	$('img.demo_controller').attr('src', globalPreloaded[button_demoController].attr('src'));
}

function repositionRightMenu()
{
	var delimiter = 'px';
	var otherRight = $('div.otherRight');
	if(otherRight.attr('class'))
	{
		var otherLeft = $('div.otherLeft');
		otherRight.css('top',  getTop(otherRight) - $('div.otherLeft').height() + 20);
	}
}

function submitFeedback()
{
	if(globalFeedbackSubmission === true)
	{
		return;
	}
	
	var element_email = $('input#email');
	var value_email = element_email.val();
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	var element_message = $('span.feedbackFormMessage'); 
	
	if(regex.test(value_email) === false)
	{
		element_message.html('Error - invalid email address specified');
		return;
	}
	
	globalFeedbackSubmission = true;
	element_message.html('Sending feedback');
	
	var data = "name=" + $('input#name').val() + "&email=" + value_email + "&feedback=" + $('textarea#feedback').val();
	
	$.ajax
	({
		type: "POST",
		url: $("form#sendFeedback").attr("action"),
		data: data,
		success: submitFeedbackResponse
	});
	
	
}

function submitFeedbackResponse(response)
{
	globalFeedbackSubmission = false;
	var message;
	
	if(response.length == 0)
	{
		message = 'Feedback sent';
		clearFeedbackForm();
	}
	else
	{
		message = response;
	}
	$('span.feedbackFormMessage').html(message);
}

function clearFeedbackForm()
{
	$('input#email').val('');
	$('input#name').val('');
	$('textarea#feedback').val('');
}

function openPage(url)
{
	window.open(url);
}

function openPageRegister()
{
	openPage(globalURLRegister);
}

function resizeBottomContainer()
{
	
	var element_otherBottom = $('div.otherBottom');
	
	if(element_otherBottom.attr('class') == undefined)
	{
		return;
	}

	var element_otherLeft = $('div.otherLeft');
	var element_otherRight = $('div.otherRight');
	
	var height_otherLeft = element_otherLeft.height();
	var height_otherRight = element_otherRight.height();
	
	if(height_otherRight > height_otherLeft)
	{
		height_otherLeft = height_otherRight;
	}
	
	if(height_otherLeft > element_otherBottom.height())
	{
		element_otherBottom.height(height_otherLeft);
	}

}

function loadTwitterFeed()
{
	var element_twitter = $('p#twitterContent');
	if(element_twitter.attr('id') == undefined)
	{
		return;
	}

	$.ajax
	({
		type: "GET",
		url: globalThemeLocation + '/twitterFeed.php',
		data: 'themeLocation=' + globalThemeLocation,
		success: loadedTwitterFeed
	});
}

function loadedTwitterFeed(response)
{
	$('p#twitterContent').html(response);
}

function validateComments(authKey)
{
	var validAuthor = validateHasText($('input#author'));
	var validEmail = validateEmail($('input#email'));
	
	var elementAuthKey = $('input#authKey');
	
	var validAuthKey = validateHasText(elementAuthKey);
	if(validAuthKey)
	{
		validAuthKey = (hex_sha1(elementAuthKey.val()) == authKey) ? true : false;
	}
	
	if(validAuthKey === true)
	{
		setFormItemValid(elementAuthKey);
	}
	else
	{
		setFormItemError(elementAuthKey);
	}
	
	var validComment = validateHasText($('textarea#comment'));

	if(validAuthor === false || validEmail === false || validAuthKey === false || validComment === false)
	{
		$('td#commentsError').html('Error processing form');
		return false;
	}
	$('td#error').html('');
	return true;
}

function trim(value)
{
	return value.replace(/^\s+|\s+$/g, '');
}

function validateHasText(element)
{
	if(trim(element.val()).length == 0)
	{
		setFormItemError(element);
		return false;
	}
	setFormItemValid(element);
	return true;
}

function clearCommentForm()
{
	$('input#author').val('');
	$('input#email').val('');
	$('input#authKey').val('');
}

function validateEmail(element, message)
{
	if(validateHasText(element) === false)
	{
		return false;
	}
	var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(regex.test(element.val()))
	{
		setFormItemValid(element);
		return true;
	}
	setFormItemError(element);
	return false;
}

function setFormItemError(element)
{
	setFormItemColour(element, '#FF0000');
}

function setFormItemValid(element)
{
	setFormItemColour(element, '#b4bec0');
}

function setFormItemColour(element, colour)
{
	element.css('border-color', colour);
}

function disableRSSForIE6()
{
	if($.browser.msie && $.browser.version < 7)
	{
		var element_subscribe = $('div.subscribe');
		if(element_subscribe.attr('class') != undefined)
		{
			element_subscribe.css('visibility', 'hidden');
		}
	}
}

/** Document Ready Handler **/
$(document).ready
(
	function()
	{
		preload();
		var element_demoContainer = $('div.demoContainer');
		if(element_demoContainer.attr('class') != undefined)
		{
			if(document.cookie.indexOf(globalHomeCookie) != -1)
			{
				showHideDemoContainer();
			}
			else
			{
				document.cookie = globalHomeCookie + "=" + 1 +';expires=10/06/2030';
			}
		}
		
		$('img.rollover').each
		(
			function()
			{
				this.image = new Image(this.id);
				$(this).mouseover
				(
					function()
					{
						this.image.setOverState();
					}
				);
				
				$(this).mouseout
				(
					function()
					{
						this.image.setNormalState();
					}
				);
			}
		);
		
		$('img.demo').each
		(
			function()
			{
				if(this.id.search('selected') != -1)
				{
					globalDemoButtonSelected = this.id;
					globalDemoButtonSelectedImage = this.image;
				}
				
				$(this).click
				(
					function()
					{
						
						var split = this.id.split("_");
						var state = split[split.length - 1];
						if(state == 'selected')
						{
							return;
						}
						
						forceStateSwitch(globalDemoButtonSelected, 'normal', globalDemoButtonSelectedImage);
						forceStateSwitch(this.id, 'selected', this.image);
						
						globalDemoButtonSelected = this.id;
						globalDemoButtonSelectedImage = this.image;
						
					}
				);
			}
		);
		
		$('img.demo_controller').click
		(
			function()
			{
				showHideDemoContainer();
			}
		);	
		
		$('img#send').click
		(
			function()
			{
				submitFeedback();
			}
		);
		
		$('img#registerTop').click
		(
			function()
			{
				openPageRegister();
			}
		);
		
		$('img#logonTop').click
		(
			function()
			{
				openPage(globalURLLogin);
			}
		);
		
		$('img#registerNow').click
		(
			function()
			{
				openPageRegister();
			}
		);
		
		$('img.twitter').click
		(
			function()
			{
				openPage(globalTwitter);
			}
		);
		
		disableRSSForIE6();
		repositionRightMenu();
		clearFeedbackForm();
		resizeBottomContainer();
		loadTwitterFeed();		
	}
);