window.blackhole = {};

var Class = {
	create : function() {
		return function() {
			this.initialize.apply(this, arguments);
		};
	}
};

var siteReady,
	activeP = 'index',
	activeSubP = '',
	firstLoad = true,
	firstFlash,
	ie = $.browser.msie;

blackhole._contentPusher = Class.create();
blackhole._contentPusher.prototype = {
	initialize: function (_url) {
		var ref = this;
		this.url = _url;
		this.contentToPush;
		this.contentPushed = 0;
		$.ajax({
			type: 'GET',
			url: ref.url,
			dataType: 'xml',
			success: function(xml) {
				var xmlNode = $(xml),
					aboutNodes = xmlNode.find('about item'),
					workNodes = xmlNode.find('works work'),
					jobNodes = xmlNode.find('jobs job');
					ref.contentToPush = aboutNodes.length + workNodes.length + jobNodes.length;
					ref.checkState();
					ref.pushAbout(aboutNodes);
					ref.pushWorks(workNodes);
					ref.pushJobs(jobNodes);
			}
		});
	},
	checkState: function() {
		var ref = this;

		if (ie) {
			var originalTitle = 'Black Hole Entertainment';
			document.attachEvent('onpropertychange', function (evt) {
				if (evt.propertyName.indexOf('title') == -1 && document.title !== originalTitle) {
					document.title = originalTitle;
				}
			});
		}

		var t = setInterval(function() {
			if (ref.contentPushed == ref.contentToPush) {
				siteReady = true;
				clearInterval(t);
			}
		}, 200);
	},
	createList: function(node, i, id, sectionUrl, mnuPrefix) {
		var outer = (node.attr('type') && node.attr('type') == 'outer') ? true : false;
		var eventDriven = (node.attr('type') && node.attr('type') == 'eventDriven') ? true : false
		var listItem = $('<li/>')
			.append(
				$('<a/>')
					.addClass((function() {if (i == 0) return 'active '; return '' }()) + (function() {if (outer) return 'outer'; return '' }()))
					.attr({
						id: function() {
								if(!outer && !eventDriven) {
									return mnuPrefix + id;
								}
							},
						href: function() {
									if (outer) return node.attr('href');
									else if(eventDriven) return '#';
									else return sectionUrl + id;
								},
						target: function() {
								if (outer) return '_blank';
									else return '_self';
								}
					})
					.data('order', i)
					.append(
						$('<span/>')
							.addClass('rght')
							.append(
								$('<span/>')
									.addClass('fBold')
									.text(node.attr('title'))
							)
					)
			)
		return listItem;
	},
	pushAbout: function(nodes) {
		var ref = this,
			listMenu = $('#aboutMenu'),
			aboutTexts = $('#aboutTexts > ul');

		aboutTexts.width(730 * nodes.length);
		nodes.each(function(i) {

			var node = $(this),
				menuNode = node.find('menu'),
				aboutId = menuNode.attr('url');

			listMenu.append(ref.createList(menuNode, i, aboutId, '#/about/', 'aboutmnu_'));

			aboutTexts.append(
				$('<li/>')
					.addClass(function() { if (i == 0) return 'active' })
					.attr('id', 'abouttxt_' + aboutId)
					.html(node.find('html').text())
			);

			ref.contentPushed++;
		});
	},
	pushWorks: function(nodes) {
		var ref = this,
			gamesPagerLinks = $('#gamesPagerLinks'),
			gameDesc = $('#gameDesc'),
			legalInfoTo = $('#legalInfoHere');

		gameDesc.width(nodes.length * 970 + 'px');

		nodes.each(function(i) {
			var node = $(this),
				gameId = node.find('menu').attr('url');

			if (i == 0) {
				firstFlash = gameId;
			}

			var listItem = $('<li/>').append(
				$('<a/>')
					.addClass(function() { if (i == 0) return 'active visible' })
					.attr({
						href: '#/game/' + gameId,
						id: 'gamesmnu_' + gameId
					})
					.data('order', i)
					.append(
						$('<img/>')
							.attr({
								alt: node.attr('name'),
								src: 'static/img/content/' + node.find('menu').attr('image'),
								width: node.find('menu').attr('width'),
								height: 132
							})
					)
			);
			gamesPagerLinks.append(listItem);
			var width = listItem.find('img').attr('width');
			listItem.find('a').width(width);
			gamesPagerLinks.width(gamesPagerLinks.width() + width + 25);

			var submenuCont = $('<ul/>')
							.addClass('listMenu eventDriven w200')
							.append(
								$('<li/>')
									.addClass('blob')
									.text(' ')
							);

			node.find('submenu menu').each(function(i) {
				submenuCont.append(ref.createList($(this), i, gameId, '#/game/', 'gamesmnu_'));
			});

			var related = $('<dl/>').addClass('related');

			node.find('related link').each(function(i) {
				var subnode = $(this);
				related.append(
					$('<dd>')
						.addClass('fNormal')
						.text(subnode.attr('title'))
				).append(
					$('<dt/>').append(
						$('<a/>')
							.attr('href', subnode.attr('href'))
							.attr('target', '_blank')
							.append(
								$('<span/>')
									.addClass('fNormal')
									.text(subnode.text())
							)
					)
				);
			});

			var moreinfo = $('<dl/>');

			node.find('moreinfo text').each(function(i) {
				var subnode = $(this);
				moreinfo.append(
					$('<dd>')
						.addClass('fNormal')
						.text(subnode.attr('title'))
				).append(
					$('<dt/>')
						.addClass('noLnk fNormal')
						.text(subnode.text())
					)
			});

			var features = $('<ul/>');

			node.find('features ftr').each(function(i) {
				var subnode = $(this);
				features.append(
					$('<li/>')
						.addClass('fNormal')
						.text(subnode.attr('text'))
				)
			});

			gameDesc.append(
				$('<li/>')
					.addClass(function() { if (i == 0) return 'gamesCont active'; return 'gamesCont' })
					.attr('id', 'games_' + gameId)
					.append(
						$('<div/>')
							.addClass('infoCol')
							.append(submenuCont)
							.append(related)
							.append(moreinfo)
					)
					.append(
						$('<div/>')
							.addClass('about')
								.append(
									$('<div/>')
										.addClass('left')
										.append(
											$('<img/>')
												.attr({
													src: 'static/img/content/' + node.attr('aboutimg'),
													width: '530',
													height: '170'
												})
										)
										.append(
											$('<div/>')
												.addClass('text')
												.html(node.find('about').text())
									)
							)
							.append(features)
					)
					.append(
						$('<a/>')
							.addClass('legalLink fBold')
							.attr('href', '#')
							.text('Legal information')
							.data('order', i)
					)
					.data('order', i)
			)

			legalInfoTo.append(
				$('<div/>')
					.addClass('legalInfo')
					.append(
						$('<div/>')
							.addClass('contentTop')
					)
					.append(
						$('<div/>')
							.addClass('content fNormal')
							.html(node.find('legal').text())
					)
					.append(
						$('<div/>')
							.addClass('contentBottom')
					)
			);

			ref.contentPushed++;
		});
	},
	pushJobs: function (nodes){
		var ref = this,
			jobsCont = $('#jobs .realC'),
			jobTitleBar = $('#jobTitleBar'),
			listMenu = $('#jobList ul.listMenu'),
			jobTexts = $('#jobTextsViewport > ul');

		jobTexts.width((nodes.length - 1) * 710);

		nodes.each(function(i) {
			var node = $(this),
				jobId = node.find('menu').attr('url');

			if (nodes.length == 1 && node.attr('type') == 'nojob') {
				jobsCont.append(node.text());
				contentPushed++;
				return false;
			}
			else {
				$('#jobTitleBar, #noOtherPos').show();
				if (node.attr('type') == 'intro') {
					$(node.text()).insertAfter('#jobs h2');
				}
				else {
					if(i == 1) {
						$('#showJobs').data('showFirst', jobId);
					}

					listMenu.append(ref.createList(node.find('menu'), i - 1, jobId, '#/job/', 'jobsmnu_'));

					var buildCol = function(side) {
						var col = $('<div/>').addClass(side + 'Col');
						node.find(side + 'col section').each(function(i, el) {
							var ul = $('<ul/>');
							ul.addClass(
									function() {
										var className = '';
										if($(el).attr('type') == 'additional') className += 'additional ';
										if(i == 0 ) className += 'noBorderT';
										return className;
									}
								)
								.append(
								$('<li/>')
									.addClass('title fBold')
									.text($(el).attr('title'))
							);
							$(el).find('item').each(function(i, elin) {
								ul.append(
									$('<li/>')
										.addClass(function() { if($(el).attr('type') == 'additional') return 'fMedium'; return 'fNormal'; })
										.text($(elin).attr('text'))
								);
							});
							col.append(ul);
						});
						return col;
					}

					jobTexts.append(
						$('<li/>')
							.addClass('jobText')
							.attr('id', 'jobtxt_' + jobId)
							.append(buildCol('left'))
							.append(buildCol('right'))
					);

					jobTitleBar.append(
						$('<span/>')
							.attr('id', 'jobtitle_' + jobId)
							.addClass('jobTitle fBold')
							.text(node.attr('title'))
					)
				}
			}

			ref.contentPushed++;
		});
	}
};

