// 1on1 written by richard z

function onoXslt(filename) {
	var xslt = this;

	$.ajax({
		url: filename,
		async: false,
		dataType: 'xml',
		success: function(data) { xslt.setDoc(data) }
	});

	if (!this.xslDoc) { return; }

	if (!window.ActiveXObject && document.implementation && document.implementation.createDocument) {
		this.xsltProcessor = new XSLTProcessor();
		this.xsltProcessor.importStylesheet(this.xslDoc);
	}
}

onoXslt.prototype = {
	xslDoc: null,
	xsltProcessor: null,

	setDoc: function(xml) {
		this.xslDoc = xml;
	},
	
	transform: function(xmlDoc, doc) {
		if (this.xsltProcessor) {
			return this.xsltProcessor.transformToFragment(xmlDoc, document);
		}

		if (this.xslDoc && window.ActiveXObject) {
			return xmlDoc.transformNode(this.xslDoc);
		}

		return '';
	}
}


function onoListing() {
}

onoListing.prototype = {
	// default values
	group: 'new',
	category: 0,
	artist: 0,
	page: 1,
	pagesize: 5,
	order: 'release',
	dir: 'desc',
	selected: -1,

	xslt: new onoXslt('/1on1/ono.xsl'),
	viewXslt: new onoXslt('/1on1/view.xsl'),
	
	fetch: function(changes) {
		if (changes) {
			this.group = changes['group'] || this.group;
			this.category = changes['category'] || this.category;
			this.artist = changes['artist'] || this.artist;
			this.page = changes['page'] || this.page;
			this.pagesize = changes['pagesize'] || this.pagesize;
			this.order = changes['order'] || this.order;
			this.dir = changes['dir'] || this.dir;
		}
		
		var listing = this;
		
		if (changes['updateHash']) {
			var sect = '';
			
			if (this.group == 'artist')
				sect += ':' + this.artist;
			else if (this.group == 'category')
				sect += ':' + this.category;
				
			location.hash = '#' + this.group + sect + '/page:' + this.page + '/sort:' + this.order;
		}

		$.get(
			'/1on1/1on1_list.php',
			{
				group: this.group,
				category: this.category,
				artist: this.artist,
				page: this.page,
				pagesize: this.pagesize,
				order: this.order,
				dir: this.dir
			},
			function(xml) {
				listing.xml = xml;
				$('#onoList').html(listing.xslt.transform(xml));
				$('#onoListTitle').sifr({
					path: '/sifr/',
					font: 'trajan-font',
					textAlign: 'left'
				});

				if (!$('#onoView').children().length) {
					var found = false;
					if (changes['selected']) {
						$('#' + changes['selected'], xml).each(function(){
							listing.view(changes['selected']);
							found = true;
						});
					}
					if (!found) {
						listing.view($('Record:first-child', xml).attr('id'));
					}
				} else {
					$('#ono' + listing.selected).addClass('selected');
				}
			},
			'xml'
		);
	},
	
	fetchPage: function(_page) {
		this.fetch({page: _page})
	},
	
	changeDir: function() {
		if (this.dir == 'asc')
			this.fetch({dir: 'desc'});
		else
			this.fetch({dir: 'asc'});
	},
	
	learnmore: function() {
		var html = '<div id="onoVideo" url="/previews/1on1Welcome.flv" thumbnail="/images/banners/h_defaultThumb.jpg" title="Welcome to Theory11 1-on-1"></div><div id="onoViewInfo"><div class="onoTitle">Welcome to 1-on-1 training</div><div class="onoArtist">at theory11</div><div class="onoDescription"><strong>Learn from the best. This is 1-on-1.</strong> Traditionally, sleight of hand and cardistry are skills that were learned through hours of practice and reading the treasured books published by true masters. 1-on-1 is a division of theory11 revolutionizing this process.<br/><br />With individual training videos focusing on individual techniques, you can now learn from the best at your own pace, without breaking the bank. Each video is produced in theory11 high definition, with <em>extreme</em>detail on every topic. Whether you\'re a beginner or pro, you\'ll find something here that will take your skills to the next level. <strong>This section is updated every Friday at 11:00pm EST.</strong></div>';
		$('#onoView').html(html);
		this.styleVideo('/1on1/');
	},
	
	styleVideo: function(returnPage) {
		$('.onoTitle').sifr({
			path: '/sifr/',
			font: 'trajan-font',
			textAlign: 'left'
		});
		$('.onoArtist').sifr({
			path: '/sifr/',
			font: 'trajan-font',
			textAlign: 'left'
		});
		
		var url = $('#onoVideo').attr('url'),
			thumbnail = $('#onoVideo').attr('thumbnail'),
			title = $('#onoVideo').attr('title')

		var so = new SWFObject("http://cdn.theory11.com/_swf/T11_1on1_player.swf", "Video", "529", "340", "8", "#000000");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("quality", "high");
		so.addParam("scale", "noscale");
		so.addParam("menu", "false");
		so.addParam("loop", "false");
		so.addParam('wmode', 'opaque');
		so.addVariable("Title", title);
		so.addVariable("VideoName", url.replace('http://cdn.theory11.com', ''));
		so.addVariable("VideoThumb", thumbnail);
		so.addVariable("AutoStart", "0");
		so.addVariable("ReturnPage", returnPage);
		so.write("onoVideo");
	},
	
	view: function(id) {
		var listing = this;

		$('#' + id, this.xml).each(function(){
			listing.selected = id;
			
			$('.onoListItem').each(function(){
				if (this.id.substr(3) == id) {
					$(this).addClass('selected');
				} else {
					$(this).removeClass('selected');
				}
			});

			$('#onoView').html(listing.viewXslt.transform(this));
			
			listing.styleVideo('/1on1/' + $('#ono' + id).attr('href'));
		});
	}
}

