(function(){

	if(!window['EMPYRE']) {
	    window['EMPYRE'] = {};
	}
	
	//
	warnIE = function(){
		$('div#ie-message').hide().slideDown(1500, 'easeOutQuart');
	}
	window['EMPYRE']['warnIE'] = warnIE;
	
	//
	function capitalize(string){
	    return string.charAt(0).toUpperCase() + string.slice(1);
	}
	
	//
	initMenu = function(){
		$('ul.sf-menu').superfish();		
	}
	window['EMPYRE']['initMenu'] = initMenu;
	
	//	
	initIndexSlides = function(){
		//Create Index Slides
		$('#ss-index').cycle({ 
		    fx:     $('#transition-style').val(),
		    speed:  $('#transition-duration').val(),
		    timeout: $('#pause-duration').val(),
		    easeIn: $('#easing-in').val(),
		    easeOut: $('#easing-out').val(),
		    pager:  'div#ss-index-nav div',
		    pagerAnchorBuilder: pagerFactory
		});
		
	    function pagerFactory(idx, slide) {
			var title = $('img',slide).attr('alt');
			return '<a href="#" title="'+title+'"></a>'; 
	    };
	}
	window['EMPYRE']['initIndexSlides'] = initIndexSlides;
	
	
	//
	initTwitter = function(){
		var ta = $('div#twitter-account').text();
		if(ta){
			$("div#ti-tweet").getTwitter({
				userName: ta,
				numTweets: $('#num-tweets').val(),
				loaderText: "Loading tweets...",
				slideIn: true,
				showHeading: false,
				headingText: "Latest Tweets",
				showProfileLink: false
			});
		} else {
			$('div#twitter-index, div#twitter-side').css('display', 'none');
		}
	}
	window['EMPYRE']['initTwitter'] = initTwitter;
	
	//
	initTips = function(){
	
		if($('#display-captions').val() == "true"){

			$('#ss-index-nav a[href][title]').qtip({
			   content: {
			      text: false // Use each elements title attribute
			   },
			   style: { 
			      width: 200,
			      padding: 5,
			      background: '#111111',
			      color: '#ffffff',
			      textAlign: 'center',
			      border: {
			         width: 7,
			         radius: 5,
			         color: '#111111'
			      },
			      tip: 'bottomMiddle',
			      name: 'dark' // Inherit the rest of the attributes from the preset dark style
			   },
			   position: {
			      corner: {
			         target: 'topMiddle',
			         tooltip: 'bottomMiddle'
			      }
			   }
		});
		
		}
		
			
	}
	window['EMPYRE']['initTips'] = initTips;
	
	//
	initLinkActions = function(){
		$('a.to-top').click(function(){
		     $('html, body').animate({scrollTop: '0px'}, 1500, 'easeInOutQuint');
		     return false;
		});
		
		$('a.back-2').click(function(){
			 history.go(-2);
		     return false;
		});
	}
	window['EMPYRE']['initLinkActions'] = initLinkActions;
	
	//
	initSearchForm = function(){
		$('input.search').val('Search...');
		$('input.search').focus(function(){
			if($(this).val() == 'Search...'){
				$(this).val('');
			}
		});
		$('input.search').blur(function(){
			if($(this).val() == ''){
				$(this).val('Search...');
			}
		});
	}
	window['EMPYRE']['initSearchForm'] = initSearchForm;
	
	
	//
	formValidation = new function(){
		
		this.init = function(){
			$('form.validate').submit(function(){
				EMPYRE.formValidation.manageErrors('reset');
				EMPYRE.formValidation.validateForm(this);
				if(errorsArr.length != 0){
					return false;
				} else {
					return true;
				}
			})
		},
		this.validateForm = function(form){
			$(form).find('p.input-error').hide();
			$(form).find(':input').each(function(){
				if ($(this).hasClass('v-not-empty')){
					EMPYRE.formValidation.validateEmpty(this, capitalize(this.name).replace("_", " ") + " can't be blank.");
				} else if ($(this).hasClass('v-email')) {
					EMPYRE.formValidation.validateEmail(this, "Please enter a valid email address.");
				}
			})
			errorsArr[0].focus();
		},
		this.manageErrors = function(action, e, msg){
			if (action != 'reset'){
				errorsArr.push(e);
				EMPYRE.formValidation.displayError(e, msg);
			} else {
				errorsArr = new Array();
			}
		},
		this.displayError = function(e, msg){
			$(e).prev('p.input-error:first').text(msg).show();
		},
		this.validateEmpty = function(e, msg){
			if(e.value.length == 0){
				EMPYRE.formValidation.manageErrors('add', e, msg);
				return false;
			}
			return true;
		},
		this.validateEmail = function(e, msg){
			var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
			if(e.value.match(emailExp)){
				return true;
			}else{
				EMPYRE.formValidation.manageErrors('add', e, msg);
				return false;
			}
		}
	}
	window['EMPYRE']['formValidation'] = formValidation;

})();

	

/* Apply functionality when DOM is ready -----------------------------------------------------
---------------------------------------------------------------------------------------------- */

$(document).ready(function(){
	
	EMPYRE.warnIE();
	EMPYRE.initMenu();
	EMPYRE.initIndexSlides();
	EMPYRE.initTwitter();
	EMPYRE.initTips();
	EMPYRE.initLinkActions();
	EMPYRE.initSearchForm();
	EMPYRE.formValidation.init();
	
}); 


