TipSwap = Class.create();
TipSwap.prototype = {	
	initialize: function(tips, qtPanel) {
		this.title = document.title;
		this.tips = tips;
		// cookie tracking
		this.seenTips = [];
		this.checkCookies();
		this.previousTip = false;
		this.qtPanel = qtPanel;
		new Insertion.After(this.qtPanel, '<div id="quicktimecontroller"></div>');
		
		this.qtControlPanel = $('quicktimecontroller');
		this.tips.each(function(tip, i) {
			this.setEvent(tip, i);
			tip.name = tip.innerHTML;
			tip.deeplink = tip.getAttribute('deeplink');
		}.bind(this));
		
		if(location.hash) {
			var initial = location.hash.substring(location.hash.indexOf('#')+1, location.hash.length);
			
			this.tips.each(function(tip, i) {				
				if(tip.deeplink == initial) this.handleClick(null, this.tips[i], i);
			}.bind(this));
		}
	},
	
	setEvent: function(tip, i) {
		Event.observe(tip, 'click', function(evt, tip) {
			this.handleClick(evt, tip, i);
		}.bindAsEventListener(this, tip));
	},
	
	handleClick: function(evt, tip, i) {
		if(evt) Event.stop(evt);
		
		//currentTip/this.previousTip used to update active states
		var currentTip = tip.up();
		// set the document.title to make click tracking a breeze
		document.title = this.title+' - '+tip.name;
		document.location.hash = tip.deeplink;

		// sets/updates cookie when clicked
		this.setCookie(tip, i);

		// set active classes
		currentTip.hasClassName('active') ? $break : currentTip.addClassName('active');
		this.previousTip ? this.previousTip.hasClassName('active') ? this.previousTip.removeClassName('active') : $break : $break;
		this.previousTip = currentTip;
		
		// set checked
		currentTip.hasClassName('visited') ? $break : currentTip.addClassName('visited');

		this.swapMovie(tip);
	},
		
	track: function(item, state) {
		if (state == 'V@S') {
			this.order++;
		}

		var movieName = document.title + ' - ';
		movieName += (item.title) ? item.title.stripTags() : item.href.match(/([^\/]+)\.\S\S\S$/)[1];
		if (state == 'V@S') {
			movieName = movieName.replace(/Apple -/, 'V@S:') + ' (US)';
			AC.Tracking.trackPage({
				pageName: movieName,
				prop4: item.href,
				prop6: movieName,
				prop13: movieName
			});
		} else if (state == 'V@E') {
			movieName = movieName.replace(/Apple -/, 'V@E:') + ' (US)';
			AC.Tracking.trackClick({
				prop13: movieName
			}, this, 'o', movieName);
		}
		
	},
	
	swapMovie: function(tip) {
		if(this.qtController) {
			this.qtController.Stop();
			this.qtController.detachFromMovie();
			this.qtController = false;
			this.qtPanel.innerHTML = '';
			this.qtControlPanel.innerHTML = '';
		} 

		var movie = AC.Quicktime.packageMovie('tipMovie', tip.href, {
			width: 640,
			height: 400,
			controller: false,
			showlogo: false,
			cache: true
		});
		this.track(tip, 'V@S', true);
		this.qtPanel.innerHTML = '';
		this.qtPanel.appendChild(movie);
		this.qtController = new AC.QuicktimeController(movie, {
			onMoviePlayable: function() {
				this.qtController.monitorMovie();

				// in essence, autoplay (timeout because JS is faster than the QT plugin)
				this.qtController.Play();

				// set the finished callback (timeout because JS is faster than the QT plugin)
				setTimeout(function() {
					this.qtController.options.onMovieFinished = this.movieFinished.bind(this, tip);
				}.bind(this), 1000);
			}.bind(this)		});
		this.qtController.render(this.qtControlPanel);				
		var movie = null;
	},
	
	movieFinished: function(tip) {
		//console.log(tip);
		this.track(tip, 'V@E', true);
	},
	
	setCookie: function(tip, i) {
		var date = new Date();
		// set cookie to expire in 1 week
		date.setDate(date.getDate()+7);
		// comma delimited to sort later
		var str = this.seenTips.join(',');
		document.cookie = 'seenTip='+str+','+i+'; expires='+date.toGMTString()+'; path=/; domain=apple.com';
		// remember we added it
		this.seenTips.push(i);
	},
	
	checkCookies: function() {
		document.cookie.split(';').each(function(cookie) {
			if(cookie.match(/seenTip/)) {
				var seen = cookie.substring(cookie.indexOf('=')+1, cookie.length).split(',');
				// push each tip into an array (this.seenTips)
				seen.each(function(s) {
					if(s) {
						this.seenTips.push(s);
						this.tips[s].up().addClassName('visited');						
					}
				}.bind(this));
			}
		}.bind(this));
	}
};

Event.observe(window, 'load', function() {
	var tips = $$('a.slideritem');
	var quicktime = $('quicktime');
	if(tips.length > 0 && quicktime) new TipSwap(tips, quicktime);
}, false);
