/*

	Filename             screen-script.js
	Detail               Screen functionality
	Author:              thunder::tech inc.
	License:             CLIENT is defined as the owner of online property from which this file resides or this code is referenced in.
						 ADDITIONAL PARTY is defined as anyone other than thunder::tech or CLIENT.
						 No right is granted to ADDITIONAL PARTY to sell, distribute, modify or otherwise transfer the following source code without explicit written permission by CLIENT or thunder::tech.

*/

/*  ================================
     Sitewide JavaScript
    ================================ */

var ua = {}, IXIAP = {};

ua.window = $(window);
ua.document = $(document);

IXIAP.pageReady = function()
{
	//thunder.client.IXIAP.flashReplace();
	//thunder.client.modify.rollImages();
	//thunder.client.modify.linkOptions();
	//thunder.client.modify.tabSet();
	thunder.client.modify.treeMenu([IXIAP.animatedTreeMenu]);
	//thunder.client.modify.treeMenu(false, [thunder.client.modes.separatorTreeMenu]);
	//thunder.client.modify.treeMenu(false, [thunder.client.modes.imageTreeMenu]);
	thunder.client.modify.selfLabelFields();
	//thunder.client.modify.requireFields();
	//thunder.client.modify.dropSelector(true);
	//thunder.client.modify.scrollFeature(640, 3000, 500);
	IXIAP.featureViewer = $('#feature-viewer');
	if(IXIAP.featureViewer.length > 0)
	{
		IXIAP.featureOptions = $('.feature-option').click(IXIAP.selectFeature);
		IXIAP.homeFeature = new ViewportFeature ('#feature-viewer', 'img', 650, 3000, 700, IXIAP.featureCallback);
		IXIAP.homeNews = $('.whats-new-item');
		/*IXIAP.currentNewsItem = -1;
		IXIAP.scrollNews();
		IXIAP.newsTimer = setInterval(IXIAP.scrollNews, 4000);*/
		
		if(IXIAP.homeNews.length==1)
		{
					IXIAP.homeNews.css({top: '5px'});
		}
		else
		{
					IXIAP.currentNewsItem = -1;
					IXIAP.scrollNews();
					IXIAP.newsTimer = setInterval(IXIAP.scrollNews, 4000);
		}


		$('#starlets').html('<img src="/presentation/amusementpark/images/starlet-1.png" id="starlet-1" class="starlet" alt="" onload="IXIAP.initStarlet(this);" /><img src="/presentation/amusementpark/images/starlet-2.png" id="starlet-2" class="starlet" alt="" onload="IXIAP.initStarlet(this);" /><img src="/presentation/amusementpark/images/starlet-3.png" id="starlet-3" class="starlet" alt="" onload="IXIAP.initStarlet(this);" />');

		IXIAP.starlets = $('.starlet');
		IXIAP.currentStarlet = 0;
		setInterval(IXIAP.animateStarlets, 1000);
	}
	else
	{
		IXIAP.pageOrange = $('#page-orange');
		IXIAP.page = $('#page');
		ua.window.resize(IXIAP.pageResize);
		IXIAP.pageResize();
		setTimeout(IXIAP.pageResize, 500);
		if($('.photo-widget').length > 0)
		{
			IXIAP.photoWidget = new ViewportFeature ('.photo-widget', 'img', 208, 0, 300);
		}
	}
	IXIAP.currentSponsorFold = 0;
	IXIAP.sponsorFolds = $('.sponsor-logo-fold');
	IXIAP.sponsorTimer = setInterval(IXIAP.scrollSponsors, 6000);
	$('.quick-hover').each(IXIAP.quickHover).mouseenter(IXIAP.quickHover).mouseleave(IXIAP.quickHover);
	
	IXIAP.leftStarburst = $('#left-starburst');
	IXIAP.rightStarburst = $('#right-starburst')
	IXIAP.starburstState = false;
	IXIAP.hoverStarbursts();
}

IXIAP.hoverStarbursts = function()
{
	IXIAP.starburstState = !IXIAP.starburstState;
	IXIAP.leftStarburst.animate({left: (IXIAP.starburstState ? -150 : -100), top: (IXIAP.starburstState ? -100 : 0)}, {duration: 2000});
	IXIAP.rightStarburst.animate({right: (IXIAP.starburstState ? -150 : -100), top: (IXIAP.starburstState ? 0 : -100)}, {duration: 2000});
	setTimeout(IXIAP.hoverStarbursts, 4010);
}

IXIAP.animateStarlets = function()
{
	var s;
	IXIAP.currentStarlet = (IXIAP.currentStarlet == 2) ? 0 : IXIAP.currentStarlet + 1;
	s = IXIAP.starlets.eq(IXIAP.currentStarlet);
	if(s.data('sWidth'))
	{
		if(s.width() >= s.data('sWidth') * 0.9)
		{
			s.animate({width: s.data('aWidth'), height: s.data('aHeight'), left: s.data('aLeft'), top: s.data('aTop')}, {duration: 1500});
		}
		else
		{
			s.animate({width: s.data('sWidth'), height: s.data('sHeight'), left: s.data('sLeft'), top: s.data('sTop')}, {duration: 1500});
		}
	}
}

IXIAP.initStarlet = function(starlet)
{
	var p, t, w, h;
	t = $(starlet	);
	p = t.position();
	w = t.width();
	h = t.height();
	t.data('sWidth',w).data('sHeight',h).data('sLeft',p.left).data('sTop', p.top);
	t.data('aWidth',w * 0.5).data('aHeight',h * 0.5).data('aLeft',p.left + (w / 4)).data('aTop', p.top + (h / 4));
}

