(function($) {
	$.easing.def = "easeInOutCirc";
	$.fn.closeBanner = closeBanner;
	var found = false;
	var hyjack = false;
	
	function init() {
		var d = $('<div id="animatedGoodness"></div>').appendTo('#body').css({ overflow:'hidden', position:'relative', background:'#000000', height:530 });
		$('#features').appendTo(d);
		$('#whats-new').appendTo(d);
		
		$('#features').css({ width:484, height:530, background:'none',position:'absolute', top:0 });
		$('#whats-new').css({ background:'#000000', position:'absolute', zIndex:11, left:484, top:0});
		$('#features a').eq(1).css({ top:358 }).end().eq(2).css({ top:358, left:238 }).end().each(function(i, a) {
			$(a).css({ position:'absolute' }).click(function(e) { 
				if(this.rel) {
					SWFAddress.setValue(e.target.parentNode.rel.slice(0,-4));
					return false;
				}
			});
		});
				
		$('<div id="overlayBackground"></div>').css({ width:742, height:530, top:0, left:0, background:'#2F2F2F url(\'/styles/images/home-loader.gif\') no-repeat center center' }).insertBefore($('#features'));
		SWFAddress.addEventListener(SWFAddressEvent.CHANGE, function(e) {
			var c = e.target.getPathNames();
			if(c == '' || c == 'home') closeBanner();
			else handleChange(c);
		});
	}
	
	function nextBanner(a) {
		hyjackHome();
		$('#overlayFlash').remove();
		
		$('#whats-new').stop().animate({ marginLeft:260 });
		$('#features a').stop().eq(0).animate({ top:-360 }, 400).end().eq(1).animate({ left:-240 }, 600, function() { 
			$('#features').hide();
			$('<div id="overlayFlash"></div>').prependTo($('#animatedGoodness:first'));
			var flashvars = { filename:a.rel, href:a.href, title:a.title, backLabel:$('#features').attr('class') };
			var params = { menu:"false", wmode:"transparent" };
			var attributes = { style:'position:absolute; top:0, left:0;' };	
			swfobject.embedSWF("/flash/home-v2.swf?random="+Math.random()*1000, "overlayFlash", "742", "530", "9.0.0","/flash/expressInstall.swf", flashvars, params, attributes);
		}).end().eq(2).animate({ marginTop:240 }, 500);	
	}
	
	function closeBanner() {
		unHyjackHome();
		$('#features').show();
		$('#whats-new').stop().animate({ marginLeft:0 });
		$('#features a').stop().eq(0).animate({ top:0 }, 400).end().eq(1).animate({ left:0 }, 400).end().eq(2).animate({ marginTop:0 }, 400, function() { 																																  
			$('#overlayFlash').remove();
		}).end();
	}
	
	function hyjackHome() {
		$('a[@href=/],a[@href=],a[@href=http://www.elkhartbrass.com]').each(function(i, a) {
			$(a).click(goHome);
		});
	}
	
	function unHyjackHome() {
		$('a[@href=/],a[@href=],a[@href=http://www.elkhartbrass.com]').each(function(i, a) {
			$(a).unbind('click', goHome);
		});
	}
	
	function goHome() {
		SWFAddress.setValue('/home/');
		closeBanner();
		return false;
	}
	
	function handleChange(c) {
		if(!hyjack) hyjackHome();
		$('#features a').each(function(i,a) {
			if (a.rel.slice(0,-4)==c) {
				nextBanner(a);
				found = true;
			}
		});
		if(!found) {
			found = true;
			$('#whats-new').stop().animate({ marginLeft:260 });
			$('#features a').stop().eq(0).animate({ top:-360 }, 400).end().eq(1).animate({ left:-240 }, 600, function() { 
				$.getJSON('ajax-data.php', { action:'homeFeatures', file:c }, function(data) { if(data.images.length<1) window.location = 'http://www.elkhartbrass.com'; changeFeatures(data,c); });
			}).end().eq(2).animate({ marginTop:240 }, 500);
		}
	}
	
	function changeFeatures(data,c) {
		$('#features a').each(function(i) {
			var current = data.images[i];
			$(this).attr('href', current.url).attr('rel',current.splash_file).attr('title',current.link).find('img').attr('src','/images/features/'+current.file).attr('alt',current.name);
		});
		handleChange(c);
	}
	
	function getFlashMovie(movieName) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
	}
		  
	$(document).ready(function() {
		init();	
   });
})(jQuery);