blackhole._initSammy = Class.create();
blackhole._initSammy.prototype = {
	initialize: function(_parent) {
		this.parent = _parent;
		this.sitemap = {
			'#/': 'index',
			'#/about' : 'about',
			'#/games': 'works',
			'#/game': 'work',
			'#/jobs': 'jobs',
			'#/job': 'job'
		};
	},
	runSammy: function() {
		var ref = this,
			app = $.sammy(function() {
				this.helper('title', function(newTitle) {
					document.title = newTitle;
				});

				this.get('#/', function(context) {
					ref.parent.showPage(ref.sitemap[context.path], null);
					currentSection = context.path;
					this.title('Black Hole Entertainment');
				});

				this.get('#/about', function(context) {
					ref.parent.showPage(ref.sitemap[context.path], null);
					currentSection = context.path;
					this.title('Black Hole Entertainment');
				});

				this.get('#/about/:anchor', function(context) {
					var spl = ref.splitURL(context.path);
					ref.parent.showPage(ref.sitemap[spl.page], spl.param);
					this.title('Black Hole Entertainment');
				});

				this.get('#/games', function(context) {
					ref.parent.showPage(ref.sitemap[context.path], null);
					this.title('Black Hole Entertainment');
				});

				this.get('#/game/', function(context) {
					return false;
				});

				this.get('#/game/:anchor', function(context) {
					var spl = ref.splitURL(context.path);
					ref.parent.showPage(ref.sitemap[spl.page], spl.param);
					this.title('Black Hole Entertainment');
				});

				this.get('#/services', function(context) {
					ref.parent.showPage(ref.sitemap[context.path], null);
					this.title('Black Hole Entertainment');
				});

				this.get('#/jobs', function(context) {
					ref.parent.showPage(ref.sitemap[context.path], null);
					this.title('Black Hole Entertainment');
				});
				this.get('#/job', function(context) {
					ref.parent.showPage(ref.sitemap[context.path], null);
					this.title('Black Hole Entertainment');
				});
				this.get('#/job/:anchor', function(context) {
					var spl = ref.splitURL(context.path);
					ref.parent.showPage(ref.sitemap[spl.page], spl.param);
					this.title('Black Hole Entertainment');
				});
			});

		app.run('#/');
	},
	splitURL: function(url) {
		var spl = {},
		url = url.split('/');
		spl.page = '#/' + url[1];
		spl.param = url[2];
		return spl;
	}
}