IXIAP.quickHover = function(evt)
{
	var p, t;
	t = $(this);
	p = t.position();
	if((typeof evt).toLowerCase()=='number')
	{
		t.data('hoverL', p.left);
		t.data('hoverT', p.top);
	}
	else if(evt.type == 'mouseenter')
	{
		t.stop(true, false).animate({left: t.data('hoverL') - 4,top: t.data('hoverT') - 4}, {duration: 300});
	}
	else
	{
		t.stop(true, false).animate({left: t.data('hoverL'),top: t.data('hoverT')}, {duration: 300});
	}
}

IXIAP.pageResize = function()
{
	ua.width = ua.window.width();
	ua.pageHeight = IXIAP.page.height();
	//$('title').text(ua.pageHeight);
	IXIAP.pageOrange.css({width: Math.max(930, (ua.width / 2) + 460), height: ua.pageHeight + 80});
}

IXIAP.featureCallback = function(a, b)
{
	IXIAP.featureOptions.not(IXIAP.featureOptions.eq(b).addClass('active-feature-option')).removeClass('active-feature-option');
}

IXIAP.selectFeature = function()
{
	var index;
	index = IXIAP.featureOptions.index(this);
	IXIAP.featureOptions.not($(this).addClass('active-feature-option')).removeClass('active-feature-option');
	if(IXIAP.homeFeature.index != index)
	{
		//IXIAP.homeFeature.index = index - 1;
		IXIAP.homeFeature.manualScroll(index - IXIAP.homeFeature.index);
	}
}

IXIAP.flashReplace = function()
{
	var so;
	so = new SWFObject("/presentation/amusementpark/flash/swf1.swf", "swf-obj-1", "300", "250", "9", "");
	so.addParam("quality", "high");
	so.addParam("wmode", "transparent");
	so.write("swf-holder-1");
}

IXIAP.scrollNews = function()
{
	if(IXIAP.currentNewsItem >= 0)
	{
		IXIAP.homeNews.eq(IXIAP.currentNewsItem).stop(true, false).animate({top: -145}, {duration: 1000});
	}
	IXIAP.currentNewsItem ++;
	if(IXIAP.currentNewsItem >= IXIAP.homeNews.length) IXIAP.currentNewsItem = 0;
	IXIAP.homeNews.eq(IXIAP.currentNewsItem).stop(true, false).css({top: 155}).animate({top: 5}, {duration: 1000});
}

IXIAP.scrollSponsors = function()
{
	IXIAP.sponsorFolds.eq(IXIAP.currentSponsorFold).stop(true, false).animate({height: 0}, {duration: 500});
	IXIAP.currentSponsorFold ++;
	if(IXIAP.currentSponsorFold >= IXIAP.sponsorFolds.length) IXIAP.currentSponsorFold = 0;
	IXIAP.sponsorFolds.eq(IXIAP.currentSponsorFold).stop(true, false).animate({height: 48}, {duration: 500});
}

function ViewportFeature (featureContainerTerm, featureItemTerm, scrollImageWidth, scrollImageTime, scrollImageAnimationTime, scrollCallback)
{
	this.counting = false;
	this.index = -1;
	this.direction = 1;
	this.scrollImageTime = scrollImageTime;
	this.precommence = function()
	{
		if(this.scrollImageTime)
		{
			var viewportFeature = this;
			this.timeout = setTimeout(function() { viewportFeature.change(); }, this.scrollImageTime);
			this.counting = true;
		}
	}
	this.commence = function()
	{
		if(this.scrollImageTime)
		{
			if(this.counting==true)
			{
				this.counting = false;
				clearTimeout(this.timeout);
			}
		}
		this.change();
	}
	this.manualScroll = function(n) { this.direction = n; this.commence(); if(this.direction > 1) this.direction = 1; if(this.direction < -1) this.direction = -1; }
	this.change = function()
	{
		this.previndex = this.index;
		this.index += this.direction;
		if(this.index >= this.images.length) this.index = 0;
		if(this.index < 0) this.index = this.images.length - 1;
		this.scrollNow();
	}
	this.changeTo = function(newIndex)
	{
		clearTimeout(this.timeout);
		this.previndex = this.index;
		this.index = newIndex;
		if(this.index >= this.images.length) this.index = 0;
		if(this.index < 0) this.index = this.images.length - 1;
		if(this.index != this.previndex) this.scrollNow();
	}
	this.scrollNow = function()
	{
		if(scrollCallback) scrollCallback(this.images[this.index], this.index);
		if(this.previndex>=0)
		{
			if(this.direction>0)
				$(this.images[this.previndex]).animate({'left': 0 - scrollImageWidth}, {duration: scrollImageAnimationTime, queue: false});
			else
				$(this.images[this.previndex]).animate({'left': scrollImageWidth}, {duration: scrollImageAnimationTime, queue: false});
		}
		if(this.index>=0)
		{
			if(this.direction>0)
				this.images[this.index].style.left = scrollImageWidth + 'px';
			else
				this.images[this.index].style.left = '-' + scrollImageWidth + 'px';
			$(this.images[this.index]).animate({'left': 0}, {duration: scrollImageAnimationTime * .95, queue: false});
		}
		this.precommence();
	}
	this.images = $(featureContainerTerm).find(featureItemTerm);
	this.images.css('left', '-'+scrollImageWidth+'px');
	if(this.images.length>0) { this.commence(); this.images[0].style.left = 0; }
}


IXIAP.animatedTreeMenu = {
	over: function(obj)
	{
		$(obj).find('.dropdown-circles').stop(true, false).css({left: 50}).animate({left: 90}, {duration: 400});
	},
	out: function(obj)
	{
	}
}

$(IXIAP.pageReady);
