// JavaScript Document
var BU, highlight_cat, highlights , highlight_cat_links , highlight_cats = [], highlight_width = 260;
//not currently in use - animation of a shape morphing from one size to another
function blowUp( obj , x , y , w , h , div , length , framerate , IDtoOpen ){
	this.sx = obj.offsetLeft;
	this.sy = obj.offsetTop;
	this.fx = x;
	this.fy = y;
	this.w = w;
	this.h = h;
	this.sw = obj.offsetWidth;
	this.sh = obj.offsetHeight;
	this.div = div;
	this.length = length;
	this.framerate = framerate;
	this.IDtoOpen = IDtoOpen;
	
	
	this.getTransformation = function(){
		this.X = [] , this.Y = [] , this.W = [] , this.H = [];
		for( var i = 0; i < this.length; i++ ){
			this.X.push( Math.floor( this.fx + ( this.sx - this.fx ) * ( this.length - ( i + 1 ) ) /this.length ) );
			this.Y[ i ] = Math.floor( this.fy + ( this.sy - this.fy ) * ( this.length - ( i + 1 ) ) /this.length);
			this.W[ i ] = Math.floor( this.sw + ( this.w - this.sw ) * ( this.length - ( i ) )/this.length ) + 1;
			this.H[ i ] = Math.floor( this.sh + ( this.h - this.sh ) * ( this.length - ( i ) )/this.length ) + 1;
		}
		this.X = this.X.reverse();
		this.Y = this.Y.reverse();
		//this.W = this.W.reverse();
		//this.H = this.H.reverse();
	};
	this.run = function(){
		this.div.style.left = this.X.pop() + "px";
		this.div.style.top = this.Y.pop() + "px";
		this.div.style.width = this.W.pop() + "px";
		this.div.style.height = this.H.pop() + "px";
		if( this.X.length > 0 ){
			setTimeout( "BU.run()" , this.framerate );
		}
		else{
			//call finish function
			this.div.style.visibility = "hidden";
			if( this.IDtoOpen == '' ){ return; }
			document.getElementById( this.IDtoOpen ).style.zIndex = 2;
			document.getElementById( this.IDtoOpen ).style.visibility = "visible";
		}
	};
	
	this.getTransformation();
	this.div.style.visibility = "visible";
	this.div.style.zIndex = 1;
	this.run();
}


