function updateOrientation(){
						switch(window.orientation){
							case 90:case -90:
							document.body.setAttribute('landscape', 'landscape');
							break;
							case 0: case 180:
							document.body.removeAttribute('landscape');
							break;
					}
			}
			
var UTIL = {
	fire : function(func,funcname, args){
		var namespace = core;

		funcname = (funcname === undefined) ? 'init' : funcname;
		if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
			namespace[func][funcname](args);
		}
	}, 

	loadEvents : function(){
		var bodyId = document.body.id;
		var isiPhone= navigator.userAgent.match(/iPhone/i) != null;
    	var isiPad = navigator.userAgent.match(/iPad/i) != null;
    	var isiTouch = navigator.userAgent.match(/iPod/i) != null;

	
	
    	if (isiPad || isiPhone || isiTouch)  
        {  
			vars.idevice=true;
			//PKNavigationView.prototype.setNavigationBarHidden();
		}
		else
			vars.idevice=false;
		// hit up common first.
		UTIL.fire('common');
		// do all the classes too.
		$.each(document.body.className.split(/\s+/),function(i,classnm){
			UTIL.fire(classnm);
			UTIL.fire(classnm,bodyId);
		});
		UTIL.fire('common','finalize');
	}
},

echo = function(txt, which){
	try{
		switch(true){
			case which == 'info':
				console.info(txt);
				break;
			case which == 'warn':
				console.warn(txt);
				break;
			case which == 'trace':
				console.trace();
				break;
			default:
				console.log(txt);
				break;
		}
	}
	catch(e){}
},

