$(init);

function init() {
	$('#footer .item').click(gotoNav);
	$('#current .item').click(gotoNav);
	$('#extras .item').click(gotoPage);
	showAd();
	showBanner();
	document.domain = 'highwayj.com';
}

function initScrollPanes() {
	$('.scroll-pane').jScrollPane({scrollbarWidth:4});
}

function initDefaultValue(ele, val) {
	$(ele).focus(function() {
		if ($(ele).val() == val) {
			$(ele).val('');
		}
	});
	$(ele).blur(function() {
		if ($(ele).val() == '') {
			$(ele).val(val);
		}
	});
}

function showBanner() {
	$('.background').fadeTo('fast', 0.1, function() {
		$('#banner').fadeIn('slow');
	});
}

function showNav() {
	$('#nav').fadeIn('slow');
}

function showPic() {
	if ($('#pic').css('display')=='none') {
		$('#pic').show('slow');
	}
}

var currentPage = '';
function gotoNav(e) {
	var src = $(e.target).parent('.item');
	var dst = $('#current .item');

	var loc = src.find('img').attr('id');
	if (currentPage == loc) return;
	
	if (loc != dst.find('img').attr('id')) {
		var src_html = src.html();
		var dst_html = dst.html();
	
		src.fadeOut("slow",function(){
	  			src.html(dst_html);
		});
		dst.fadeOut("slow",function(){
			dst.html(src_html);
		});
	
		dst.fadeIn("slow");
		src.fadeIn("slow");
	}

	gotoPage(e);
}

function gotoPage(e) {
	var src = $(e.target).parent('.item');
	currentPage = src.find('img').attr('id');
	if (!currentPage) return;
	
	try {
		var content = $('#content');
		var details = $('#content #details');
		content.fadeOut('slow', function() {
			showPic();
			details.load(currentPage + '.php', {}, function() {
				content.fadeIn('slow');
				initScrollPanes();
			});
		});	
	} catch (ex) {
		location.href = location.href;
	}
}

var ads = new Array();
function addAd(name, page) {
	var obj = new Object();
	obj['name'] = name;
	obj['page'] = page;
	
	ads[ads.length] = obj;
}

function showAd(idx) {
	idx = idx || 0;
	var content = $('#ad');
	var title = $('#ad .title');
	var details = $('#ad .details');
	
	var item = ads[idx];
	var speed = 2000;
	content.fadeOut(speed, function() {
		title.html(item['name']);
		details.html('');
		details.load(item['page'], {}, function() {
			content.fadeIn(speed);
		});
	});	

	setTimeout('showAd(' + (idx + 1) % ads.length + ')', 10000);
}