/* Application */

var Cal = function() {

	var defaults = {
		'screen1': '',
		'screen2': '',
		'earnings': '',
		'annual': '',
		'monthly': '',
		'apr': '',

		'submit_button': '',
		'reset_button': '',

		'interest': 0
	};

	var options = arguments[0] || {};

	for(k in defaults) {
		this[k] = typeof options[k] != 'undefined' ? options[k] : defaults[k];
	}

	this.initilise = function() {
		this.screen2.css('display', 'none');
		var that = this;

		this.submit_button.bind('click', function() {
			that.run();
		});

		this.reset_button.bind('click', function() {
			that.restart();
			return false;
		});
	};

	this.run = function() {
		var earnings = 0;
		var interest = 0;

		if(!(earnings = parseInt(this.earnings.val()))) {
			earnings = 0;
		}

		if(earnings > 15000) {
			interest = earnings - 15000;
		}
		
		this.screen1.toggle();
		this.screen2.toggle();

		var total = (interest/100) * 9;

		this.annual.html(this.rd(total));
		this.monthly.html(this.rd(total / 12));

		var rate = this.interest / 12;
		this.apr.html(this.rd(rate));

		return true;
	};

	this.rd = function(n){
		return n.toLocaleString().split(".")[0] + "." + n.toFixed(2).split(".")[1];
	};

	this.restart = function() {
		this.screen1.toggle();
		this.screen2.toggle();
		this.earnings.val(0);
	};
};

var Menu = {

	initilise: function(page) {

		var offset = 0;
		var height = 42;

		switch(page) {
			case 'events' : // magenta
				offset = (height * 0);
				break;
			case 'advice' : // blue
				offset = (height * 1);
				break;
			case 'links' : // orange
				offset = (height * 3);
				break;
			case 'forum' : // pink
				offset = (height * 4);
				break;
			case 'video' : // red
				offset = (height * 5);
				break;
			default: // green
				offset = (height * 2);
		}

		$('#mainmenu li a').css('backgroundPosition', '0px -' + offset + 'px');
		
		/*
		$('#mainmenu li a').css('backgroundPosition', '0px -' + offset + 'px').hover(function() {
			$(this).css('backgroundPosition', 'left bottom');
		}, function() {
			$(this).css('backgroundPosition', '0px -' + offset + 'px');
		});
		*/

		$('#mainmenu li.selected a').css('backgroundPosition', 'left bottom');

	}

};

var Popup = function() {

	var options = arguments[0] || {};
	var defaults = {
		width: 540,
		height: 350
	};
	for(key in defaults) {
		this[key] = options[key] ? options[key] : defaults[key];
	}

	this.open = function(content) {

		this.close();

		if(content == '') {
			return;
		}

		var popup_id = 'popup_container';
		var popup_overlay = 'popup_overlay';

		// binding
		var _self = this;

		// overlay
		var height = $(document).height();

		var styles = {
			'width': $(window).width() + 'px',
			'height': height + 'px',
			'top': 0 + 'px',
			'z-index': '1000',
			'opacity': '0.8'
		};

		var overlay = $('<div>').attr({'id': popup_overlay}).css(styles).bind('click', function() {
			_self.close();
		});
		$('body').append(overlay);

		// container
		var container = $('<div>').attr({'id': popup_id}).css({
			'z-index': '2000',
			'top': $(window).scrollTop() + 50 + 'px',
			'left': ($(document).width() / 2) - (this.width / 2) + 'px',
			'width': this.width + 'px',
			'height': this.height + 'px'
		});

		container.html(content);

		$('body').append(container);
	};

	this.close = function() {
		$('#popup_overlay').remove();
		$('#popup_container').remove();
	};
};

var Swf = function() {

	this.params = {};
	this.embed = {};

	this.addparam = function(key, value) {
		if(key == 'file') {
			this.params['movie'] = value;
			this.embed['src'] = value;
		} else {
			this.params[key] = value;
			this.embed[key] = value;
		}
	};

	var defaults = {
		'type': 'application/x-shockwave-flash',
		'allowscriptaccess': 'always',
		'allowfullscreen': 'true',
		'file': '',
		'width': '300',
		'height': '200',
		'flashvars': ''
	};

	var options = arguments[0] || {};

	for(k in defaults) {
		var value = (options[k]) ? options[k] : defaults[k];
		this.addparam(k, value);
	}

	this.write = function() {

		var video = '<object width="' + this.params.width + '" height="' + this.params.height + '">';

		// build params
		video += this._param(this.params);

		// build embed
		video += this._embed(this.embed);

		video += '</object>';

		return video;
	};

	this._param = function(params) {

		var data = '';

		for(k in params) {
			data += '<param name="' + k + '" value="' + params[k] + '"></param>';
		}

		return data;
	};

	this._embed = function(params) {

		var data = '<embed';

		for(k in params) {
			data += ' ' + k + '="' + params[k] + '"';
		}

		data += '></embed>';

		return data;
	};

};

