
if (window.welterlin) _welterlin = welterlin;

welterlin = {
	info : 'Welterlin Base JavaScript',
	log : function() {
		try {console.log.apply('',arguments)} catch(e) {}
	},
	scriptsLoc : '/scripts/modules/',
	mod : {
		'carousel' : {},
		'simplemodal' : {},
		'tooltip' : {},
		'validate' : {}
	},
	loaded : [],
	init : function() {

		welterlin.log('welterlin.global.init');

		// remove non-js message from DOM
		$('.enable-js').remove();
		
		// remove upgrade node from non IE6 browsers
		if (!$('html').is('.ie6')) $('.ie6-upgrade').remove();

		// for browsers that do not support SVG, replace with PNG
		if (!document.implementation.hasFeature('http://www.w3.org/TR/SVG11/feature#BasicStructure','1.1')) {
			$('div.homepage embed').css('display','none');
			$('div.homepage').append('<img src="/images/logo-fredericWelterlinLarge.png" width="290" height="290" alt="Cancer is the fourth astrological sign in the zodiac..." />');
		}

		// preload images
		(function(){
			var imagesToLoad = [
				'../css/images/bg-tooltip-left.png',
				'../css/images/bg-tooltip-right.png'
			];
			$('<div class="no-print" style="position:absolute;left:-9999px;top:-9999px;height:0;width:0;overflow:hidden;"/>').appendTo('body').html('<img src="'+imagesToLoad.join('"/><img src="')+'"/>');
		})();

		// add easeOutQuad to the easing options and make it the default option
		jQuery.extend(jQuery.easing, {
			def: 'easeOutQuad',
			easeOutQuad: function (x, t, b, c, d) {
				return -c *(t/=d)*(t-2) + b;
			}
		});

		// accessibility: [plugin] treat spacebar 'keypress' event the same as the mouse 'click' event
		$.fn.klik = function(f){
			return this.bind('click keypress', function(e) {
				if (!e.keyCode || e.keyCode == 32){
					return f.call(this, e);
				}
			});
		};

		// setup and, if needed, load modules & plugins from other files
		$.each(welterlin.mod, function(moduleName) {
			$.extend(welterlin.mod[moduleName], {
				className : '.mod-' + moduleName,
				load : function(m) {
					if($.inArray(moduleName, welterlin.loaded) < 0) {

						var headElem = document.getElementsByTagName('head')[0];         
						var newScript = document.createElement('script');
						newScript.type = 'text/javascript';
						newScript.src = welterlin.scriptsLoc + 'welterlin.' + moduleName.replace(/_/g,'-') + '.js';
						headElem.appendChild(newScript);

						(function(){
							if(!welterlin.mod[moduleName].init){
								setTimeout(arguments.callee, 30);
								return;
							}
							welterlin.mod[moduleName].init(m);
						})();

						welterlin.loaded.push(moduleName);
					}
				}
			});

			var module = $('.'+moduleName.replace(/_/g,'-'));
			if (module.size()) welterlin.mod[moduleName].load(module);
		});

		// show/hide label for inputs
		$.fn.inputHint = function() {
			return this.each(function(){
				var hint = $(this);
				var f = hint.attr('for');
				if (f) {
					var input = $('#' + f).data('input-hint',hint[0]);

					this.hide = function() {
						hint.css({left:-9999});
					};

					this.show = function() {
						if (input.val() == '') hint.css({left:26});
					};

					// handlers
					input.focus(this.hide);
					input.blur(this.show);

					if (input.val() != '') this.hide();
				}
			});
		};
		
		// apply show/hide label for inputs
		$('.input-hint').inputHint();

		// primay nav "on" state
		(function(){

			var curLoc;
			curLoc = document.location.toString();
			
			var navList = new Array;
			
				navList[0] = new Object; navList[0].id = 'mnAbout'; navList[0].path = '/about/';
				navList[1] = new Object; navList[1].id = 'mnPortfolio'; navList[1].path = '/portfolio/';
				navList[2] = new Object; navList[2].id = 'mnProjects'; navList[2].path = '/projects/';
				navList[3] = new Object; navList[3].id = 'mnResume'; navList[3].path = '/resume/';
				navList[4] = new Object; navList[4].id = 'mnGallery'; navList[4].path = '/gallery/';
				navList[5] = new Object; navList[5].id = 'mnContact'; navList[5].path = '/contact/';
				navList[6] = new Object; navList[6].id = ''; navList[6].path = ''; //catch misc files
			
			var setNavState = function() {
				if (!document.getElementById) return;
				var id;
				for (i=0; i<=navList.length; i++) {
					if (curLoc.indexOf(navList[i].path) > -1) {
						id = navList[i].id;
						break;
					}
				}
				if (document.getElementById(id)) {
					var o = document.getElementById(id);
					o.className = 'active';
				}
			}
			setNavState();

		})();
	}
};

// instantiate WELTERLIN object
welterlin.init();

