var Intro = {
	/** settings **/
	ticker: {speed: 3, interval: 50, id: "#ticker", slider: "#ticker-slider", mouseover: false, speedFactor: 1, speedDecay: 0.04},
	blinkInterval: 400,
	blinkExclude: null, 
	blinkIntervalId: 0,
	
	/** setup functions **/
	setup: function(){
		this.loadIntroSWF();
		this.setupTicker();
		this.setupRollOvers();
		this.setupTours();
		this.setupBlinks();
		
		setInterval(window.Intro.runTicker, window.Intro.ticker.interval);
	},
	
	loadIntroSWF: function (){
		
		var flashvars = {};
		var params = {menu: "false", scale: "noscale", wmode: "transparent"};
		var attributes = {};
		
		swfobject.embedSWF("intro.swf", "display", "994", "455", "9.0.0","expressInstall.swf", flashvars, params, attributes);
		
		if ($('#disclaimer-ani').length > 0){
			params = {menu: "false", scale: "noscale", wmode: "window"};
			swfobject.embedSWF("intro-logo.swf", "disclaimer-ani", "688", "240", "9.0.0","expressInstall.swf", flashvars, params, attributes);
		}

	},
	
	setupTicker: function(){
		_this = window.Intro;
		$(_this.ticker.id).bind('mouseenter', function(){
			_this.ticker.mouseover = true;
		});
		
		$(_this.ticker.id).bind('mouseleave', function(){
			_this.ticker.mouseover = false;
		});
	},
	
	setupRollOvers: function(){
		_this = window.Intro;
		$(".rollover").hover(
				function(){  
					//$(this).children("a").fadeOut();  
					$(this).children("a").stop().animate({'top':'-50px'});
					$(this).children("a").children("span").stop().animate({'margin-top':'30px'});
				},
				function(){  
					//$(this).children("a").fadeIn();  
					$(this).children("a").stop().animate({'top':'30px'});
					$(this).children("a").children("span").stop().animate({'margin-top':'40px'});
				}
			);  
	},
	
	setupTours: function(){
		_this = window.Intro;
		
		var kids = $('#tourstrip ul li');
		if (kids.length > 0){
			var w = $(kids[0]).outerWidth(); 
			$('#tourstrip ul').css('width', parseInt(kids.length * w) + 'px');
		}
		
		$('#tourstrip ul li').each(function(idx, elm){
			$(elm).bind('click', window.Intro.tourClick);
		});
		
		$('.navi-left').bind('click', _this.naviLeftClick);
		$('.navi-right').bind('click', _this.naviRightClick);
	},
	
	setupBlinks: function(){
		_this = window.Intro;
		$('.hoverblink').hover(_this.hoverBlinkIn, _this.hoverBlinkOut);
	},
	
	/** logic functions **/
	runTicker: function(){
		
		_this = window.Intro;

		if (_this.ticker.mouseover && (_this.ticker.speedFactor > 0)) _this.ticker.speedFactor -= _this.ticker.speedDecay
		else if (!_this.ticker.mouseover && (_this.ticker.speedFactor < 1)) _this.ticker.speedFactor += _this.ticker.speedDecay;

		var ts = $(_this.ticker.slider);
		var t = $(_this.ticker.id);
		var p = ts.position();
		var l = p.left - (_this.ticker.speed * _this.ticker.speedFactor);
		
		if (l < (0 - ts.width())) l = t.width();
		
		$(_this.ticker.slider).css("left", l);
	},
	
	/** Handlers **/
	naviLeftClick: function() {
		var kids = $('#tourstrip ul li');
		if (kids.length > 0){
			var ts = $('#tourstrip');
			var tsul = $('#tourstrip ul');
			var p = tsul.position();
			var l = p.left - $(kids[0]).outerWidth();
			if (l < (0 - tsul.width() + ts.width() - $(kids[0]).outerWidth())) l = 0;
			tsul.stop().animate({'left': l});
		}
		return false;
	},
	
	naviRightClick: function() {
		var kids = $('#tourstrip ul li');
		if (kids.length > 0){
			var ts = $('#tourstrip');
			var tsul = $('#tourstrip ul');
			var p = tsul.position();
			var l = p.left + $(kids[0]).outerWidth();
			if (l > ($(kids[0]).outerWidth())) l = 0 - tsul.width() + ts.width();
			tsul.stop().animate({'left': l});
		}
		return false;
	},
	
	hoverBlinkIn: function(){
		_this = window.Intro;
		
		_this.hoverBlinkOut();
		_this.blinkExclude = this;
		//_this.blinkIntervalId = setInterval(_this.blinker, _this.blinkInterval);
		_this.blinker();
	},
	
	hoverBlinkOut: function(){
		_this = window.Intro;
		_this.blinkExclude = null;
		//clearInterval(_this.blinkIntervalId);
		$('.hoverblink').stop(true).css('opacity', 1.0);
	},
	
	blinker: function(){
		_this = window.Intro;
		//console.log($('.hoverblink'));
		$('.hoverblink').each(function(idx, elm){
			if (elm !== _this.blinkExclude){
				$(elm).stop()
					.animate({opacity: 1.0}, 0)
					.delay(idx*100)
					.animate({opacity: 0.0}, 0)
					.animate({opacity: 1.0}, window.Intro.blinkInterval)
					;
			}
		});
	},
	
	tourClick: function(){
		_this = window.Intro;
		var src = $(this).children('a').attr('src');
		
		$.ajax({url: src, success: _this.tourAjaxSuccess, data: null, dataType: 'text'});

		return false;
	},
	
	tourAjaxSuccess: function(data){
		var dt = encodeURIComponent(data);
		
		var flashvars = {
			tourInfoXMLData: dt,
			skinPath: 'tours/skins/mint_tea_hide/MintTeaHideSkin.swf?20101013-152055'
		}
		var params = { allowFullScreen: true, menu: false, wmode: 'opaque' };
		var attributes = {};
		
		var tpVersion = '20110124-155050';
		
		var tpArgs9 = {
				swf: 'tours/tourplayer9.swf?' + tpVersion,
				minFlashVersionString: "9.0.28"
		};
		var tpArgs10 = {
				swf: 'tours/tourplayer10.swf?' + tpVersion,
				minFlashVersionString: "10.0.0"
		};
		tpArgs = swfobject.hasFlashPlayerVersion('10.0.0') ? tpArgs10 : tpArgs9;
		
		swfobject.embedSWF(tpArgs.swf, "display", 994, 500, tpArgs.minFlashVersionString, "expressInstall.swf", flashvars, params, attributes);	
		//console.log(data);
	}
}

$(document).ready(function (){
		//console.log("domready");
		Intro.setup();
	}
);
