$(document).ready(function() {
	balanceCategoryListing();
	
	$('.anchorLinks a, h2 .btnBackToTop').click(function() {
		var hash = $(this).attr('href').match(/#.*$/);
		$.scrollTo('' + hash, 500);
		return false;
	});
	
	if ($('#hero').length != 0) {
		eStore.hipsHero = new SuperHero($('#hero'), true);
	}
});

function balanceCategoryListing() {
	var maxUlHeight = 0;
	$('.l1 .relatedPages .page').each(function() {
		var ulHeight = $('ul', this).height();
		if (ulHeight > maxUlHeight) maxUlHeight = ulHeight;
	});
	$('.l1 .relatedPages .page').css('height', (maxUlHeight + 22) + 'px');
}

var SuperHero = function(container, autoplay) {
	var self = this;
	var timer = null;
	this.panels = new Array();
	this.currentIndex = 0;
	var flashPlayerVersion = swfobject.getFlashPlayerVersion();
	this.hasFlash = (flashPlayerVersion && flashPlayerVersion.major >= 8 ? true : false);
	this.callBackFn = null;

	this.init = function() {
		$('.panel', container).each(function(i) {
			var duration = Number($(this).attr('data-img-duration'));
			var swfDuration = Number($(this).attr('data-swf-duration'));
			var imgSrc = $(this).attr('data-img');
			var swfSrc = $(this).attr('data-swf');
			var type = 'image';
			var altText = $('a', this).text();
			var date = new Date();
			var id = 'hero' + i + date.getTime();
			//$('a', this).attr('id', id).html('<img src="' + imgSrc + '" width="556" height="265" border="0" alt="' + altText + '" />');
			$('a', this).attr('id', id).css('background-image', 'url(' + imgSrc + ')');
			if (swfSrc) {
				if (self.hasFlash) {
					$(this).addClass('flashContent');
					type = 'flash';
					var flashvars = {};
					var autoplay = (i == 0 ? 'true' : 'false' );
					var params = {wmode: 'opaque', play: autoplay, allowScriptAccess: 'always'};
					var attributes = {id: id, name: id};
					swfobject.embedSWF(swfSrc, id, '609', '314', '9', '', flashvars, params, attributes);
					duration = swfDuration;
				}
			}
			self.panels.push({elem: this, duration: duration, type: type, id: id});
		}).eq(0).addClass('show');
		
		self.buildControls();
		if (autoplay) {
			self.play();
		}
		container.removeClass('loading');
		
		$(container).hover(self.pause, self.play);
	}
	
	this.setCallBack = function(fn) {
		this.callBackFn = fn;
	}

	this.buildControls = function() {
		var controls = $('<div class="controls"><div class="pagination"></div><a class="button btnPrev" href="#">Previous</a><a class="button btnNext" href="#">Next</a></div>');
		if (isIE6) {
			$('.button', controls).addClass('noShadow');
		}
		for (var i = 0; i < self.panels.length; i++) {
			var label = $('a', self.panels[i].elem).text();
			$('<a href="#" data-index="' + i + '">' + label + '</a>').appendTo($('.pagination', controls));
		}
		$('.pagination a', controls).eq(0).addClass('selected');
		
		
		$('.button', controls).click(function() {
			self.pause();
			if ($(this).hasClass('btnNext')) {
				self.next();
			}
			else if ($(this).hasClass('btnPrev')) {
				self.prev();
			}
			return false;
		});
		
		$('.pagination a', controls).click(function() {
			var index = Number($(this).attr('data-index'));
			self.pause();
			self.gotoIndex(index, true);
			return false;
		});
		
		container.append(controls);
	}

	this.next = function() {
		var nextIndex = self.currentIndex + 1;
		if (self.currentIndex == (self.panels.length - 1)) nextIndex = 0;
		if (nextIndex == 0 && self.callBackFn != null) {
			self.gotoIndex(nextIndex, false);
		}
		else {
			self.gotoIndex(nextIndex, true);
 			var duration = self.panels[nextIndex].duration * 1000;
			self.timer = setTimeout(self.next, duration);
		}
		
	}
	
	this.prev = function() {
		var nextIndex = self.currentIndex - 1;
		if (self.currentIndex == 0) nextIndex = self.panels.length - 1;
		if (nextIndex == 0 && self.callBackFn != null) {
			self.gotoIndex(nextIndex, false);
		}
		else {
			self.gotoIndex(nextIndex, true);
		}
		
	}
	
	this.gotoIndex = function(nextIndex, rewind) {
		if (self.currentIndex == nextIndex) return false;
		
		var currentPanel = self.panels[self.currentIndex ].elem;
		var nextPanel = self.panels[nextIndex].elem;
		
		if (self.panels[self.currentIndex].type == 'flash') {
			var id = self.panels[self.currentIndex].id;
			document.getElementById(id).StopPlay();
			document.getElementById(id).Rewind();
		}
		$(currentPanel).fadeOut().removeClass('show');
		if (!rewind) {
			self.callBackFn();
			$(nextPanel).show();
		} 
		else {
			$(nextPanel).fadeIn(750, function() {
				$(this).addClass('show');
				if (self.panels[nextIndex].type == 'flash') {
					var id = self.panels[nextIndex].id;	
					document.getElementById(id).Play();
				}
			});
		}
		self.currentIndex = nextIndex;
		
		$('.pagination a', container).removeClass('selected').eq(nextIndex).addClass('selected');
	}
	
	this.play = function() {
		var currentPanel = self.panels[self.currentIndex];
		var duration = currentPanel.duration * 1000;
		self.timer = setTimeout(self.next, duration);
	}
	
	this.pause = function() { 
		clearTimeout(self.timer);
	}
	
	this.init();
}
