var slideit;
var delay = 6500;	// transition delay
var time = 1000; // transition spped

function slideTime() {
	slideswitch();
	slideit = setTimeout(slideTime, delay)
}

function playVid(){
	$j('video').get(0).play();
}
function pauseVid(){
	$j('video').get(0).pause();
}

function slideswitch(){
	if(jQuery("#homebanner div.active ul.bannerslider").children().length === 1){	
	}else{
		var	 $cur = $j('#homebanner div.active ul.bannerslider li.active'),
		time = 1500,
		$next;		
		if ($cur.index() === jQuery("#homebanner div.active ul.bannerslider").children().length - 1) {
			$next = $j('#homebanner div.active ul.bannerslider li:first');
		}else{
			$next = $cur.next();
		}
		$next.stop(true,true).fadeIn(time).addClass('active')
		$cur.stop(true,true).fadeOut(time).removeClass('active')
	}
	
}
function runIt(){
	var wHeight = $j(window).height(),
	wWidth = $j(window).width();
	$j('#homebanner, .mybanner, .mybanner ul').height(wHeight).width(wWidth);
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
		$j('.video-js-box, .video-js').height(wHeight-150).width(wWidth-150);
	}else{
		$j('.video-js-box, .video-js').height(wHeight).width(wWidth);
	};
	$j('.vjs-flash-fallback').height(wHeight-35).width(wWidth);
	$j('#homebanner li img, #tselogo').each(function(){
		var ratio = 0.6;	
		imgW = wWidth;
		imgH = Math.round(wWidth * ratio);	
		if(imgH < wHeight){
			imgH = wHeight;
			imgW = Math.round(imgH / ratio);
		}
		$j(this).height(imgH).width(imgW)
	})
}

$j(document).ready(function(){
	var hash = window.location.hash.substr(1);
	var href = $j('#banner-nav a.bannerselector').each(function(){
		var href = $j(this).attr('href');
		if(hash==href.substr(1,href.length-0)){			
			$j(href).fadeIn(1000).addClass("active");
			$j(this).addClass('active');
		}
		else if(hash == ''){
			$j('#homebanner div.mybanner:first').addClass('active');
			$j('#banner-nav a:first').addClass('active');
		}else{
		}
		runIt()
	});
	
	
	var $allbanner = $j('#homebanner div.mybanner');
	if($j('#homebanner div.mybanner:first').hasClass('active')){
		$j('#tselogo').show(0)
	}
	
	$j('div.mybanner').each(function(){$j('li:first',this).addClass('active').fadeIn(500);})
	$j('#banner-nav a.bannerselector').click(function(e){
		if($j(this).hasClass('active')){}else{
			e.preventDefault();
			runIt()
			window.clearInterval(slideit);
			var $curbanner = $j(this).attr("href");
			$allbanner.fadeOut(800).removeClass("active")
			$j('#banner-nav a.bannerselector').removeClass("active")
			$j($curbanner).fadeIn(1000).addClass("active");
			$j(this).addClass('active');
			slideit = setTimeout(slideTime, delay)
			
			if($j('#homebanner div.mybanner:first').hasClass('active')){
				$j('#tselogo').show(0)
			} else {$j('#tselogo').hide(0);}
			window.location = $j(this).attr('href').substr(0,$j(this).attr('href').length-0);
			pauseVid();
		}
	})
	
	$j('#nav, ul.link').hover(function(){
		window.clearInterval(slideit);
	}).mouseleave(function(){
		slideit = setTimeout(slideTime, delay)
	})
					
	$j('a#prev').click(function(e){
		e.preventDefault();
		runIt()
		window.clearInterval(slideit);
		var	 $cur = $j('#homebanner div.active'),
		curm = $j('#banner-nav a.active'),
		lastm = $j('#banner-nav a:last'),
		$last = $j('#homebanner').find('div.mybanner:last');
		var $prev;
		var prevm;
		if ($cur.index() === 1){$prev = $last; prevm = lastm;}else{$prev = $cur.prev(); prevm = curm.parent().prev().find('a');}
		
		$cur.fadeOut(time).removeClass("active")
		$prev.fadeIn(time).addClass("active")
		curm.removeClass('active'); prevm.addClass('active');
		window.location = prevm.attr('href').substr(0,prevm.attr('href').length-0);
		slideit = setTimeout(slideTime, delay)
		
		if($j('#homebanner div.mybanner:first').hasClass('active')){
			$j('#tselogo').show(0)
		} else {$j('#tselogo').hide(0);}
		pauseVid()
	})
	$j('a#next').click(function(e){
		e.preventDefault();	
		runIt()
		window.clearInterval(slideit);
		var	 $cur = $j('#homebanner div.active'),
		curm = $j('#banner-nav a.active'),
		firstm = $j('#banner-nav a:first'),
		$first = $j('#homebanner').find('div.mybanner:first'),
		$isLast = $j('#homebanner div.mybanner').length,
		$next;
		var nextm;
		if ($cur.index() === $isLast){
			$next = $first; nextm = firstm;
		}else{
			$next = $cur.next(); nextm = curm.parent().next().find('a');
		}
		$cur.fadeOut(time).removeClass("active")
		$next.fadeIn(time).addClass("active")
		curm.removeClass('active'); nextm.addClass('active');
		window.location = nextm.attr('href').substr(0,nextm.attr('href').length-0);		
		slideit = setTimeout(slideTime, delay)
		if($j('#homebanner div.mybanner:first').hasClass('active')){
			$j('#tselogo').show(0)
		} else {$j('#tselogo').hide(0);}
		pauseVid()
	})
});	

$j(window).load(function(){
	runIt()
	slideit = setTimeout(slideTime, delay)
})

$j(window).bind('resize', function() {
	runIt()
})
$j("a.play").click(function () {
	var url = $j(this).attr('rel'),
	vheight = 600,
	vwidth = 600;
	$j.fancybox({
		'padding': 7,
		'overlayOpacity': 0.7,
		'autoDimensions': false,
		'width': '+vwidth+',
		'content': '<div style="height:100%"><div class="video-js-box vim-css">' +
					'<video id="example_video_1" class="video-js" width="'+vwidth+'" controls="controls" preload="auto">' +
					'<source src="http://video-js.zencoder.com/oceans-clip.webm" type="video/webm; codecs="vp8, vorbis"" />' +
					  '<!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. -->' +
					  '<object id="flash_fallback_1" class="vjs-flash-fallback" width="640" height="480" type="application/x-shockwave-flash"' +
						'data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">' +
						'<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />' +
						'<param name="allowfullscreen" value="true" />' +
						'<param name="flashvars" value=\'config={"clip":{"url": "'+url+'", "autoPlay":false, "autoBuffering":true}]}\' />' +
						'<!-- Image Fallback. Typically the same as the poster image. -->' +
						'<img src="http://video-js.zencoder.com/oceans-clip.png" width="632" height="15" alt="Poster Image"' +
						 ' title="No video playback capabilities." />' +
					  '</object>' +
					'</video>' +
					'</div></div>',
		'onComplete': function () { $j("#fancybox-inner").css({ 'overflow': 'hidden' }); },
		'onClosed': function () { $j("#fancybox-inner").empty(); }
	});
	return false;
	VideoJS.setupAllWhenReady();
});// fancyvideo

