var news_timeout;
var partners_interval;


function attach_home_actions() {

	$("div.featured_img_home a.next").click(function() {
		$("div.featured_img_home a.featured_news.selected").removeClass('selected').parent().next().children('a').trigger('click');		
		if($("div.featured_img_home a.featured_news.selected").length==0) $("div.featured_img_home a.featured_news:first").trigger('click');
		return false;
	});

	$("div.featured_img_home a.back").click(function() {
		$("div.featured_img_home a.featured_news.selected").removeClass('selected').parent().prev().children('a').trigger('click');		
		if($("div.featured_img_home a.featured_news.selected").length==0) $("div.featured_img_home a.featured_news:last").trigger('click');
		return false;
	});
	clearTimeout(news_timeout);
	news_timeout = setTimeout(function() {
		$("div.featured_img_home a.featured_news.selected").removeClass('selected').parent().next().children('a').trigger('click');		
		if($("div.featured_img_home a.featured_news.selected").length==0) $("div.featured_img_home a.featured_news:first").trigger('click');
	}, 5000);	
}

$(function() {
	// to activate splash on homepage
	$("#splash").trigger("click");
	
	if (!($.browser.msie)) {
		$("div#header h1").hide();
		$("div#header .thinkfast").css('opacity', 0);
		$("div#header div.tagline").hide();
		$("div#header .thinkfast").css('left', 760);		
	};
	
	
	$("a.featured_news").click(function() {
		nextimage = new Image();
		var imgsrc = $(this).find('img').attr('src');
		var newimgsrc = imgsrc.substring(0, imgsrc.indexOf('/',2)+1);
		newimgsrc = newimgsrc + '295_295_' + imgsrc.substring(imgsrc.indexOf('_')+1);
		
		nextimage.src = $(this).find('img').attr('src');
		
		var data = $('#article_summary_'+$(this).attr('rel')).html();
		$("div#active_article").hide().html(data).fadeIn(500);
		
		$("a.featured_news").removeClass('selected');
		$(this).addClass('selected');
		attach_home_actions();
		return false;
	}).hover(function() {
		$(".featured_img_home p.caption").hide().html($(this).attr('title')).show();
	}, function() {
		$(".featured_img_home p.caption").hide();
	});
	
	$(".partner-box a.hidden").each(function() {
		$(this).removeClass('hidden').hide();
	});
	
		
	$(".featured_vid_home a.arrow_up").mousedown(function() {
		$('.vid_gallery.vertical_scroll')[0].scrollBy(-250);
	}).click(function() {
		return false;
	});

	$(".featured_vid_home a.arrow_down").mousedown(function() {
		$(".vid_gallery.vertical_scroll")[0].scrollBy(250);
	}).click(function() {
		return false;
	});
	$("ul.tabs").tabs();
	// createPlayer();
	$("div.vid_thumbs a").click(function() {
		player.sendEvent('LOAD', {file:$(this).attr('href'), type:'video'});
		player.sendEvent('PLAY', 'true');
		return false;
	});
	// $("a.gs_promo").click(function() {
	// 	window.location = 'http://www.sjstealth.com/promotions/'
	// });
	setTimeout(function() {
		header_animation();
	}, 1500);
	createPlayer('http://www.sjstealth.com/files/originals/videos/washington_stealth.flv');
});
window.onload = function() {
	$('.vertical_scroll').jScrollPane({showArrows: true});
	attach_home_actions();
	clearInterval(partners_interval);
	partners_interval = setInterval(function() {		
		var selected = $(".partner-box a:visible");
		$(selected).hide();
		if($(selected).next().length) var next = $(selected).next();
		else var next = $(".partner-box a:first");
		$(next).show();
	}, 30000);	
	
	$("div.vid_thumbs").livequery('click', function() {
		nextimage = new Image();
		var imgsrc = $(this).find('img').attr('src');
		var target_vid = $(this).find('img').attr('rel');
		// var newimgsrc = imgsrc.substring(0, imgsrc.indexOf('/',2)+1);
		// newimgsrc = newimgsrc + '432_324_' + imgsrc.substring(imgsrc.indexOf('_gallery')+1);
		
		// $("img.zoom").attr('src', newimgsrc);
		player.sendEvent('LOAD', {file:target_vid, type:'video'});
		player.sendEvent('PLAY', 'true');
		
		$("div.vid_thumbs.selected").removeClass('selected');
		$(this).addClass('selected');
		var caption = $(this).find('p.caption').html();
		$("div.vid1 p.caption").html(caption);
		
		
	}).livequery(function() {
		$(this).hover(function() {
			$(this).find('p.caption').css('color', 'red');
		}, function() {
			$(this).find('p.caption').css('color', 'black');
		});
	});
		
	$("div.featured_vid_home li.next a").livequery('click', function() {
		$("div.vid_thumbs.selected").removeClass('selected').next().trigger('click');		
		if($("div.vid_thumbs.selected").length==0) $("div.vid_thumbs:first").trigger('click');
		return false;
	});

	$("div.featured_vid_home li.back a").livequery('click', function() {
		$("div.vid_thumbs.selected").removeClass('selected').prev().trigger('click');		
		if($("div.vid_thumbs.selected").length==0) $("div.vid_thumbs:last").trigger('click');
		return false;
	});
	
};


function header_animation() {
	if (($.browser.msie)) return;
	setTimeout(function() {
		$("div#header h1").show("puff", {
			percent: 150,
	        origin: ["bottom", "left"] 
		},
		1000);
		setTimeout(function() {
			$("div#header .thinkfast").animate({
				left: 110,
				opacity: 1.0
			}, 500);
			$("div#header div.tagline").fadeIn();			
		}, 500);
	}, 100);
}

// function createPlayer() {
// 	var s1 = new SWFObject('/flash/player.swf','ply','420','250','9','#ffffff');
// 	s1.addParam('allowfullscreen','true');
// 	s1.addParam('allowscriptaccess','always');
// 	s1.addParam('wmode','opaque');
// 	s1.addParam('flashvars','file=/video/jeffdowling.flv');
// 	s1.write('video_holder');
// };