/*
* Global namespace
*/
var aston = aston || {},
	addthis_config,
	addthis_share,
	a2a_config = a2a_config || {};


/*
* Safe console
*/
if (typeof console === 'undefined') { 
	console = { log: function(){} };
}


/*
* Events
* am:init - on init
* am:resize - on window resize
* am:theme(name) - on theme change
*/

(function($){

	var aston = this;
		
	// Master configuration
	aston.w = $(window);
	aston.plugins = {};
	
	aston.width = 0;
	aston.height = 0;
	aston.theme = 'medium';
	aston.ie = $('html').hasClass('ie6') || $('html').hasClass('ie7') || $('html').hasClass('ie8');
	aston.mobileWebkit = navigator.userAgent.match(/iPad/i)!==null || navigator.userAgent.match(/iPhone/i)!==null;
	
	// Internal variables
	var i,
		themes = {
			"small": {
				min: 0,
				max: 1099
			},
			"medium": {
				min: 1100,
				max: 1369
			}/*,
			"large": {
				min: 1370,
				max: 1699
			},
			"xlarge": {
				min: 1700,
				max: 999999999
			}*/
		};
	
	/**
		@private
		@name preInit
		@function
		@description Master page init
	*/
	function preInit() {
		aston.w.trigger("am.preInit");
		
		checkTheme();
		listeners();
		
		aston.w.resize(function() {
			aston.w.trigger('am.resize');
		});
	}
	
	/**
		@private
		@name postInit
		@function
		@description Executed after all plugins have been initialised
	*/
	function postInit() {
		aston.w.trigger("am.preInit");
		aston.w.trigger('am.resize');
	}
	
	
	/**
		@private
		@name listeners
		@function
		@description Register master event listeners
	*/
	function listeners() {		
		aston.w.bind('am.resize', function() {
			checkTheme();
		});
	}
	
	
	/**
		@private
		@name checkTheme
		@function
		@description Check if browser size triggers new theme
	*/
	function checkTheme() {
		var html = $('html'),
			width = aston.w.width();
			
		for (i in themes) {
			var theme = themes[i];
			
			if (width > theme.min && width < theme.max) {
				// if the theme has changed
				if (aston.theme !== i) {
				
					aston.theme = i;
				
					html.removeClass("small medium large xlarge").addClass(aston.theme);
					aston.w.trigger("am.theme", [aston.theme]);
				
					return;
				}
			}
		}
	}
	
	
	/**
		@public
		@name aston.themeFilename
		@function
		@description Parse an image filename and calculate 'theme' version
		@param {String} message
	*/
	aston.themeFilename = function(filename, $carousel) {
		if (debug || ($carousel && $carousel.is('.brochure'))) { return filename; }
		var image = filename.substr(0, filename.length - 4),
			extension = filename.substr(image.length),
			
			// only add suffix if we're not on xlarge
			themePart = aston.theme == 'xlarge' ? '' : '-' + aston.theme;
		
		// note: separator must be a dash (-) not underscore (_)
		return image + themePart + extension;
	};
	
	/**
	 	@public
		@name aston.redrawFix
		@function
		@description Fixes an issue with some elements in IE7 not being redrawn after a DOM change.
		If the DOM is updated and the page dimensions change as a result, these elements are not repainted in IE7.
		This fix forces a repaint of the elements.
	 	@param {[HTMLElement]} context Optional HTML DOM Element to use as target for redraw (defaults to body)
	 	@param {[Boolean]} override Boolean flag to run redraw fix regardless of browser type/version
	 */
	aston.redrawFix = function(redrawContext, override) {
		var context = redrawContext || document.documentElement;
		
		// only run for IE7 performance fix (for now)
		if (override === true || ( $.browser.msie && $.browser.version < 8 && $.browser.version > 6)) {
			context.className = (context.className || "" ) + " ie_redraw";
			context.className = context.className.replace( / ie_redraw$/, "" );
		}
	};
	
	
	/**
		@public
		@name aston.error
		@function
		@description Throw an application error
		@param {string} message
	*/
	aston.error = function(message) {
		aston.w.trigger("am.error", [message]);
		console.log("ERROR :: ", message);
	};
	
	/**
		@public
		@name aston.register
		@function
		@description Register a new plugin for this page
		@param {object} plugin
	*/
	aston.register = function(p) {
		if (aston.plugins[p.__id]) {
			aston.error('Plugin already registered ', p.__id);
		} 
		else {
			aston.plugins[p.__id] = $.extend(true, p, { __loaded: false });
		}
	};
	
	/**
		@public
		@name aston.ready
		@function
		@description Initialise all plugins
	*/
	aston.ready = function() { 
		// call master & plugin inits
		preInit();
		for (i in aston.plugins) {
			if (typeof aston.plugins[i].init === 'function') {
				aston.plugins[i].init();
				aston.plugins[i].__loaded = true;
			}
		}
		postInit();
	};
	
	// startup theme checker to avoid page jumping
	checkTheme();
}).call(aston, jQuery);
