var QuickTip = Class.create();
QuickTip.prototype = {
	initialize: function(qtPanel, qtControlPanel, qtTrigger, qtEndstate) {
		this.qtPanel = $(qtPanel);
		this.qtControlPanel = $(qtControlPanel);
		this.qtTrigger = $(qtTrigger);
		this.qtEndstate = $(qtEndstate);
		this.movieUrl = this.qtTrigger.href;
		this.qtController = null;
		Event.observe(qtTrigger, 'click', this.setupMovie.bindAsEventListener(this));
	},
	
	setupMovie: function(evt) {
		Event.stop(evt);
		var movie = AC.Quicktime.packageMovie('qt-movie', this.movieUrl, {
			width: 640,
			height: 400,
			autoplay: true,
			controller: false,
			cache: true,
			poster: ''
		});
		this.qtEndstate.hide();
		this.qtPanel.innerHTML = '';
		this.qtPanel.appendChild(movie);
		this.qtController = new AC.QuicktimeController(movie, {
			onMoviePlayable: function() {
				this.track('V@S');
				this.qtController.monitorMovie();
				this.qtController.Play();
				setTimeout(function() {
					this.qtController.options.onMovieFinished = this.resetMovie.bind(this);
				}.bind(this),1000);
			}.bind(this)
		});
		this.qtController.render(this.qtControlPanel);
		movie = null;
	},
	
	resetMovie: function() {
		this.track('V@E');
		this.qtController.options.onMovieFinished = null;
		this.qtController.Stop();
		this.qtControlPanel.innerHTML = '';
		this.qtPanel.innerHTML = '';
		this.qtEndstate.down('.text').innerHTML = 'Watch Again';
		var replay = Builder.node('p', { className:'pillbutton replay' }, [
			Builder.node('a', { href:'/au/business/theater/'}, [
				Builder.node('span', 'View more Quick Tips'),
				Builder.node('b', '&gt;')
			])
		]);
		this.qtEndstate.appendChild(replay);
		this.qtEndstate.show();
	},
	
	track: function(state) {
		if (state == 'V@S') {
			this.order++;
		}

		var movieName = document.title + ' - ' + $('qt-endstate').down('h3').innerHTML.stripTags();
		if (state == 'V@S') {
			movieName = movieName.replace(/Apple -/, 'V@S:') + ' (US)';
			AC.Tracking.trackPage({
				pageName: movieName,
				prop4: this.movieUrl,
				prop6: movieName,
				prop13: movieName
			});
		} else if (state == 'V@E') {
			movieName = movieName.replace(/Apple -/, 'V@E:') + ' (US)';
			AC.Tracking.trackClick({
				prop13: movieName
			}, this, 'o', movieName);
		}
		
	}
}
var heroPromos = [
	'<a href="\/business\/profiles\/quist\/"><img src="\/business\/images\/index_hero_1_20080226.jpg" width="483" height="245" border="0" alt="Quist Valuation"><\/a>', 
	'<a href="\/business\/profiles\/sarafrance\/"><img src="\/business\/images\/index_hero_2_20080226.jpg" width="483" height="245" border="0" alt="Sara France Photography"><\/a>', 
	'<a href="\/business\/profiles\/slopecycle\/"><img src="\/business\/images\/index_hero_3_20080229.jpg" width="483" height="245" border="0" alt="Slopecycle"><\/a>', 
	'<a href="\/business\/profiles\/invisiblechildren\/"><img src="\/business\/images\/index_hero_4_20080229.jpg" width="483" height="245" border="0" alt="Invisible Children"><\/a>', 
	'<a href="\/business\/profiles\/duematernity\/"><img src="\/business\/images\/index_hero_5_20080226.jpg" width="483" height="245" border="0" alt="Due Maternity"><\/a>'
	
];
var imgLoader = new Image();
for(i=0; i<heroPromos.length; i++) {
	imgLoader.src = heroPromos[i].substring(heroPromos[i].indexOf('images/index_hero'), heroPromos[i].indexOf('index_hero')+25);
}