blackhole._siteInit = Class.create();
blackhole._siteInit.prototype = {
	initialize: function() {
		this.contentPusher = new blackhole._contentPusher('static/xml/site.xml');
		this.initSammy = new blackhole._initSammy(this);
		this.moreAboutSlideDown = new blackhole._slideDown($('#moreAbout'), $('#aboutUs .listMenu, #aboutTexts'), '#/', '#/about', null);
		this.jobSlideDown = new blackhole._slideDown($('#showJobs'), $('#jobList .listMenu, #jobTextsViewport'), '#/jobs', '#/job');
		this.slideSide = new blackhole._slideSide();
		this.xmlReady();
	},
	xmlReady: function() {
		var ref = this;
		var t = setInterval(function() {
			if (siteReady) {
				if (!ie) {
					ref.loadCufon();
				}
				$('#jobTextsViewport').height(
					$('#jobTextsViewport').find('li.jobText:nth-child(1)').height() + 'px'
				);
				$('#gameDescViewport').height(
					$('#gameDescViewport').find('li:nth-child(1)').height() + 'px'
				);
				$('#gameDesc').find('li.gamesCont:nth-child(1)').height(
					$('#gameDescViewport').height() + 'px'
				);
				$('#jobList .listMenu, #jobTextsViewport').hide();
				ref.slideSide.setEvents();
				setLegalInfo();
				ref.gamesPager = new blackhole._gamesPager();
				ref.initSammy.runSammy();
				$('#loader').fadeOut(2000, function() {
					$('#loaderText').fadeOut(500);
				});
				clearInterval(t);
			}
		}, 200);
	},
	showPage: function(id, param) {
		var target,
			ref = this;

		if(id == 'work') {
			var target = $('#works');
		}
		else if (id == 'job') {
			var target = $('#jobTitleBar');
		}
		else {
			var target = $('#' + id);
		}

		var targetOffset = target.offset().top,
			t = Math.floor(Math.abs((targetOffset - $('html').scrollTop()) / 1.4));

		//open about
		if (activeP != 'about' && id == 'about') {
			$('html:not(:animated),body:not(:animated)').animate({scrollTop: targetOffset}, t, 'easeInOutQuad', ref.moreAboutSlideDown.doSlide(true));
		}

		//open about with parameter
		if (param && (id == 'about')) {
			this.slideSide.sliderMenuAnimate($('#aboutmnu_' + param));
			this.slideSide.sliderSlide(param, '#aboutTexts ul', '#abouttxt_');
		}

		//close about
		if (activeP == 'about' && id != 'about') {
			if (id == 'work') {
				ref.moreAboutSlideDown.doSlide(false);
			}
			else {
				ref.moreAboutSlideDown.doSlide(false);
				$('html:not(:animated),body:not(:animated)').animate({scrollTop: targetOffset}, t, 'easeInOutQuad');
			}
		}

		//open job
		if (activeP != 'job' && id == 'job') {
			changeJobTitle(($('#jobList a.active').attr('id')).replace(/jobsmnu_/gi,''), true);
			$('html:not(:animated),body:not(:animated)').animate({scrollTop: targetOffset}, t, 'easeInOutQuad', ref.jobSlideDown.doSlide(true));
			resizeToHeight($('#jobTextsViewport'), $('#jobList a.active').attr('id').replace(/jobsmnu_/gi,''), '#jobtxt_');
		}

		//open job with parameter
		if (param && (id == 'job')) {
			$('html:not(:animated),body:not(:animated)').animate({scrollTop: targetOffset}, t, 'easeInOutQuad');
			this.slideSide.sliderMenuAnimate($('#jobsmnu_' + param));
			this.slideSide.sliderSlide(param, '#jobTextsViewport ul.jobTextList', '#jobtxt_');
			resizeToHeight($('#jobTextsViewport'), param, '#jobtxt_');
			changeJobTitle(param, true);
		}

		//close job
		if (activeP == 'job' && id != 'job') {
			if (id == 'work') {
				ref.jobSlideDown.doSlide(false);
			}
			else {
				ref.jobSlideDown.doSlide(false);
				$('html:not(:animated),body:not(:animated)').animate({scrollTop: targetOffset}, t, 'easeInOutQuad');
			}
			changeJobTitle(null, false)
		}

		//open jobs
		if (id != 'about' && id != 'work' && id != 'job') {
			$('html:not(:animated),body:not(:animated)').animate({scrollTop: targetOffset}, t, 'easeInOutQuad');
		}

		//open work with parametesr
		if (param && (id == 'work')) {
			this.slideSide.sliderSlide(param, '#gameDesc', '#games_');
			$('#gameDesc').find('li.gamesCont.active .listMenu li:nth-child(2) a').trigger('click');
			resizeToHeight($('#gameDescViewport'), param, '#games_');
			deleteFlash();
			loadFlash(param);
			hideFlash();
			mnuNode = $('#gamesmnu_' + param);
			mnuContainer = $('#gamesPagerLinks')
			mnuContainer.find('a.active').removeClass('active');
			mnuNode.addClass('active');
			if (firstLoad) {
				this.gamesPager.indicatorInit();
			}
			else {
				this.gamesPager.moveArrow();
			}
		}

		//open but not work
		if (firstLoad && id != 'work') {
			loadFlash(firstFlash);
			hideFlash();
		}

		firstLoad = false;
		activeP = id;
		activeSubP = param;
		return false;
	},
	loadCufon: function() {
		Cufon.replace('.fNormal', {
			fontFamily: 'gbNormal'
		});
		Cufon.replace('.fMedium', {
			fontFamily: 'gbMedium',
			fontWeight: 300
		});
		Cufon.replace('.fBold', {
			fontFamily: 'gbBold'
		});
		Cufon.replace('.fLight', {
			fontFamily: 'gbLight'
		});
		Cufon.now();
	}
}

