var app = function() {
	
	var 
	
	//
	debug = false,
	
	// list scripts to load in top of module load them into the stack
	_stack = [],
	
	_load_module = function(module) {
		
		module = module.split(' ')[0];

		if ( $.inArray(module, _stack) === -1 ) {
			head.js('/js/modules/' + module + '.js', function() {
				_stack.push(module);
				(debug) ? console.log( _stack ) : null;
			});
		}
		
	};
	
	return {
		
		init: function(module) {
			// init globals
			this.globals.init();
			_load_module( $('#content').attr('class') );
		},
		
		globals: {
			
			init: function() {
				this.navigation();
				this.callout_cycle();
				this.image_cycle();
				this.dealer_login();
			}, // init
			
			navigation: function() {
				
				var top_level = $('li.top_level > a'),
						location = window.location.pathname + window.location.search,
						currentp = $('a[href="' + location + '"]');
				
				// open drop downs
				top_level.click(function() {
					$('#nav').find('a').removeClass('active');
					$(this).addClass('active').parent('li').siblings('li').find('ul:first').slideUp('fast');
					$(this).parent('li').find('ul:first').slideDown('fast');
					return false;
				});
				
				// show dropdown on page load
				if ( currentp.next('is:(.second_level)') ) {
					//currentp.next('ul').slideDown();
				}	
				else if ( currentp.parent().parent().parent().hasClass('top_level') ) {
					//console.log(currentp.parent().parent().show());
				}
				
			},
			
			tooltip: function() {
				var tooltip = $('a.tooltip');
				tooltip.qtip({});
			}, // tooltip
			
			zoom: function() {

			},
			
			callout_cycle: function() {
				var callouts = $('#callouts');
				if ( callouts.length > 0 ) {
					head.js('/js/plugins/jquery.cycle.lite.min.js', function() {
						callouts.find('li.cycle').cycle({
							timeout: 5000
						});
					});
				}
			}, // callout_cycle
			
			image_cycle: function() {
				var images = $('.image_cycle');
				if ( images.length > 0 ) {
					head.js('/js/plugins/jquery.cycle.lite.min.js', function() {
						images.cycle({
							timeout: 5000
						});
					});
				}
			}, // image_cycle
			
			dealer_login: function(){
				$('li.dealer_login').find('a:first').click(function() {
					$('#dealer_login').slideToggle();
					return false;
				});
				
				$('a.dealer_close').click(function() {
					$('#dealer_login').slideToggle();
					return false;
				});
			}, //dealer_login
			
			dimmer: function() {
				
				var popup = $('a.popup');
				
				if ( popup.length > 0 ) {
					head.js('/js/plugins/jquery.colorbox-min.js', function() {
						popup.each(function() {
							var options = { opacity: 0.75 };
							if ( $(this).is('#enlarge') ) {
								$.extend(options, {
									scalePhotos: true,
									maxHeight: 400
								});
							}
							if (this.id === 'contact_popup' || this.id === 'email_popup') {
								$.extend(options, {
									onOpen: function() {
										$('#cboxContent').addClass('defs');
									},
									onClosed: function() {
										$('#cboxContent').removeClass('defs');
									}
								});
							}
							$(this).colorbox(options);
						});
						
					});
				}
			
			}, // dimmer
			
			preload: function( selector ) {
				$(selector).preload();
			}
			
		} // globals
		
	};
	
}();

function printPopup(popupUrl) {
	popWidth = 840;
	popHeight = 472;
	popLeft = (screen.width - popWidth) / 4;
	popTop = (screen.height - popHeight) / 4;
	popup = window.open(popupUrl,'printPopup','width=' + popWidth + ',height=' + popHeight + ',left=' + popLeft + ',top=' + popTop + ',menubar=no,toolbar=no,directories=no,location=no,status=no,scrollbars=no,resizable=yes');
	popup.focus();
}

$('.email_friend_btn').live('click', function() {
	$.post($('#email_friend').attr('action'), $('#email_friend').serialize(), function(data) {
		$('#email_friend').find('h3').html($(data).text());
	});
	return false;
});

$('#contact_concierge #send').live('click', function() {
	$.post($('#contact_concierge').attr('action'), $('#contact_concierge').serialize(), function(data) {
		$('#contact_concierge').find('h3').html($(data).text());
	});
	return false;
});