var randomizeWhyMac = [
	'<img src="\/business\/images\/index_macatwork_leopard20071031.jpg" width="100" height="114" alt="Leopard" class="right">\r<h4>Reason #1: Work more, labor&nbsp;less.<\/h4>\r<p>In no time, you’ll find you and your employees are working more happily and efficiently than ever before. <a href="\/business\/macatwork\/" class="more">Learn&nbsp;More<\/a><\/p>',	
	'<img src="\/business\/images\/index_macatwork_ilife20071031.jpg" width="100" height="114" alt="Leopard" class="right">\r<h4>Reason #2: Unleash your creativity. Finally.<\/h4>\r<p>Don’t let the designers have all the fun. Discover your creative side with iLife and iWork. <a href="\/business\/macatwork\/#creativity" class="more">Learn&nbsp;More<\/a><\/p>',
	'<img src="\/business\/images\/index_macatwork_photos20071031.jpg" width="100" height="114" alt="Leopard" class="right">\r<h4>Reason #3: The extras don’t cost any extra.<\/h4>\r<p>Macs are streamlined and simple – yet right out of the box, they do more than you might expect. <a href="\/business\/macatwork\/#extras" class="more">Learn&nbsp;More<\/a><\/p>', 
	'<img src="\/business\/images\/index_macatwork_office20071031.jpg" width="100" height="114" alt="Leopard" class="right">\r<h4>Reason #4:  Mac be nimble, Mac be quick.<\/h4>\r<p>Anything you need to do for your business, you can do on a Mac. <a href="\/business\/macatwork\/#nimble" class="more">Learn&nbsp;More<\/a><\/p>', 
	'<img src="\/business\/images\/index_macatwork_print20071031.jpg" width="100" height="114" alt="Leopard" class="right">\r<h4>Reason #5: Macs play well with others.<\/h4>\r<p>Mac user in a PC world? Don’t worry. Most popular applications for Mac and PC use the same file formats. <a href="\/business\/macatwork\/#playwell" class="more">Learn&nbsp;More<\/a><\/p>',
	'<img src="\/business\/images\/index_macatwork_applecare20071031.jpg" width="100" height="114" alt="Leopard" class="right">\r<h4>Reason #6: Good help is easy to find.<\/h4>\r<p>With an intuitive system, it isn’t often that you’ll be looking for assistance. <a href="\/business\/macatwork\/#help" class="more">Learn&nbsp;More<\/a><\/p>',
	'<img src="\/business\/images\/index_macatwork_imac20071031.jpg" width="100" height="114" alt="Leopard" class="right">\r<h4>Reason #7: Pick up the pace, Mac style.<\/h4>\r<p>Macs come with two secret weapons for speed and security. <a href="\/business\/macatwork\/#pace" class="more">Learn&nbsp;More<\/a><\/p>'
];
var randomizePromoTop = [
	'<a href="http:\/\/www.apple.com.au\/macbookair\/"><img src="\/business\/images\/index_heropromo_macbookair.gif" width="73" height="60" border="0" alt="Macbook air"><\/a>\r<h3><a href="http:\/\/www.apple.com\/macbookair\/">Introducing MacBook Air.<\/a><\/h3>\r<p>The world\'s thinnest notebook. <a href="http:\/\/www.apple.com\/macbookair\/" class="more">Learn&nbsp;More<\/a><\/p>',				 
	'<a href="http:\/\/www.apple.com.au\/macosx\/"><img src="\/business\/images\/index_heropromo_leopard.gif" width="73" height="60" border="0" alt="Leopard"><\/a>\r<h3><a href="http:\/\/www.apple.com\/macosx\/">Leopard.<\/a><\/h3>\r<p>See the most impressive Mac OS X version yet. <a href="http:\/\/www.apple.com\/macosx\/" class="more">Learn&nbsp;More<\/a><\/p>', 
	'<a href="http:\/\/www.apple.com.au\/iwork\/"><img src="\/business\/images\/index_heropromo_iwork.gif" width="73" height="60" border="0" alt="iWork"><\/a>\r<h3><a href="http:\/\/www.apple.com\/iwork\/">The new iWork \'08.<\/a><\/h3>\r<p>Streamlined word processing, stunning presentations and now spreadsheets. <a href="http:\/\/www.apple.com\/iwork\/" class="more">Learn&nbsp;More<\/a><\/p>'
];
var randomizePromoBottom = [
	'<a href="http:\/\/store.apple.com\/133-622\/WebObjects\/australiastore?node=home\/mac\/campaigns\/ms_office_2008_landing"><img src="\/business\/images\/index_heropromo_office.gif" width="73" height="60" border="0" alt="Office 2008"><\/a>\r<h3><a href="http:\/\/store.apple.com\/1-800-MY-APPLE\/WebObjects\/AppleStore?node=home\/mac\/campaigns\/ms_office_2008_landing">Office ‘08 for Mac.<\/a><\/h3>\r<p>More tools and greater compatibility with MS Office 2007 for Windows. <a href="http:\/\/store.apple.com\/1-800-MY-APPLE\/WebObjects\/AppleStore?node=home\/mac\/campaigns\/ms_office_2008_landing" class="more">Learn&nbsp;More<\/a><\/p>',
	'<a href="http:\/\/www.apple.com.au\/timecapsule\/"><img style="margin-top:8px;" src="\/business\/images\/index_heropromo_timecapsule.gif" width="73" height="60" border="0" alt="Time Capsule"><\/a>\r<h3 style="padding-top:12px;"><a href="http:\/\/www.apple.com\/timecapsule\/">Time Capsule.<\/a><\/h3>\r<p>A leap forward for backup. <a href="http:\/\/www.apple.com\/timecapsule\/" class="more">Learn&nbsp;More<\/a><\/p>'			 
];

Event.onDOMReady(function() {
	new AC.CrossfadeQueue(heroPromos, 'hero-promo');
	// quicktip movie
	new QuickTip('qt-movie', 'qt-controller', 'qt-trigger', 'qt-endstate');
	
	// solutions swqp
	new AC.ContentSwap('swapnav', 'swapcontent', 'click');

	
	// Why Mac and hero promo randomizers
	if ($('random-tips')) {
		var randNum = Math.floor(Math.random() * (randomizeWhyMac.length));
		$('random-tips').innerHTML = randomizeWhyMac[randNum];
	}
	// 	if ($('index-heropromo-top')) {
	// 		var randNum = Math.floor(Math.random() * (randomizePromoTop.length));
	// 		$('index-heropromo-top').innerHTML = randomizePromoTop[randNum];
	// 	}
	if ($('index-heropromo-bottom')) {
		var randNum = Math.floor(Math.random() * (randomizePromoBottom.length));
		$('index-heropromo-bottom').innerHTML = randomizePromoBottom[randNum];
	}
	
	// insert profiles
	if($('profiles')) new ShowProfiles($('profiles'));
});