function getHighlights(){
	if( xmlHttp.readyState == 4 ){
		var results = xmlHttp.responseXML.documentElement.childNodes;
		highlights = [];
		highlight_cats = [];
		highlight_cat_links = [];
		for( var i = 0; i < results.length; i++ ){
			try{
				if( results[ i ].getAttribute( "CategoryID" ) == highlight_cat ){
					//document.getElementById( "highlights_div" ).innerHTML = results[ i ].getAttribute( 'Title' ) + " Highlights";
					getHighLightsIn( results[ i ] );
					buildHighlights();
					return;
				} else { 
				}
			}catch(e){}
		}
	}
}
function getHighLightsIn( results ){try{
	var cat = "" + results.getAttribute( "CategoryID" ) + "";//.textContent;
	var catNode = results;
	results = results.childNodes;
	for( var i = 0; i < results.length; i++ ){
		if( results[ i ].nodeName == "Category" ){
			getHighLightsIn( results[ i ] );
		} else if( results[ i ].nodeName == "Program" && ( results[ i ].getElementsByTagName( "Highlight" )[0].text == "true" || results[ i ].getElementsByTagName( "Highlight" )[0].textContent == "true" ) ) {
			highlights.push( results[ i ] );
			highlight_cats.push( cat );
			highlight_cat_links.push( "<a href='programs.aspx?catid=" + catNode.getAttribute( "CategoryID" ) + "'>more " + catNode.getAttribute( "Title" ) + " programs</a>" );
		}
	}}catch(e){alert(e);}
}
function buildHighlights(){
	var hL = document.getElementById( "highlights_list" );
	if( transX != null ){ transX.to = 0; } 
	hL.style.left = document.getElementById( "highlight_scroller" ).style.left = "0px";
	hL.innerHTML = "";
	
	for( var i = 0; i < highlights.length; i++ ){
	try{
		var name = ( highlights[ i ].getElementsByTagName( "Title" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "Title" )[0].text : highlights[ i ].getElementsByTagName( "Title" )[0].textContent;
		if( highlights[ i ].getElementsByTagName( "Description" )[0] != null ){
			var desc = ( highlights[ i ].getElementsByTagName( "Description" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "Description" )[0].text : highlights[ i ].getElementsByTagName( "Description" )[0].textContent;
		} else { var desc = ""; }
		hL.innerHTML += "<div style='font-size:10px; width:" + ( highlight_width - 0 ) + "px; position:absolute; left:" + highlight_width * i + "px;' ><div style='padding-left:20px; padding-right:20px;'><a href='javascript:var NW = window.open(\"cat-content.aspx?catID=" + highlight_cats[ i ] + "&progID=" + highlights[ i ].getAttribute( 'ProgramID' ) + "\",\"\",\"scrollbars=yes,resizable=yes,width=560,height=650\");'><h3>" + name + "</h3></a><p class='highlight_description'><a href='javascript:var NW = window.open(\"cat-content.aspx?catID=" + highlight_cats[ i ] + "&progID=" + highlights[ i ].getAttribute( 'ProgramID' ) + "\",\"\",\"scrollbars=yes,resizable=yes,width=560,height=650\");'><img src='dbImages/prog-" + highlights[ i ].getAttribute( 'ProgramID' ) + ".jpg' align='left' class='highlight_img' onerror='//this.src = \"images/defaults/" + highlight_cat + ".jpg\"' border='0' /></a>" + desc + "</p><a href='javascript:var NW = window.open(\"cat-content.aspx?catID=" + highlight_cats[ i ] + "&progID=" + highlights[ i ].getAttribute( 'ProgramID' ) + "\",\"\",\"scrollbars=yes,resizable=yes,width=560,height=650\");' class='hProgram_btn'><img src='images/buttons/view_prog.png' border='0' /></a><div class='highlight_link'><hr width='215'>" + highlight_cat_links[ i ] + "</div></div></div>";}catch(e){}
	}
}
//old function for when there was a custom scroll bar on the highlights
function highlightScroll( clientX , clientY , nx , ny ){
	var hL = document.getElementById( "highlights_list" );
	var numMoves = highlights.length - Math.round( 571 / highlight_width );
	var offset = -( nx * numMoves * highlight_width / this.maxX );
	if( Math.abs(  highlight_width * Math.round( offset / highlight_width ) - hL.offsetLeft ) > 20 ){
		//hL.style.left = highlight_width * Math.round( offset / highlight_width ) + "px";
		transX = new XTransformation( hL , highlight_width * Math.round( offset / highlight_width ) , 0 , 2 , 2 , 10 , 20 );
	}
}
//shifts to an even position on mouse up
function highlightEndScroll( clientX , clientY , nx , ny ){
	var hL = document.getElementById( "highlights_list" );
	var scroller = document.getElementById( "highlight_scroller" );
	var numMoves = highlights.length - Math.round( 571 / highlight_width );
	var x = ( transX != null && transX.to != null ) ? transX.to : hL.offsetLeft;
	scroller.x = Math.round( x / highlight_width ) * ( -scroller.maxX / numMoves );
	scroller.style.left = scroller.x + "px";
	//hL.style.left = highlight_width * Math.round( hL.offsetLeft / highlight_width ) + "px";
}
function highlightScrollClick( nx ){
	/*nx -= 270;
	var scroller = document.getElementById( "highlight_scroller" );
	var hL = document.getElementById( "highlights_list" );
	var offset = -( nx * ( highlights.length - 1 ) * highlight_width / scroller.maxX );
	scroller.x = Math.round( nx / highlight_width ) * ( -scroller.maxX / ( highlights.length - 1 ) );
	scroller.style.left = scroller.x + "px";
	if( Math.abs(  highlight_width * Math.round( offset / highlight_width ) - hL.offsetLeft ) > 20 ){
		//hL.style.left = highlight_width * Math.round( offset / highlight_width ) + "px";
		transX = new XTransformation( hL , highlight_width * Math.round( offset / highlight_width ) , 0 , 2 , 2 , 10 , 20 );
	}*/
}
function highlightNext(){
	var hL = document.getElementById( "highlights_list" );
	var scroller = document.getElementById( "highlight_scroller" );
	
	var numMoves = highlights.length - Math.round( 571 / highlight_width );
	var x = ( transX != null && transX.to != null ) ? transX.to : hL.offsetLeft;
	if( x != -numMoves * highlight_width ){
		transX = new XTransformation( hL , Math.max( x - highlight_width , -numMoves * highlight_width ) , 0 , 2 , 2 , 10 , 20 );
	}
	else{
		hL.style.left = "0px";
		transX.to = 0;
	}
	highlightEndScroll();
}
function highlightBack(){
	var hL = document.getElementById( "highlights_list" );
	var scroller = document.getElementById( "highlight_scroller" );
	
	var numMoves = highlights.length - Math.round( 571 / highlight_width );
	var x = ( transX != null && transX.to != null ) ? transX.to : hL.offsetLeft;
	if( x != 0 ){
		transX = new XTransformation( hL , Math.min( highlight_width + x , 0 ) , 0 , 2 , 2 , 10 , 20 );
	}
	else{
		hL.style.left = -numMoves * highlight_width + "px";
		transX.to = -numMoves * highlight_width;
	}
	highlightEndScroll();
}
var carousel_items = [];
var curcar = 0;
var carint;
//for the homepage carousel
function buildCarousel(){
	var carousel = document.getElementById( "carousel_txt" );
	var carousel_pics = document.getElementById( "carousel_pics" ).rows[0].cells;;
	//var sidecar = document.getElementById( "side_carousel" );
	var bottomcar = document.getElementById( "bottom_carousel" );
	var codeS = "", codeB = "";
	
	var today = new Date();
	var rows = $("#carousel_pics div.carousel_div").filter(function(){return this.getAttribute("rel")!=null;});
	var dates = rows.map(function(){return getDatefrom(this.getAttribute("rel"));});
	for( var i = rows.length -1; i >=0; i--){
		if( dates[i] <= today ){
			var r = rows[i].parentNode.cellIndex;
			$("#carousel_pics" )[0].rows[0].deleteCell(r);
			$("#carousel_txt" )[0].rows[0].deleteCell(r);
		}
	}
	
	carousel_items = carousel.rows[0].cells;
	
	/*var carousel_headers = $(".jccheader3 a");
	var header_titles = [];
	carousel_headers.filter( function(){ header_titles = this.innerHTML; } );*/
	$( "div.carousel_div a" ).each( function(){
		var $this = $(this);
		if( $this.attr("href").indexOf("cat-content") > -1 && $this.attr("href").indexOf("open(") < 0 ){
			$this.attr("href","javascript:var NW = window.open(\"" + $this.attr("href") + "\",\"\",\"scrollbars=yes,resizable=yes,width=560,height=650\");");
		}
		$this.click(function(){ urchinTracker( "/highlight/carousel/" + this.rel ); } );
	});
	var carousel_links = [];
	for( var i = 0; i < carousel_items.length; i++ ){
		//codeS += "<div id='sidecar" + i + "' class='carousel_btn' onclick='stopCar(); carouselTo(" + i + ");'>" + carousel_items[ i ].title + "</div>";
		carousel_links = carousel_items[ i ].getElementsByTagName( "A" );
		for( var j = 0; j < carousel_links.length; j++ ) {
			//carousel_links[ j ].rel = "javascript:alert('tracker'); urchinTracker('/highlight/carousel/" + i + "/" + carousel_items[ i ].title + "');";
			carousel_links[ j ].rel = i + "/" + carousel_items[ i ].title;
			//carousel_links[ j ].onclick = function(){alert(this.rel);};
		}
		carousel_links = carousel_pics[ i ].getElementsByTagName( "A" );
		for( var j = 0; j < carousel_links.length; j++ ) {
			carousel_links[ j ].rel = i + "/" + carousel_items[ i ].title;
			//carousel_links[ j ].onclick = function(){alert(this.rel);};//"javascript:alert('tracker'); urchinTracker('/highlight/carousel/" + i + "/" + carousel_items[ i ].title + "');";
		}
		
		codeB += "<img src='images/backgrounds/selection_off.png' id='bottomcar" + i + "' alt='" + carousel_items[ i ].title + "' title='" + carousel_items[ i ].title + "' class='carousel_btn' onclick='stopCar(); carouselTo(" + i + ");' onMouseOver='MM_swapImage(this.id, \"\", \"images/backgrounds/selection_hover.png\", 1);' onMouseOut='MM_swapImage(this.id, \"\", \"images/backgrounds/selection_off.png\", 1);' />";
	}
	//sidecar.innerHTML = codeS;
	bottomcar.innerHTML = codeB;
	//document.getElementById( "sidecar" + curcar ).className = 'carousel_pick';
	document.getElementById( "bottomcar" + curcar ).src = 'images/backgrounds/selection2.png';
	document.getElementById( "bottomcar" + curcar ).onmouseout = function(){ MM_swapImage(this.id, "", "images/backgrounds/selection2.png", 1) };
	carint = window.setInterval( "if( transX == null || !isMoving ){ carouselTo( curcar + 1 ); }" , 6000 );
}
//permanently remove the timer that keeps the carousel showing new items
function stopCar(){
	window.clearInterval( carint ); 
	if( document.getElementById( 'carousel_txt' ) != null ){
		document.getElementById( 'carousel_txt' ).style.backgroundImage = 'url( images/backgrounds/trans3.png )';
	}
}
//slide to position i regardless of current item
function carouselTo( i ){
	var carousel = document.getElementById( "carousel_div" );
	if( carousel_items.length > 0 ){ i = i % carousel_items.length; curcar = curcar % carousel_items.length; }
	if( document.getElementById( "bottomcar" + i ) != null ){
		document.getElementById( "bottomcar" + i ).src = 'images/backgrounds/selection2.png';
		document.getElementById( "bottomcar" + i ).onmouseout = function(){ MM_swapImage(this.id, "", "images/backgrounds/selection2.png", 1) };
	}
	var dist = -577;
	if( carousel == null ){
		carousel = document.getElementById( "highlights_column" );
		dist += 87;
		if( document.getElementById( "highlights_holderM" ) != null ){
			dist -= 50;
		}
	}
	if( i != curcar ){
		clearTimeout( transX.int );
		//if(document.getElementById( "sidecar" + curcar )!= null ){document.getElementById( "sidecar" + curcar ).className = 'carousel_btn';}
		if( location.href.indexOf( "default.aspx" ) != -1 || location.pathname == "/" ){
			document.getElementById( "bottomcar" + curcar ).src = 'images/backgrounds/selection_off.png';
			document.getElementById( "bottomcar" + curcar ).onmouseout = function(){ MM_swapImage(this.id, "", "images/backgrounds/selection_off.png", 1) };
		} else {
			document.getElementById( "bottomcar" + curcar ).src = 'images/backgrounds/selection_hover.png';
			document.getElementById( "bottomcar" + curcar ).onmouseout = function(){ MM_swapImage(this.id, "", "images/backgrounds/selection_hover.png", 1) };
		}
			
		if( ( i == 0 && curcar == carousel_items.length - 1 ) ){// || ( curcar == 0 && i == carousel_items.length - 1 ) ){
			curcar = i;
			//if(document.getElementById( "sidecar" + curcar )!= null ){document.getElementById( "sidecar" + curcar ).className = 'carousel_pick';}
			carousel.style.left = i * dist + "px";			
		} else {
			curcar = i;
			//if(document.getElementById( "sidecar" + curcar )!= null ){document.getElementById( "sidecar" + curcar ).className = 'carousel_pick';}
			transX = new XTransformation( carousel , i * dist , 0 , 2 , 2 , 20 , 20 );
		}
	}
}
