var core = function(){
  return {
		
	  init: function(){
			this.openOnReady.defer(1500, this);
			this.closeOnReady.defer(1500, this);
			this.fadeInOnReady.defer(2500, this);
			// TODO: tidy this up
			Ext.select('.onclick-toggle').on('click', function(e,t){ core.contentToggle({element:Ext.get(this).next('.toggle-content-wrapper').child('.toggle-content')},null,true);});
			this.initHasHelpContent();
			//this.pageLoaded.defer(500, this);
			//this.initMask();
			this.setHandlers();
		},
		
		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"});
		},
		
		initHasHelpContent: function(){
			this.helpEl = Ext.get('help-display');
			if(!this.helpEl) return;
		  Ext.select('.has-help-content').hover(this.showHelp, this.hideHelp, this);
		},
		
		showHelp: function(e, t){
			if(this.helpShowing == true) return;
			this.helpShowing = true;
		  var element = e.getTarget('.has-help-content', null, true);
			var helpContent =  element.child('.help-content').dom.innerHTML;
			var mousePos = e.getXY();
			var l = mousePos[0] - 225; //element.getLeft() - 215;
			var t = mousePos[1] + 15; //element.getTop() + 30;
			this.helpEl.setLeft(l).setTop(t);
			this.helpEl.update(helpContent);
			this.helpEl.fadeIn({duration:.5, stopFx: true});
		},
		
		hideHelp: function(e, t){
			this.helpShowing = false;
		  this.helpEl.fadeOut({duration:.25, stopFx: true});
		},
		
		pageLoaded: function(){
		  //Ext.get('load').fadeOut({duration: .25, remove: true, callback: this.showSite});
		},
		
		contentToggle: function(args, data, toggleOnly){
			var deferTime = 1;
			var el = Ext.get(args.element);

			if(el.loaded != true && !toggleOnly){
				deferTime = 500;
			  //if(args.populate == true){
				el.update('');
			  for(var i=0;i<data.length;i++){
				  args.template.append(el, data[i]);
				}
			  //}
			  el.loaded = true;
			}
			
			
			var parent = el.parent('.toggle-content-wrapper');
			var clickElement = parent.prev('.onclick-load')? parent.prev('.onclick-load') : parent.prev('.onclick-toggle');
			if(clickElement.hasClass('open')){
			  parent.setHeight(1, {duration: .9, easing: "backIn"});
				clickElement.removeClass('open');
				el.removeClass('open');
			} else {
				parent.setHeight(el.getHeight()+10, {duration: .9, easing: "backIn"});
				clickElement.addClass('open');
				el.addClass('open');
				
				(function(el){
			    el.fadeIn({duration:.25, stopFx: true});
			  }).defer(deferTime, this, [el]);
				(function(parent){
			    parent.setStyle('background-image', 'none');
			  }).defer(deferTime, this, [parent]);
			}
			
			
		},
		
		initMask: function(){
			/*this.mask = Ext.get('mask');
			this.mask.select('.mask-close').each(function(el){
        el.on('click', this.hideMask, this);
			}, this);*/
		},
		
		showMask: function(){
		  //this.mask.fadeIn({duration: .2, endOpacity: .98});
		},
		
		hideMask: function(dur){
			//this.mask.select('.mask-content').each(function(el){ el.dom.style.display = 'none'; });
		  //this.mask.fadeOut({duration: .1, endOpacity: 0, remove: false, useDisplay: true });
		},
		
		setHandlers: function(){
			Ext.select('.scroll-to-top').on('click', function(){ scrollToElement(Ext.getBody(), 1.4);});
		},
		
		ajaxLoadContentToggle: function(obj){ // el, method, args (obj), template, element
		  var el = Ext.get(obj.element);
		  if(el.loaded==true){
				this.contentToggle({element: obj.element}, null);
			  return;
			}
			el.fadeOut({duration:.2, stopFx: true});
			el.parent('.toggle-content-wrapper').setHeight(50, {duration: .2});
			var params = {
				url: obj.url,
				method: obj.method
			};
			for(var i in obj.args){
			  params[i] = obj.args[i];
			}
		  obj.args.populate = true;
		  this.ajax(params, this.contentToggle, this, {element: obj.element, template: obj.template});
		},
		
		ajax: function(params, callback, scope, args){
			if(!params.language) params.language = LANG;
			var url = params.url;
			var config = {
				 url: '/' + LANG + url,
				 success: function(response){
					 var obj = Ext.decode(response.responseText);
					 if(obj.reload == true){
						 //alert('reload to: '+obj.location);
						 window.location = obj.location;
						 return;
					 }
					 callback.call(scope, args, obj);
				 },
				 failure: this.ajaxFailure,
				 params: params
			};
		  Ext.Ajax.request(config);
		},
		
		ajaxFailure: function(){
		
		}
		
	}
}();

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){
	dur = dur || 2.5;
	var docBody = Ext.isWebkit || Ext.isChrome? Ext.getBody() : Ext.get(document.documentElement || document.body);
  docBody.scrollTo('top', el.getTop(), {duration: dur, easing: 'easeOutStrong'});
}
function fadeInOnComplete(el, img){
  if(img.complete == true){
	  el.fadeIn({duration: 1, stopFx: true});
  } else {
		fadeInOnComplete.defer(250, null, [el, img]);
	}
}


