
var Vega = {
	
	locales: new Array(),
	
	tinyMceLocale: 'en',
	
	base_url: '',

	_site_url: '',

	_loader_shown: false, // A flag for the AJAX loader overlay state
	_loader_timeout_id: null, // The timeout ID of the loader function
	
	scripts: new Array(),

	restyle: function()
	{
		$('.jq_button').button();
		$('.jq_buttonset').buttonset();
	},

	loadMsgStack: function()
	{
		
	},

	reload: function()
	{
		window.location.reload();
	},

	inputPersist: function(selector, value)
	{
		/* header search box */
		$(selector).on('focusin',function() {
			$(selector).removeClass("dimmed");
			if($(this).val() == value){
				$(this).val('');
			}
		});
		$(selector).on('focusout',function() {
			if($(this).val() == ""){
				$(this).addClass("dimmed");
				$(this).val(value);
			}
		});
	},

	inputRadioBind: function(radio_name)
	{
		$(document).ready(function(){

			// On document init loop through the radio options
			$('input[name="'+radio_name+'"]').each(function(){

				// And bind the change event
				$(this).change(function(){

					// Disable all (since the onChange event does not fire when the radio button is unchecked)
					$('input[name="'+radio_name+'"]').each(function(){
						$('#'+$(this).attr('id')+'_bind input').attr('disabled','disabled');
						$('#'+$(this).attr('id')+'_bind select').attr('disabled','disabled');
					})

					if($(this).is(':checked'))
					{
						$('#'+$(this).attr('id')+'_bind input').removeAttr('disabled');
						$('#'+$(this).attr('id')+'_bind select').removeAttr('disabled');
					}
				})
			})
		})
	},

	inputBooleanBind: function(input_selector)
	{
		$(input_selector).change(function(){
			
			if($(this).attr('checked'))
			{
				$(input_selector + '_bind input').removeAttr('disabled');
				$(input_selector + '_bind select').removeAttr('disabled');
			}
			else
			{
				$(input_selector + '_bind input').attr('disabled', 'disabled');
				$(input_selector + '_bind select').attr('disabled', 'disabled');
			}
		})
	},

	site_url: function(uri)
	{
		return Vega._site_url+uri;
	},

	add_tax: function(price, tax_rate, decimals)
	{
		if(price)
		{
			wtax = price * (1 + parseFloat(tax_rate));
		}
		else
		{
			wtax = 0;
		}

		if(decimals === undefined)
		{
			decimals = 2;
		}
		
		return wtax.toFixed(decimals);
	},

	remove_tax: function(price, tax_rate, decimals)
	{
		if(price)
		{
			notax = parseFloat(price) / (1+parseFloat(tax_rate));
		}
		else
		{
			notax = 0;
		}
		
		if(decimals === undefined)
		{
			decimals = 2;
		}

		return notax.toFixed(decimals);
	},

	/*
	 * Queue the loader overlay to be shown, if expiration passes and the AJAX answer is still pending
	 */
	queue_loader: function(expiration)
	{
		Vega._loader_timeout_id = setTimeout(Vega.show_loader, expiration);
	},

	show_loader: function()
	{
		Vega._loader_timeout_id = null;
		Vega._loader_shown = true;
		$('#ajax_loader').fadeTo(300, 0.5);
	},

	hide_loader: function()
	{
		if(Vega._loader_timeout_id != null)
		{
			clearTimeout(Vega._loader_timeout_id);
		}

		if(Vega._loader_shown)
		{
			Vega._loader_shown = false;
			$('#ajax_loader').fadeOut(300);
		}
	},
	
	add_javascript: function(src)
	{
		if(Vega.scripts[src] == undefined)
		{
			$('body').append('<script type="text/javascript" src="'+Vega.base_url+ $.trim(src)+'"></script>');
			Vega.scripts[src] = true;
		}
	},
	
	parse_float: function(value, decimals)
	{
		value = parseFloat(value);
		
		if (isNaN(value))
		{
			return 0;
		}
		
		if (isInt(decimals))
		{
			return value.toFixed(decimals)
		}
		
		return value;
	}
}
