/*side bar clicks*/

var cL = null;
var cR = null;
var sP = null;
var	sPW = 1003;



function loginStatus() {
	if(document.getElementById('signIn')) {
		el = document.getElementById('signIn')
		el.style.background = "#69901F url('/img/small_loader.gif') no-repeat center";
		el.style.width = "33px";
		el.firstChild.nodeValue = "\u00A0";
		clearInterval(loginStatusInt);
	}
}

loginStatusInt = setInterval('loginStatus()',10);

function createSideBarClick() {
	
	sP = document.getElementsByTagName('body')[0];

	if(sP) {
	
		cL = document.createElement('a');
		cR = document.createElement('a');
		
		cL.setAttribute('id', 'sideClickLeft');
		cL.setAttribute('href', lL);
		cL.setAttribute('target', lT);
		cR.setAttribute('id', 'sideClickRight');
		cR.setAttribute('href', lR);
		cR.setAttribute('target', lT);
		
		sP.appendChild(cL);
		sP.appendChild(cR);
		
		resizeSideBars();
	}
}

function resizeSideBars() {
	var cW = Math.floor((sP.offsetWidth-sPW)/2)+'px'
	cL.style.width = cW
	cR.style.width = cW
	cL.style.height = sP.clientHeight+'px'
	cR.style.height = sP.clientHeight+'px'
}
/* this function needs to be called post dom load  as it appends Children */
function sideBarClick(linkLeft,linkRight,linkTarget) {
	lL = linkLeft;
	lR = linkRight;
	lT = linkTarget;
	window.onresize = resizeSideBars;
	createSideBarClick();
}
/* end side bar click funcs*/

$('#call_to_action').ready(function(){ 
	
	var full_name = null;
	var group_id = null;
	
	if($.cookie){
	var full_name = $.cookie('CakeCookie[user]');
	var group_id = $.cookie('CakeCookie[group_id]');
	}
	if(full_name !=null){
	full_name = full_name.replace("+"," "); 
	$('#call_to_action > h4').replaceWith('<h4>Welcome '+full_name+'</h4>');

	}

	if(group_id !=null){
	// set last li to signout
	$("a.users-login").replaceWith("<a href='/users/logout/' class='users-login'>Sign Out</a>");
	

	
	if(group_id > 1) {
	$('#call_to_action > p').replaceWith('<p><a href="/admin/">Admin</a></p>');
	} else {
	$('#call_to_action > p').replaceWith('<p><a href="/users/profile">Edit Profile</a></p>');
	}
	} else {
	$("a.users-login").replaceWith("<a href='/users/login' class='users-login'>Sign In</a>");		
	}
});

function initRating()  {

	$('input.star').each(function () { 
		$(this).rating();
	});
		
}


$('div.editPassword').ready(function(){
	
	$('div.editPassword').hide();
	
	$('a.changePassword').click(function () {
			
		$("div.editPassword").slideToggle("slow");
			
		return false;
			
	})
	
});


$(document).ready(function(){ 

	$('#rated_box > h3 > a > img').css('visibility','visible');

	$('a.outlink').attr('target','_blank');

	if($('body').is('#users-login')) {
	
	} else {
	
		$('#flashMessage').fadeOut('10000');
	
	}

	clearInterval(fcInt);

});


function showTakeover() {
	try { 
		enable_takeover();
		clearInterval(updatebg);
	} catch (e) {}
}

$(document).ready(function(){ 
	try { 
		enable_takeover();
	} catch (e) {
		updatebg = setInterval('showTakeover()',5000);
	}
});


function checkCarousel() {
	if($('#featuresCarousel > ul').length) {
		$('#featuresCarousel').css('background',"white url(/img/fc_loader.gif) no-repeat center 37px")
		$('#featuresCarousel > ul > li:eq(0) > h4').css('visibility',"hidden");
		$('#featuresCarousel > ul > li:eq(1) > h4').css('visibility',"hidden");
		$('#featuresCarousel > ul > li:eq(2) > h4').css('visibility',"hidden");
		
		$('.fcTab2').css('visibility',"hidden");
		$('.fcTab3').css('visibility',"hidden");
		
		clearInterval(fcInt);
	}
}

fcInt = setInterval('checkCarousel()',50)

function selectTab(el) {

	var tabNum = el.parent().parent().attr('class');
	
	tabNum = tabNum.substring(tabNum.length-1);
	
	for(k=1;k<4;k++) {
		if(tabNum != k) {
			$('.fcTab'+k).hide();
			$('.fcList'+k+' > h4 > a').removeClass('selected');
		} else {
			el.addClass('selected');
			$('.fcTab'+tabNum).show();
			currentCarousel = '.fcTab'+tabNum;
		}
	}

//	alert(currentCarousel);

}

	var currentCarousel = ".fcTab1";

