if (typeof console == 'undefined') {
	console = {
		log : function() {}
	};
}

var remy = {
	
	config : {
		'ajaxpathname' : 'ajax', //what our routes are prepended with to keep the SWF from picking them up
		'contentheight' : 631,
		'debug' : false,
		'dynamicheight' :	'700px',
		'flashheight' :	'525px',
		'horizon' : {
			'margincenter' : -314,
			'marginup' : 0,
			'topcenter' : '50%',
			'topup' : '0%'
		},
		'jQueryeasingdef' :	'easeInOutCubic',
		'loadervars' : {
			'opacity' : 0.75,
			'text' : 'Loading...'
		},
		'rolltime' : 500 //ms
	},
	
	init : function(p,a) { //called once only on page load

		jQuery.easing.def = remy.config.jQueryeasingdef;

		remy.loader('hide');
		
		$('.dynamic').live('click',function(event){
			event.preventDefault();
			var href = this.pathname.replace(/^\//,'');
			var newlink = remy.config.ajaxpathname + '/' + href;
			window.location = '/#/' + newlink;	
		//		SWFAddress.setValue(newlink);
		});

/*
		$('.open-flash').live('click',function(event){
			event.preventDefault();
			remy.roll('close');
			SWFAddress.setValue('home');			
		});
*/
		
		$('.totop').live('click', function(event){
			event.preventDefault();
			$.scrollTo(document.body, 500, {
				
			});
		});
		
		$(window).resize(function(){
			if(remy.iswindowsmall()) {
				remy.horizon('top',false);
			}
		});
		
		if(remy.config.debug == true) remy.debuginit();
		
	},

	roll : function(state) {
		if(remy.section == 'press') return;
		if(state == 'open') {

			$('#flashcontent-wrapper').stop().animate({
				'height' : '0px'
			},remy.config.rolltime, function(){
				$('#dynamic-wrapper').stop().animate({
					'height' : '100%'
				},remy.config.rolltime);			
			});			

			remy.horizon('top',true);

		} else if(state == 'close') {
		
			$('#dynamic-wrapper').stop().animate({
				'height' : '0px'
			},remy.config.rolltime);
			
			$('#flashcontent-wrapper').stop().animate({
				'height' : remy.config.flashheight
			},remy.config.rolltime);

			if(remy.iswindowsmall() == true) {
				remy.horizon('top',true);
			} else {
				remy.horizon('center',true);
			}
		} else {
			if(remy.isflashopen()) {
				remy.roll('open');
			} else {
				remy.roll('close');
			}
		}

	},
	
	horizon : function(position,animation,callback) {

		if(position == 'top') {
			if (animation != false) {
				$('.horizon-content').stop().animate({
					'marginTop' : remy.config.horizon.marginup,
					'top' : remy.config.horizon.topup
				}, remy.config.rolltime);		
			} else {
				$('.horizon-content').css({
					'marginTop' : remy.config.horizon.marginup,
					'top' : remy.config.horizon.topup
				});
				
			}
		} else if(position == 'center') {
			if (animation != false) {
				$('.horizon-content').stop().animate({
					'marginTop' : remy.config.horizon.margincenter,
					'top' : remy.config.horizon.topcenter
				}, remy.config.rolltime);		
			} else {
				$('.horizon-content').css({
					'marginTop' : remy.config.horizon.margincenter,
					'top' : remy.config.horizon.topcenter
				});
			}
		}

	},
	
	route : function() {
		var val = SWFAddress.getValue();
		var pts = SWFAddress.getPathNames();
		var ajaxval = val.split('/' + remy.config.ajaxpathname);
		var menustate = 'off';

		if(remy.isloggedin()) {
			menustate = 'on';
		}
		
		remy.menustate(menustate);
		if(remy.ishtmlpage()){

			var alturl = ajaxval[1];
			$('#dynamic-loader').fadeIn(600,function(){
				$.ajax({
					url: alturl,
					beforeSend: function(){
						remy.loader('show');				
					},
					success: function(data, textStatus) {	
						$('#dynamic-inner').html($(data).find('.content'));
						remy.roll('open');					
					},
					complete: function() {
						remy.loader('hide');
						$.scrollTo(document.body, 'fast');
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						
					}
					
				});			
			
			});			

			
		} else {
			remy.roll('close');
		}
		remy.navstate(pts);
		
	},
	
	loader : function(state) {
		$('#dynamic-loader').html(remy.config.loadervars.text);
		if(state == 'show') {
			$('#dynamic-loader').fadeIn(600);			
		} else {
			$('#dynamic-loader').fadeOut(600);
		}
	},
	
	ishtmlpage : function() {
		var paths = SWFAddress.getPathNames();
		if(paths[0] == remy.config.ajaxpathname) {
			return true;
		} else {
			return false;
		}
	},

	isflashopen : function() {
		if($('#flashcontent-wrapper').height() != 0) {		
			return true;
		} else {
			return false;
		}
	},
	
	isloggedin : function() {
		if(document.cookie.indexOf('exp_age-check') > -1) {
			return true;
		}
		return false;
	},
	
	iswindowsmall : function() {
		if($(window).height() < remy.config.contentheight) {
			return true;
		} else {
			return false;
		}
	
	},
	
	navstate : function(paths) {
		var path = paths[0];

		if(remy.ishtmlpage()) {
			path = paths[1];
			$('#nav a, #footer a').removeClass('nav-on');	
			$('#nav li.' + path + ' a').addClass('nav-on');
		}
	},
	
	menustate: function(state) {
		m = $('#nav');
		if(state == 'off') {
			m.addClass('menu-off');
		} else if(state == 'on') {
			m.removeClass('menu-off');			
		} else {
			m.toggleClass('menu-off');
		}
	},
	
	
	
};

function menustate(state) {
	m = $('#nav');
	if(state == 'off') {
		m.addClass('menu-off');
	} else if(state == 'on') {
		m.removeClass('menu-off');			
	} else {
		m.toggleClass('menu-off');
	}
	return true;
}


SWFAddress.onChange = function() {
	remy.route();
}