blackhole._slideDown = Class.create();
blackhole._slideDown.prototype = {
	initialize: function(_lnk, _target, _openHref, _closeHref){
		this.lnk = _lnk;
		this.target = _target;
		this.openHref = _openHref;
		this.closeHref = _closeHref;
	},
	doSlide: function(open) {
		var ref = this,
			targetLen = this.target.length;

		this.target.animate({
			'opacity': 'toggle',
			'height': 'toggle',
			'paddingBottom': 'toggle',
			'marginBottom': 'toggle'
		}, 500, 'easeInOutQuad', function(){
			if (targetLen-- > 1)
				return;
			else {
				(open) ? function(){
					ref.crossFade(false);
					ref.lnk.attr('href', ref.openHref);
				}() : function(){
					ref.crossFade(true);
					ref.lnk.attr('href', ref.closeHref);
				}();
			}
		});
	},
	crossFade: function(dir) {
		var ref = this;
		this.lnk.fadeOut(100, function(){
			(dir) ? ref.lnk.removeClass('slideUp') : ref.lnk.addClass('slideUp');
			ref.lnk.fadeIn(150);
		});
	}
}

blackhole._slideSide = Class.create();
blackhole._slideSide.prototype = {
	initialize: function(){
		this.setEvents();
	},
	setEvents: function(){
		var ref = this;
		$('ul.eventDriven').delegate('a:not(.outer)', 'click', function(e){
			e.preventDefault();
			var node = $(this);
			ref.sliderMenuAnimate(node);
			if (node.data('order') == 0) {
				hideFlash(node);
			}
			else
				if (node.data('order') == 1) {
					showFlash(node);
				}
		});
	},
	sliderMenuAnimate: function(node) {
		node.parent('li')
		.siblings('li.blob')
			.hide()
			.css({
				top: node.data('order') * 30 + 'px',
				left: '177px'
			})
			.show()
			.animate({
				left: '186px'
				}, 250, 'linear'
			)
		.end()
		.siblings('li')
			.find('a.active')
				.removeClass('active')
		node.addClass('active');
	},
	sliderSlide: function(param, container, prefix){
		var $container = $(container),
			$item = $(prefix + param),
			s = $item.position().left,
			t = Math.floor(Math.abs((s - Math.abs($container.position().left))) / 1.4);

		$container.animate({
			left: -s + 'px'
		}, t, 'easeInOutQuint');
		$item.siblings().removeClass('active').end().addClass('active');
	}
}