$('#featuresCarousel').ready(function() {
	if($('#featuresCarousel').length) {
		
		$('#featuresCarousel > ul > li:eq(0) > h4').removeAttr('style')
		$('#featuresCarousel > ul > li:eq(1) > h4').removeAttr('style')
		$('#featuresCarousel > ul > li:eq(2) > h4').removeAttr('style')
		
		$('#featuresCarousel').append('<div class="section"><a class="prev" href="#"> </a> <a class="next" href="#"> </a><ul><li><a class="fc1" href="#">1</a></li><li><a class="fc2" href="#">2</a></li><li><a class="fc3" href="#">3</a></li><li><a class="fc4" href="#">4</a></li></ul></div>');
		
		
		fcTab = new Array();
		
		var pos = 0;
		
		for(i=1;i<4;i++) {
			if($(".fcTab"+i).length) {
				fcTab[i] = $(".fcTab"+i).jCarouselLite({
					btnNext: "#featuresCarousel .section .next",
				    btnPrev: "#featuresCarousel .section .prev",
				    scroll: 3,
					easing: 'easeOutQuad',
					speed: 300,
					visible: 3,
					total:12,
					customHeight:"22em",
					section:"fc",
					id:".fcTab"+i,
					initLink:".fcList"+i+" > h4 > a"
				});
				
				//set tab widths
				if(i>=2) {
					
					pos += $(".fcList"+(i-1)+" > h4").width();
					
					if($.browser.msie && parseInt($.browser.version) == 8) {
					pos += 1;
					}
					
					$(".fcList"+i+" > h4").css('left',pos)
					
				}
			}
		}

		$('.fcTab2').removeAttr('style')
		$('.fcTab3').removeAttr('style')
		
		selectTab($(".fcList1 > h4 > a"));

	}
})


$('#coreTrailers').ready(function() {
	
	if($("#coreTrailers").length) {

	$('#loadingTrailers').hide();
   
	if($("#webtv_trailers-index #coreTrailers").length) {
		
		$('#content_main > h1').hide();
		
			$('#coreTrailers').prepend("<h1>Trailer &amp; Video Highlights</h1>").fadeIn();
			
			
			$('#coreTrailers').append('<a class="prev" href="#"> </a> <a class="next" href="#"> </a><div id="section"> </div> <a class="jcs1" href="#"> </a><a class="jcs2" href="#"> </a><a class="jcs3" href="#"> </a> <a class="jcs4" href="#"> </a><a class="jcs5" href="#"> </a>');

	$("#coreTrailersList").jCarouselLite({
	    btnNext: "#coreTrailers .next",
	    btnPrev: "#coreTrailers .prev",
	    scroll: 4,
		easing: 'easeOutQuad',
		speed: 300,
		visible: 4,
		total:20,
		auto:5000
	
	});
	
 	}

	if($("#webtv_trailers-view #coreTrailers").length) {
		$('#descSharePrint > h2').hide();
		if($('#coreTrailersList > ul').children().size() > 2)
		$('#coreTrailers').append('<a class="prev" href="#"> </a> <a class="next" href="#"> </a>');
		num = $('#coreTrailersList > ul').children().size()
		$('#coreTrailers').prepend("<h2>Related video ("+num+")</h2>").fadeIn();
		$("#coreTrailersList").jCarouselLite({
		    btnNext: "#coreTrailers .next",
		    btnPrev: "#coreTrailers .prev",
		    scroll: 1,
			easing: 'easeOutQuad',
			speed: 300,
			visible: 2
	
		});
	}
	
	if($("#carousels-index #coreTrailers").length) {
		num = $('#coreTrailersList > ul').children().size()
		if(num > 3) {
			$('#coreTrailers').append('<a class="prev" href="#"> </a> <a class="next" href="#"> </a>');
			$('#coreTrailers > h3').append(' ('+num+')');
		}
		
		$("#coreTrailersList").jCarouselLite({
		    btnNext: "#coreTrailers .next",
		    btnPrev: "#coreTrailers .prev",
		    scroll: 1,
			easing: 'easeOutQuad',
			speed: 300,
			visible: 3
	
		});
	}
	
	if($("#products-view #coreTrailers").length) {
		num = $('#coreTrailersList > ul').children().size()
		if(num > 3) {
			$('#coreTrailers').append('<a class="prev" href="#"> </a> <a class="next" href="#"> </a>');
			$('#coreTrailers > h3').append(' ('+num+')');
	
		
		$("#coreTrailersList").jCarouselLite({
		    btnNext: "#coreTrailers .next",
		    btnPrev: "#coreTrailers .prev",
		    scroll: 1,
			easing: 'easeOutQuad',
			speed: 300,
			visible: 3
	
		});
		
		}
		
	}
	
}

});