$(document).ready(function () {
	
	
	// FUNCTIONS
	
	
	jQuery.fn.exists = function () {
		return jQuery(this).length > 0;
	}
	
	jQuery.fn.preload = function () {
		this.each(function () {
			$('<img/>')[0].src = this;
		});
	}
	
	
	// VARIABLES
	
	
	var menu_fade_speed = 150;
	var menu_delay = 1500;
	
	
	// MENU
	
	
	var melbourne_over = false;
	var melbourne_timeout;
	
	$('.sub-menu').hide().removeClass('hidden');
	
	$('#melbourne-btn, #melbourne-menu li').bind('mouseover mousedown', function () {
		clearTimeout(melbourne_timeout);
		hide_perth();
		$('#melbourne-menu').fadeIn(menu_fade_speed);
		melbourne_over = true;
	});
	
	$('#melbourne-btn, #melbourne-menu li').bind('mouseout', function () {
		melbourne_over = false;
		melbourne_timeout = setTimeout(hide_melbourne, menu_delay);
	});
	
	hide_melbourne = function () {
		if (!melbourne_over) {
			$('#melbourne-menu').fadeOut(menu_fade_speed);
		}
	}
	
	var perth_over = false;
	var perth_timeout;
	
	$('.sub-menu').hide().removeClass('hidden');
	
	$('#perth-btn, #perth-menu li').bind('mouseover mousedown', function () {
		clearTimeout(perth_timeout);
		hide_melbourne();
		$('#perth-menu').fadeIn(menu_fade_speed);
		perth_over = true;
	});
	
	$('#perth-btn, #perth-menu li').bind('mouseout', function () {
		perth_over = false;
		perth_timeout = setTimeout(hide_perth, menu_delay);
	});
	
	hide_perth = function () {
		if (!perth_over) {
			$('#perth-menu').fadeOut(menu_fade_speed);
		}
	}
	
	$('#' + loc + '-btn a').not('.sub-menu a').addClass('selected');
	
	
	// SHORTLIST
	
	
	$('.shortlist').click(function () {
		if (shortlisted == 1) {
			$.post(base_url + '/inc/shortlist.php?model=' + model, function (total) {
				$('.shortlist').html('add to shortlist');
				$('.shortlist-number').html(total);
				shortlisted = false;
			});
		} else {
			$.post(base_url + '/inc/shortlist.php?model=' + model, function (total) {
				$('.shortlist').html('remove from shortlist');
				$('.shortlist-number').html(total);
				shortlisted = true;
			});
		}
	});
	
	
	// INTRO
	
	
	if ($('#intro').exists()) {
		
		logo_images = 0;
		intro_preload = 0;
		intro_loaded = false;
		intro_images = 0;
		repeat = 0;
		
		var swap = 'top';
		var position;
		var top;
		var height;
		var zero = 0;
		
		$('#intro-container, .intro-logo-grey').hide().removeClass('hidden');
		
		$([base_url + '/media/scene/scene-intro.png', base_url + '/media/scene/scene-intro-grey.png']).preload();
		
		function logo () {
			$('.intro-logo-grey').fadeTo(750, 0.99, function () {
				$('.intro-logo-white').removeClass('hidden');
				if (intro_preload >= 2) {
					setTimeout(intro, 1250);
				} else {
					setTimeout(logo, 250);
				}
			});
		}
		
		function intro () {
			$('.intro-logo-grey').fadeOut(250);
			$('#intro-container').fadeIn(1000, function () {
				setTimeout(intro_scroll, 1000);
			});
			
			height = $('#intro-top').height();
			$('#intro-bottom').css('top', height);
		}
		
		function intro_scroll () {			
			if (intro_images < total_intro_images) {
				setTimeout(intro_scroll, 250);
				return;
			}
			top = $('#intro').position().top;
			position = top + height + zero;
			
			if (position <= 0) {
				zero += height;
				repeat++;
				$('#intro-' + swap).css('top', $('#intro-' + swap).offset().top + zero + height + height - (repeat * 5));
				if (swap == 'top') {
					swap = 'bottom';
				} else {
					swap = 'top';
				}
			}			
			$('#intro').animate({top: '-=10px'}, 75, 'linear', intro_scroll);
		}
		
		$('.intro-logo').imageLoad(function () {
			logo_images++;
			if (logo_images >= 2) {
				setTimeout(logo, 500);
			}
		});
		
		$('.preload').imageLoad(function () {
			intro_preload++;
		});
		
		$('.intro-image').imageLoad(function () {
			intro_images++;
		});
	}
	
	
	// HOME
	
	
	var home_speed = 1500;
	var home_pause = 2500;
	var home_loaded = 0;
	
	if ($('#home').exists()) {
		
		$('.cycle').hide().removeClass('hidden');
		
		$('.preload').imageLoad(function () {
			home_loaded++;			
			
			if (home_loaded >= 3) {
				setTimeout(home_start, 500);
			}
		});
		
		function home_start () {
			$('.cycle').cycle({
				fx		: 'fade',
				speed 	: home_speed,
				timeout	: home_pause
			});
			
			$('.cycle').fadeIn(750);
		}
	}
	
	
	// MODEL-LIST
	
	
	$('.star').removeClass('hidden').hide();
	$('.shortlisted').show();
	$('.out-of-town').css('opacity', 0.3);
	
	var model_timeout;
	
	$('#model-list li').bind('mouseover mousedown', function () {
		$('#model-list li').not($(this)).find('.model-box img').fadeTo(250, 0.7);
		$(this).find('.out-of-town').css('opacity', 0.75);
		$(this).find('.model-box img').fadeTo(250, 1.0);
		$(this).find('a').addClass('selected');
		$(this).find('.star').show();
		clearTimeout(model_timeout);
	});
	
	$('#model-list li').bind('mouseout', function () {
		$(this).find('.out-of-town').css('opacity', 0.3);
		if ($(this).find('.star').attr('shortlisted') != 'true') {
			$(this).find('.star').hide();
		}
		$(this).find('a').removeClass('selected');
		model_timeout = setTimeout(show_models, 250);
	});
	
	show_models = function () {
		$('#model-list li').find('.model-box img').fadeTo(375, 1.0);
	}
	
	$('.star').click(function () {
		if ($(this).attr('shortlisted') == 'true') {
			$(this).removeClass('shortlisted');
			$(this).attr({
				shortlisted: 'false',
				title: 'Add ' + $(this).attr('name') + ' to Shortlist'
			});
		} else {
			$(this).addClass('shortlisted');
			$(this).attr({
				shortlisted: 'true',
				title: 'Remove ' + $(this).attr('name') + ' from Shortlist'
			});
		}
		$.post(base_url + '/inc/shortlist.php?model=' + $(this).attr('id'), function (total) {
			$('.shortlist-number').html(total);
		});
	});
	
	
	// SCROLL
	
	
	if ($('#image-strip').exists()) {
		$('#image-strip').css('width', total_width);
		$('#content-wide').jScrollPane({
			showArrows: false,
			horizontalDragMinWidth	: 148,
			horizontalDragMaxWidth	: 148,
			trackWidth				: 960
		});
	}
	
	if ($('#model-list, #blog').exists()) {
		$('#content').jScrollPane({
			showArrows				: false,
			verticalDragMinHeight	: 148,
			verticalDragMaxHeight	: 148
		});
	}
	
	
	// DISABLE IMAGE RIGHT CLICK
	
	
	$('img').bind('contextmenu', function(e) {
		return false;
	});
	
	
	// UNHIDE
	
	
	$('#container').removeClass('hidden');
	
});


$(window).bind('load', function () {
	
	
	// SCROLL
	
	
	if ($('#text').exists()) {
		$('#content').jScrollPane({
			showArrows				: false,
			verticalDragMinHeight	: 148,
			verticalDragMaxHeight	: 148
		});
	}
	
});