function onoShowSortOptions(alignWith) {
	$('#onoCategories').slideUp(200, null);
	$('#onoArtists').slideUp(200, null);
	$('#onoSortOptions')
		.css('left', $(alignWith).offset().left)
		.css('top', $(alignWith).offset().top + $(alignWith).height())
		.slideToggle(200, null);
}

function onoChangeDirClass(elem) {
	if (elem.className == 'onoDirasc') {
		elem.className = 'onoDirdesc';
	} else {
		elem.className = 'onoDirasc';
	}
	elem.onmouseout = function() { onoChangeDirClass(elem) };
}

var onoMainListing;

$(document).ready(function(){

	onoMainListing = new onoListing();
	
	var preloads = [
		'onomibg.gif',
		'onoitembg.gif',
		'onoitembg_hover.gif'
	];
	for (var i = 0; i < preloads.length; ++i)
		$('<img>').attr('src', '//cdn.theory11.com/images/ono/' + preloads);
	
	$('.onoSortOption').click(function(){
		$('#onoSortOptions').slideToggle(200, null);
		onoMainListing.fetch({order:this.title, updateHash:true});
		return false;
	});
	
	$('.onoCatOption').click(function(){
		$('#onoCategories').slideToggle(200, null);
		onoMainListing.fetch({
			group: 'category',
			category: this.getAttribute('value'),
			page: 1,
			updateHash: true
		});
		return false;
	});
	
	$('.onoArtistOption').click(function(){
		$('#onoArtists').slideToggle(200, null);
		onoMainListing.fetch({
			group: 'artist',
			artist: this.getAttribute('value'),
			page: 1,
			updateHash: true
		});
		return false;
	});

	var firstGo = location.hash.substr(1).split('/'), params = {};
	for (var i = 0; i < firstGo.length; ++i) {
		var param = firstGo[i].split(':');
		if (param.length > 0) {
			switch (param[0])
			{
			case 'new':
			case 'best':
				params.group = param[0];
				break;
			
			case 'learnmore':
				onoMainListing.learnmore();
				break;
			}
		}
		if (param.length > 1) {
			switch (param[0]) {
			case 'category':
			case 'artist':
				params.group = param[0];
				params[param[0]] = param[1];
				break;
		
			case 'page':
				params.page = param[1];
				break;
			
			case 'sort':
				params.order = param[1];
				break;
			
			case 'video':
				params.selected = param[1];
			}
		}
	}
	
	onoMainListing.fetch(params);
	
	$('.onoMiText').addClass('onoClickable');
	$('.onoMenuItem')
		.addClass('onoClickable')
		.click(function(){
			var section = this.getAttribute('section');
	
			if (section == 'new') {
				onoMainListing.fetch({
					page: 1,
					group: section,
					updateHash: true
				});
			} else if (section == 'categories') {
				$('#onoArtists').slideUp(200, null);
				$('#onoSortOptions').slideUp(200, null);
				$('#onoCategories')
					.css('left', $(this).offset().left)
					.css('top', $(this).offset().top + $(this).height())
					.slideToggle(200, null);
			} else if (section == 'artists') {
				$('#onoCategories').slideUp(200, null);
				$('#onoSortOptions').slideUp(200, null);
				$('#onoArtists')
					.css('left', $(this).offset().left)
					.css('top', $(this).offset().top + $(this).height())
					.slideToggle(200, null);
			} else if (section == 'best') {
				onoMainListing.fetch({
					page: 1,
					group: 'best',
					updateHash: true
				});
			} else {
				alert('Not implemented yet.');
			}
			return false;
		});
});