blackhole._gamesPager = Class.create();
blackhole._gamesPager.prototype = {
	initialize: function() {
		this.toLeft = $('#toLeft');
		this.toRight = $('#toRight');
		this.container = $('#gamesPagerLinks');
		this.elWidths = [];
		this.sumWidth = 0;
		this.gap;
		this.visibleSum;
		this.unvisibleItem = 0;
		this.prevUnvisibleItem = 0;
		this.viewPortWidth = 611;
		this.lastDir = 'left';

		this.indicatorCont = $('#indicatorCont');
		this.indicator = $('#indicator');

		this.getElWidths();

		this.minPos = 0;
		this.maxPos = 0 - (this.sumWidth - this.viewPortWidth);

		this.setEvents();
		this.setArrows(0);
	},
	getElWidths: function() {
		var ref = this;
		this.container.find('a').each(function(i, el) {
			ref.elWidths.push($(el).width() + 25);
			ref.sumWidth += ref.elWidths[i];
		});
	},
	calculateGap: function() {
		this.gap = 0;
		this.visibleSum = 0;

		while((this.visibleSum + this.elWidths[this.unvisibleItem]) < this.viewPortWidth) {
			this.visibleSum += this.elWidths[this.unvisibleItem];
			if (this.lastDir == 'left') {
				this.unvisibleItem++;
			}
			else {
				this.unvisibleItem--;
			}
		}
		this.gap = this.viewPortWidth - this.visibleSum;

		if (this.lastDir == 'left') {
			this.lastDir = 'right';
		}
		else {
			this.lastDir = 'left';
		}
	},
	setEvents: function() {
		var ref = this;
		this.toLeft.click(function(e) {
			e.preventDefault();
			if (!ref.container.is(':animated')) {
				if (ref.prevUnvisibleItem > 0) {
					if (ref.lastDir == 'right') {
						ref.calculateGap();
						ref.animateContainer((ref.elWidths[ref.unvisibleItem] - ref.gap));
					}
					else {
						ref.animateContainer(ref.elWidths[ref.unvisibleItem]);
					}

					ref.prevUnvisibleItem = ref.unvisibleItem;
					if (ref.unvisibleItem > 0)
						ref.unvisibleItem--;
				}
			}
		});
		this.toRight.click(function(e) {
			e.preventDefault();
			if (!ref.container.is(':animated')) {
				if (ref.prevUnvisibleItem < ref.elWidths.length - 1) {
					if (ref.lastDir == 'left') {
						ref.calculateGap();
						ref.animateContainer(ref.elWidths[ref.unvisibleItem] - ref.gap);
					}
					else {
						ref.animateContainer(ref.elWidths[ref.unvisibleItem]);
					}

					ref.prevUnvisibleItem = ref.unvisibleItem;
					if (ref.unvisibleItem < ref.elWidths.length - 1)
						ref.unvisibleItem++;
				}
			}
		});
		this.container.delegate('a', 'click', function(e) {
			var cumulated = 0;
			if (!ref.container.is(':animated')) {
				if (ref.lastDir == 'left' && ref.prevUnvisibleItem < ref.elWidths.length - 1) {
					for (var i = ref.prevUnvisibleItem; i <= $(this).data('order'); i++) {
						cumulated += ref.elWidths[i];
					}

					if (cumulated > ref.viewPortWidth) {
						ref.animateContainer(ref.viewPortWidth - cumulated);
						ref.prevUnvisibleItem = $(this).data('order');
						if (ref.prevUnvisibleItem < ref.elWidths.length - 1)
							ref.unvisibleItem = ref.prevUnvisibleItem + 1;
						else
							ref.unvisibleItem = ref.prevUnvisibleItem;
						ref.lastDir = 'right';
					}
				}
				else
					if (ref.prevUnvisibleItem > 0) {
						for (var i = ref.prevUnvisibleItem; i >= $(this).data('order'); i--) {
							cumulated += ref.elWidths[i];
						}

						if (cumulated > ref.viewPortWidth) {
							ref.animateContainer(ref.viewPortWidth - cumulated);
							ref.prevUnvisibleItem = $(this).data('order');
							if (ref.prevUnvisibleItem > 0)
								ref.unvisibleItem = ref.prevUnvisibleItem - 1;
							else
								ref.unvisibleItem = ref.prevUnvisibleItem;
							ref.lastDir = 'left';
						}
					}
			}
		});
	},
	animateContainer: function(dist) {
		var ref = this;
		if (this.lastDir == 'right') {
			dist = ref.container.position().left - dist;
		}
		else {
			dist = ref.container.position().left + dist;
			if (dist > 0)
				dist = 0;
		}
		this.container.not(':animated').animate({
			left: dist + 'px'
		}, 300, 'easeInOutQuad', ref.setArrows(dist));
		this.indicatorCont.not(':animated').animate({
			left: dist + 'px'
		}, 300, 'easeInOutQuad', ref.setArrows(dist));
	},
	indicatorInit: function() {
		this.indicatorCont.width(this.sumWidth);
		this.moveArrow();
	},
	moveArrow: function() {
		var moveTo = 0,
			j = 0,
			activeIndex = this.container.find('li a.active').data('order');

		for (var i = 0; i < activeIndex; i++) {
			moveTo += this.elWidths[i];
			j++;
		}

		moveTo += Math.floor(this.elWidths[j] / 2) - 25;

		this.indicator.not(':animated').animate({
			left: moveTo
		}, 300, 'easeInOutQuad');
	},
	setArrows: function(dist) {
		var containerLeft = this.container.position().left;
		if (dist == this.minPos)
			this.toLeft.css({
				visibility: 'hidden'
			});
		else
			this.toLeft.css({
				visibility: 'visible'
			});

		if (dist == this.maxPos)
			this.toRight.css({
				visibility: 'hidden'
			});
		else
			this.toRight.css({
				visibility: 'visible'
			});
	}
}

