var core = function(){
  return {
		
	  init: function(){
			this.openOnReady.defer(1500, this);
			this.closeOnReady.defer(1500, this);
			this.fadeInOnReady.defer(2500, this);
			//this.pageLoaded.defer(500, this);
			//this.initMask();
			this.setHandlers();
			
				var theWindow   = $(window),
						$bg         = $("#bg"),
						aspectRatio = $bg.width() / $bg.height();
																																			
				function resizeBg() {
					if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
						$bg
							.removeClass()
							.addClass('bgheight');
					} else {
						$bg
							.removeClass()
							.addClass('bgwidth');
					}
				}
				
																																							
				theWindow.resize(function() {
					resizeBg();
				}).trigger("resize");
		},
		
		openOnReady: function(){
		  Ext.select('.open-onready').each(function(el){
        el.setHeight(el.child('.onready-guide').getHeight(), {duration: 1, easing: "easeOutStrong"});
			});
		},
		
		closeOnReady: function(){
		  Ext.select('.close-onready').each(function(el){
        el.setHeight(0, {duration: 2, easing: "easeOutStrong"});
			});
		},
		
		fadeInOnReady: function(){
		  Ext.select('.fadein-onready').fadeIn({duration: 1.5, easing: "easeOutStrong"});
		},
		
		setHandlers: function(){
			Ext.select('.scroll-to-top').on('click', function(){ scrollToElement(Ext.getBody(), 1.4);});
		}
		
	}
}();

Ext.onReady(function(){ core.init(); });

// utility functions
function getPageHeight(){ // could use Ext for this?
  if(typeof window.innerWidth != 'undefined'){
      return window.innerHeight;
  }
  // IE6+ in standards mode
  else if (typeof document.documentElement != 'undefined'
           && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
	  return document.documentElement.clientHeight;
  } else {
		return document.getElementsByTagName('body')[0].clientHeight;
	}
}

// dev function
function logToConsole(str){
	if(!IS_LIVE) return;
	var d = new Date();
	var time = d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + '.' + d.getMilliseconds();
	var html = '<div><span style="width:70px;">' + time + '</span><span> - ' + str + '</span></div>';
  Ext.get('console').insertHtml('afterBegin', html);
	//Ext.get('console').highlight();
}
function inspect(obj){
	if(!IS_LIVE) return;
  for(var i in obj){
	  logToConsole(i+': '+obj[i]);
  }
}
function scrollToElement(el, dur, easing, offset){
	dur = dur || 2.5;
  offset = offset || 0;
	easing = easing || 'easeOutStrong';
	var docBody = Ext.isWebkit || Ext.isChrome? Ext.getBody() : Ext.get(document.documentElement || document.body);
  docBody.scrollTo('top', el.getTop() + offset, {duration: dur, easing: 'easeOutStrong', stopFx: true});
}