vars = {},
core = {
	common : {
		init : function(){
			echo('core -> common -> init','info');
		},
		finalize : function(){
			echo('core -> common -> finalize','info');
			$('#fade').fadeOut(500);
		}
	},
	has_quotes : {
		init : function(){
			var id  = $('body').attr('id'),
				flash = null;
			
			if( id == 'home' ){
				flash = homeflash;
			}
			else if( id == 'courtyard' ){
				flash = courtyardflash;
			}
			
			if( $.browser.msie && flash ){
				$('#quotes')
				.html( flash );
			}
			else{
				$("#quotes")
				.cycle({
				  fx:     'fade',
				  speed:  1000,
				  timeout:4000
				});
			}
		}
	},
	has_gallery : {
		init : function(){
			var imgs = $('#photos img'),
				fimg = imgs.eq(0).data('first',true),
				cur = 0,
				lstcls = null;
				
			imgs
			.addClass('nne')
			.onImagesLoad({
				itemCallback : function( el ){
					var dim = core.fn.getDims();
					var dem = core.fn.resize(this, dim[1], dim[0]);

					//vars.img
					/*.css({
						'width':dem.w,
						/*'height': dem.h,
						'marginRight':-dem.ml,
						'marginBottom':-dem.mt
					})*/
					var that = $(this);
					
					that
					.data('h', this.height)
					.data('w', this.width)
					.data('ready',true)
					.hide()
					.removeClass('nne');
					
					if( that.data('first') ){
						that.fadeIn();
					}
				}
			});
			
			$('#nav-gallery .next a')
			.unbind('click')
			.click(function(e){
				echo('hit');
				e.preventDefault();

				if (cur == (imgs.length-1)) {
					cur = -1;
				}
				
				checkHide(++cur == 0);
				
				imgs.filter(':visible')
				.fadeOut( 300,
				function(){
					imgs.eq(cur).fadeIn( 900 );
					checkClassed(imgs.eq(cur).attr('rel'));
				});
						
				return false;
			});
			$('#nav-gallery .prev a').unbind('click').click(function(e){
				e.preventDefault();

				if (cur == 0) {
					cur = imgs.length;
				}
				
				imgs.filter(':visible')
				.fadeOut( 900,
				function(){
					imgs.eq(--cur).fadeIn( 2000 );
					checkHide(cur == 0);
					checkClassed(imgs.eq(cur).attr('rel'));
				});
						
				return false;
			});
	
			function checkClassed(cls){
				echo( cls );
				if( typeof cls != 'undefined' ){
					$('#nav-gallery').removeClass(lstcls);
					lstcls = cls;
					$('#nav-gallery').addClass(lstcls);
				}
				else{
					$('#nav-gallery').removeClass(lstcls);
				}
			}
	
			function checkHide(hide){
				if( !hide )
					$("#quotes").css({'position':'absolute','left':'-999em','top':'-999em'});
				else
					setTimeout(function(){$("#quotes").css({'position':'',left:'',top:''});}, 1000)
			}
			
		}
	},
	spa : {
		init : function(){

			$('#snavCol').hide().css('left','-155px');
			$('#snavCol').oneTime(500,'showSpa',function(){
			$('#snavCol').animate({opacity:'show',left:"155"},{duration:500, easing:'linear'});});
			
			$('#snavCol a').not('.ex').click(function(e){
				e.preventDefault();
				$('<div id="extracontent" class="" style="display: none"></div>')
				.load('/spa/detail.php #detail')
				.appendTo("#bodyCol")
				.show(300, function(){
					Cufon.replace('.f',{hover: true});
					$('.close a').click(function(e){e.preventDefault(); $('#extracontent').remove();});
				});
			})
		}
	},
	home :{
		init : function(){
			echo('core -> home -> init','info');
			
			core.fn.presentImage(core.fn.randomHP());

			
			$('.primary-nav').hide();
			$('navCol').show();
			$('#bg-tier1').hide().css('left','-155px');
			
			$('#navCol a:not(.external):not(.main)')
			.click(function(e){
				e.preventDefault();
				var loc = $(this).attr('href');
				$('#bodyCol')
				.animate({
					opacity:'hide'
				},
				{
					duration:300,
					easing:'linear',
					complete:function(){
						window.location = loc;
					}
				}
				);
			});
			
			$('#navCol')
			.bind(
				'mouseenter click', 
				function(){
					animateMenu('in','#bg-tier1','.dummy-nav',".primary-nav");
					$('#navCol').stopTime('hideMenu');
				}
			);
			
			$('#navCol')
			.mouseleave(
				function(){
				
					$('#navCol')
						.oneTime(
							3000, 
							'hideMenu', 
							function(){echo('hideMenu');
								animateMenu('out','#bg-tier1','.dummy-nav',".primary-nav");
							}
						);
				}
			);
			
			/*$(window)
			.bind(
				'resize.image',
				function(){
					var dim = core.fn.getDims();
					var dem = core.fn.resize(this, dim[1], dim[0]);

					vars.img
					.css({
						'width':dem.w,
						'height': dem.h,
						'marginRight':-dem.ml,
						'marginBottom':-dem.mt
					})
				}
			);*/
			
			echo( $('#nav-menu a.main') );
			
			$('#nav-menu a.main')
			.click(
				function( e ){
					e.preventDefault();
				}
			);
		}
	},
	fn :{
		randomHP : function(){
			var totalHP = 3;
			var rndNum = Math.floor(Math.random() * totalHP)+1;
			var path = '/common/img-assets/home/greenwich_hotel-homepage-0'+rndNum+'.jpg';
			return path;
		},
		presentImage : function( src ){
			vars.img = $('<img src="'+src+'" class="nne">').appendTo('#photos')
			.onImagesLoad({
				itemCallback : function( el ){
					var dim = core.fn.getDims();
					var dem = core.fn.resize(this, dim[1], dim[0]);
					echo(dem);
					vars.img
					/*.css({
						'width':dem.w,
						/*'height': dem.h,
						'marginRight':-dem.ml,
						'marginBottom':-dem.mt
					})*/
					.data('h', this.height)
					.data('w', this.width);
					vars.img.hide().removeClass('nne');
					vars.img.fadeIn();
				}
			});
		},
		getDims : function(){
			vars.photo_w = $('#photos').width();
			vars.photo_h = $('#photos').height();
			return [vars.photo_w,vars.photo_h];
		},
		resize : function(img, maxh, maxw) {
			echo(' core -> fn -> resize :' + maxh +' ' +maxw);
			
						
			var width = ( $(img).data('w') || img.width ),
				height = ( $(img).data('h') || img.height );
			
			var r = {
				w : parseInt(width,10),
				h : parseInt(height,10)
			};
			/*
			var ratio = Math.min(maxw / width, maxh / height);
			
			if (height/width < ratio){
				if (height < maxh){
				  r.w = Math.round(width*(maxh/height));
				  r.h = maxh;
				}
			} else {
				if (width < maxw){
				  r.h = Math.round(height*(maxw/width));
				  r.w = maxw;
				}
			}
			if (width > height){
				if (width > maxw){
					r.w = parseInt(maxw,10);
					r.h = 'auto';
				}
				else{
					r.w = 'auto';
					r.h = parseInt(maxh,10);
				}
			}
			else{
				if (height > maxh){
					r.w = 'auto';
					r.h = parseInt(maxh,10);
				}
				else{
					r.w = parseInt(maxw,10);
					r.h = 'auto';
				}
			}
			
			var hr = height/maxh;
			var wr = width/maxw;
			
			if( hr > wr ){
				r.h = 'auto';
				r.w = '100%';
			}
			else{
				r.w = 'auto';
				r.h = '100%';
			}*/
			
			r.mt = 0;//Math.round((r.h-maxh)/2);
			r.ml = 0;//Math.round((r.w-maxw)/2);
			
			return r;
		}
	}
};