/*
 * Flash-related helpers.
 */

function deleteFlash() {
	var flashCont = $('#flashCont');

	swfobject.removeSWF(flashCont.find('object').attr('id'));
	flashCont
		.hide()
		.find('div').remove();
}

function loadFlash(param) {
	var flashCont = $('#flashCont'),
		flashId = 'gamesflash_' + param,
		flashSucc = function(e) {
			if (e.success) {
				flashCont.find('a').hide();
			}
			else {
				flashCont.find('a').css('display', 'block');
			}
		};

	flashCont
		.show()
		.append(
			$('<div/>').attr('id', flashId)
		);

	var params = { quality: 'high', allowscriptaccess: 'always', allowFullScreen: 'true' },
		flashvars = { xmlPath: 'static/swf/xml/content.xml', gameID: $('#games_' + param).data('order') },
		attributes = { id: flashId, name: flashId };

	swfobject.embedSWF('static/swf/viewer.swf', flashId, '740', '304', '10', 'expressInstall.swf', flashvars, params, attributes, flashSucc);
}


function hideFlash() {
	$('#flashCont, #flashCont > object').hide();
	$('#gameDesc').find('.gamesCont.active .about').fadeIn().end().css('z-index', '3');
}

function showFlash() {
	$('#flashCont, #flashCont > object').show();
	$('#gameDesc').find('.gamesCont.active .about').fadeOut().end().css('z-index', '1');
}

