$(function(){

	var featureTimer = null;
	var activeFeatureIndex = -1;
	var lastFeatureIndex = -1;
	var isWindowReady = true;
	
	resize();
	
	$.get('./data.xml?' + Math.random(), function(data){
		// Get features
		var features = $(data).find('features feature');
		features.each(function(){
			// Create feature HTML
			html = '';
			html += '<div class="feature">';
			html += '<div>';
			html += '<div class="header"><img src="' + $(this).find('image').text() + '" alt="" width="294" height="60" /></div>';
			html += '<div class="content" style="display: none;">' + $(this).find('content').text() + '</div>';
			html += '<div class="wrapper">&nbsp;</div>';
			html += '</div>';
			html += '</div>';
			$('#carousel').append(html);
		});
		carousel.init();
		// Get page
		var page = $(data).find('page[filename="' + filename + '"]');
		// Get main content
		var content = page.find('content').text();
		// Get box content
		var box = page.find('box').text();
		// Insert box content into main content on Contact page
		if (filename == 'contact.html') {
			content += '<div class="box boxWide">';
			content += '<div class="header">&nbsp;</div>';
			content += '<div class="content">';
			content += box;
			content += '</div>';
			content += '<div class="footer">&nbsp;</div>';
			content += '</div>	';	
		}
		if (filename == 'index.html') {
			$('#content').html(content);
		} else {
			$('#copy .content').html(content);
			$('div.box .content').html(box);
		}
		if (typeof loadIe7Js == 'function') {
			loadIe7Js();
		}
	});
					
	$('#nav img:not(.on)').bind('mouseover', function(){
		this.src = this.src.replace(/(.*)\.(jpg|gif|png)$/i, "$1-over.$2");
	});
	$('#nav img:not(.on)').bind('mouseout', function(){
		this.src = this.src.replace(/(.*)-over\.(jpg|gif|png)$/i, "$1.$2");
	});
	$('#copy .scrollUp').bind('click', function(){
		$('#copy .content').scrollTo('-=60px', 500, {easing: 'easeInOutQuad'});
	});			
	$('#copy .scrollDown').bind('click', function(){
		$('#copy .content').scrollTo('+=60px', 500, {easing: 'easeInOutQuad'});
	});	
	$(window).bind('resize', resize);
	function resize(){
		
		var w = $(window).width();
		var n = $(window).height() - 720;
		/*
		if (w < 1088) {
			isWindowReady = false;
			$('html, body').animate({scrollLeft: (1088 - w)/2}, 500, function(){
				isWindowReady = true;
			});
		}	
		*/
		if (n < 60) {
			$('#master').css('margin-top', -36);
		} else {
			$('#master').css('margin-top', 0);
		}
	}
	$('a[href="http://rally.submit4jobs.com/"]').bind('click', function(){
		window.open('http://rally.submit4jobs.com/','dialog','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=625,height=800,screenX=100,screenY=100,top=100,left=100');
		return false;
	});
});

var carousel = {
	isReady: true,
	init: function(){
		$('#carousel .feature .wrapper').bind('mouseover', function(event){
			var item = $(this).parent('div');
			item.addClass('active');
			item.find('.content').fadeIn();
			return false;
		});				
		$('#carousel .feature .wrapper').bind('mouseout', function(event){
			var item = $(this).parent('div');
			item.removeClass('active');
			item.find('.content').fadeOut();
		});
		$('#banner .previous').bind('click', function(){
			carousel.scrollLeft();
			return false;
		});
		$('#banner .next').bind('click', function(){
			carousel.scrollRight();
			return false;
		});	
	},	
	scrollRight: function(){
		if (this.isReady == true) {
			this.isReady = false;
			$('#carousel').animate({left: -590}, 500, 'easeInOutQuad', function(){
				$('#carousel .feature:first').appendTo('#carousel');
				$('#carousel').css('left', '-295px');
				carousel.isReady = true;
			});
		}
	},
	scrollLeft: function(){
		if (this.isReady == true) {
			this.isReady = false;	
			$('#carousel').animate({left: 0}, 500, 'easeInOutQuad', function(){
				$('#carousel .feature:last').prependTo('#carousel');
				$('#carousel').css('left', '-295px');
				carousel.isReady = true;
			});
		}
	}	
};