function animateMenu(dir, bg, menu, menu2,callback)
{
  switch(dir)
  {
    case 'in':
      $(bg)
      .animate
      (
        {left:0/*, opacity:0.8*/}
        ,{duration: 500 ,queue:false}
      );
      
      $(menu)
      .animate
      (
        {left: -155, opacity:'hide'}
        ,{duration: 300, easing:'linear', complete: function()
          { 
            if (menu2)
            {
              $(menu2)
              .animate
              (
                {left: 0, opacity:'show'}
                ,{duration: 500, easing:'linear'}
              );
            }
            if (callback)
            {
              callback;
            }
          }
        }
      );
    break;
    
    case 'out':
      if (menu2)
      {
        $(menu2)
        .animate
        (
          {left: -155, opacity:'hide'}
          ,{duration: 500, easing:'linear', complete: function()
          {
            $(bg)
            .animate
            (
              {left:-155/*, opacity:'hide'*/}
              ,{duration: 300 ,queue:false}
            );

            $(menu)
            .animate
            (
              {left: 0, opacity:'show'}
              ,{duration: 500, easing:'linear'}
            );
          }
        }
        );
      }
      else
      {
        $(bg)
        .animate
        (
          {left:-155/*, opacity:0*/}
          ,{duration: 300 ,queue:false}
        );

        $(menu)
        .animate
        (
          {left: 0, opacity:'show'}
          ,{duration: 500, easing:'linear'}
        );
      }
    break;
  }
}


var courtyardflash = ''+
	'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '+
	'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" '+
	'id="common/img-assets/quotes/courtyard" '+
	'align="middle" height="120" width="474">'+
	'<param name="allowScriptAccess" value="sameDomain">'+
	'<param name="allowFullScreen" value="false">'+
	'<param name="wmode" value="transparent">'+
	'<param name="movie" '+
	'value="../common/img-assets/quotes/courtyard.swf"><param '+
	'name="quality" value="high"><embed '+
	'src="../common/img-assets/quotes/courtyard.swf" '+
	'quality="high" '+
	'name="common/img-assets/quotes/courtyard" '+
	'allowscriptaccess="sameDomain" allowfullscreen="false" '+
	'type="application/x-shockwave-flash" '+
	'pluginspage="http://www.macromedia.com/go/getflashplayer" '+
	'align="middle" height="120" width="474" wmode="transparent">'+
	'</object>';

var drawingflash = ''+
	'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '+
	'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" '+
	'id="common/img-assets/quotes/drawing" '+
	'align="middle" height="120" width="474">'+
	'<param name="allowScriptAccess" value="sameDomain">'+
	'<param name="allowFullScreen" value="false">'+
	'<param name="wmode" value="transparent">'+
	'<param name="movie" '+
	'value="../common/img-assets/quotes/drawing.swf"><param '+
	'name="quality" value="high"><embed '+
	'src="../common/img-assets/quotes/drawing.swf" '+
	'quality="high" '+
	'name="common/img-assets/quotes/drawing" '+
	'allowscriptaccess="sameDomain" allowfullscreen="false" '+
	'type="application/x-shockwave-flash" '+
	'pluginspage="http://www.macromedia.com/go/getflashplayer" '+
	'align="middle" height="120" width="474" wmode="transparent">'+
	'</object>';

var homeflash = ''+
	'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '+
	'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" '+
	'id="common/img-assets/quotes/home" '+
	'align="middle" height="120" width="474">'+
	'<param name="allowScriptAccess" value="sameDomain">'+
	'<param name="allowFullScreen" value="false">'+
	'<param name="wmode" value="transparent">'+
	'<param name="movie" '+
	'value="../common/img-assets/quotes/home.swf"><param '+
	'name="quality" value="high"><embed '+
	'src="../common/img-assets/quotes/home.swf" '+
	'quality="high" '+
	'name="common/img-assets/quotes/home" '+
	'allowscriptaccess="sameDomain" allowfullscreen="false" '+
	'type="application/x-shockwave-flash" '+
	'pluginspage="http://www.macromedia.com/go/getflashplayer" '+
	'align="middle" height="120" width="474" wmode="transparent">'+
	'</object>';
	
$(document).ready(UTIL.loadEvents);