function triggerSecondaryMenu(gameId) {
	document.location.hash = '#/game/' + $('#gameDesc > li').eq(gameId).attr('id').split('_')[1];
	document.title = 'Black Hole Entertainment';
	return false;
}

/*
 * Misc helper functions.
 */

function resizeToHeight(container, param, prefix) {
	var node = $(prefix + param);
	container.animate({
		'height': node.outerHeight(true) + 'px'
	}, function() {
		if(prefix == '#games_') {
			node.height(node.find('.about').height() + 'px');
		}
	});
}

function setLegalInfo() {
	var legalInfoTo = $('#legalInfoHere');
	$('.legalLink').click(function(e) {
		e.preventDefault();
		return false;
	});
	$('.legalLink').hover(
		function(e) {
			e.preventDefault();
			legalInfoTo
				.css('top', $(this).parent().height() + 106 + 'px').find('.legalInfo:nth-child(' + ($(this).data('order')  + 1) + ')').show()
				.end().show();
		},
		function(e) {
			e.preventDefault();
			legalInfoTo
				.children().hide()
				.end().hide();
		}
	);
}

function changeJobTitle(param, show) {
	if(show) {
		$('#jobTitle').hide();
		$('.jobTitle').hide();
		$('#jobtitle_' + param).show()
	}
	else {
		$('.jobTitle').hide();
		$('#jobTitle').show();
	}
}

(function($) {
	window.siteInit = new blackhole._siteInit()
})(jQuery);


