// JavaScript Document
var BU, highlight_cat, highlights , highlight_cat_links , highlight_prog_links , highlight_cats = [], highlight_width = 245;
var L = location.search.toLowerCase();
var highlight_cat; //L.substring( L.indexOf( 'catid' ) + 6 , L.indexOf( 'catid' ) + 10 );
var imageW = 68, imgH = 68, borderW = 32;
function getHighlights(){//find the highlights, this function is only used for subcategories
	if( xmlHttp.readyState == 4 ){
		getAJAXhighlights(); return; //this is patch code
	
		highlight_cat = current_category;
		try{stopCar(); }catch(e){}
		var results = xmlHttp.responseXML.documentElement;
		var build = false;
		if( programIndex.length == 0 ){ 
			programIndex = xmlHttp.responseXML.documentElement.getElementsByTagName( "P" ); 
			build = true;
		}
		highlights = [];
		highlight_cats = [];
		highlight_cat_links = [];
		highlight_prog_links = [];
		
		var cat = $( results ).find("C[CID=" + highlight_cat + "]");
		//.find("red:contains('true')").parent()
		
		getHighLightsIn( cat[ 0 ] );
		removeDuplicateHighlights();
		buildHighlights();
		if( build ){ 
			buildRightNav();
			buildInstructorList( getCategory( current_category ) ); 
		}
	}
}
function getHighlights_dynamic( results ){//this function is used for a subcategory when it is dynamically loaded
	getAJAXhighlights(); return; //this is patch code
	
	try{stopCar(); }catch(e){}
	highlights = [];
	highlight_cats = [];
	highlight_cat_links = [];
	highlight_prog_links = [];
		
	var cat = $( results ).find("C[CID=" + highlight_cat + "]");
	getHighLightsIn( cat[ 0 ] );
	
	removeDuplicateHighlights();
	buildHighlights();
	if( programIndex.length == 0 ){ 
		buildRightNav();
		buildInstructorList( getCategory( current_category ) ); 
	}
	return;
}
//this function is used once the category has already been found, to return the result set
function getHighLightsIn( results ){try{
	var cat = "" + results.getAttribute( "CID" ) + "";//.textContent;
	var catNode = results;
	//results = results.childNodes;
	results = $( results ).find("H:contains('true')").parent();
	for( var i = 0; i < results.length; i++ ){
		//if( results[ i ].getElementsByTagName( "H" )[0].text == "true" || results[ i ].getElementsByTagName( "H" )[0].textContent == "true" ) {
			highlights.push( results[ i ] );
			highlight_cats.push( results[ i ].parentNode.getAttribute( "CID" ) );
			highlight_prog_links.push( "<a href='programs.aspx?catid=" + catNode.getAttribute( "CID" ) + "'>more " + catNode.getAttribute( "T" ) + " programs</a>" );
			highlight_cat_links.push( "<a href='category.aspx?catid=" + catNode.getAttribute( "CID" ) + "'>see the " + catNode.getAttribute( "T" ) + " section</a>" );
		//}
	}
	}catch(e){
		//alert(e);
	}
}
var curcar = 0;
//sets up the actual carousel html
function buildHighlights(){
	var hL = document.getElementById( "highlights_column" ), start, end, isXlist;
	if( transX != null ){ transX.to = 0; } 
	//hL.style.left = document.getElementById( "highlight_scroller" ).style.left = "0px";
	hL.innerHTML = "";
	highlights = sortHighlights( highlights );
	for( var i = 0; i < highlights.length && i < 12; i++ ){
	try{
		start = getDatefrom( getText( highlights[ i ].getElementsByTagName( "S" )[0] ) );
		var rec = ( highlights[ i ].getElementsByTagName( "RT" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "RT" )[0].text : highlights[ i ].getElementsByTagName( "RT" )[0].textContent;
		
		if( getText( highlights[ i ].getElementsByTagName( "TX" )[0] ) != "" ){
			end = constrain( getText( highlights[ i ].getElementsByTagName( "TX" )[0] ), 45 );
		} else if( getText( highlights[ i ].getElementsByTagName( "E" )[0] ) != "" ){
			end = getDatefrom( getText( highlights[ i ].getElementsByTagName( "E" )[0] ) );
			end = textDay( start.getDay() ) + " " + ( start.getMonth() + 1 ) + "/" + start.getDate() + ( ( rec != "S" ) ? " - " + ( end.getMonth() + 1 ) + "/" + end.getDate() : "" );
		} else { end = textDay( start.getDay() ) + " " + ( start.getMonth() + 1 ) + "/" + start.getDate(); }
		
		var name = stripTags( ( highlights[ i ].getElementsByTagName( "PT" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "PT" )[0].text : highlights[ i ].getElementsByTagName( "PT" )[0].textContent );
		if( highlights[ i ].getElementsByTagName( "D" )[0] != null ){
			var desc = ( highlights[ i ].getElementsByTagName( "D" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "D" )[0].text : highlights[ i ].getElementsByTagName( "D" )[0].textContent;
		} else { var desc = ""; }
		var pid = highlights[ i ].getAttribute( 'PID' );
		isXlist = ( getText( highlights[ i ].getElementsByTagName( "CP" )[0] ).indexOf( highlights[ i ].parentNode.getAttribute( 'T' ) ) == -1 );
		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;'>  <p><a href='javascript:var NW = window.open(\"cat-content.aspx?catID=" + highlight_cats[ i ] + "&progID=" + pid + "\",\"\",\"scrollbars=yes,resizable=yes,width=560,height=650\");' onclick='javascript:urchinTracker(\"/highlight/prog/" +  ( ( isXlist ) ? 'xlist/' : '' ) + "(" + highlight_cats[ i ] + ")" + getText( highlights[ i ].getElementsByTagName( "R" )[0] ) + "\");' title='" + name + "'><img src='dbImages/prog-" + pid + ".jpg' align='left' class='highlight_img' onerror='this.src = \"images/questionmark.png\"' border='0' />" + stripTags( constrain( name , 30 ) ) + "<br />" + ( ( !hasSessions( pid ) ) ? ( end + "<br />" + textTime( start ) + "<br />" ) : "(multiple class sections)" ) + "</a></p></div></div>";}catch(e){}//strip tags from title to avoid tag spillovers, decision made by daniel and tatyana 6/7/10
	}
	var codeB = "";
	carousel_items = [];
	for( var i = 0; i < highlights.length / 2 && i < 6; i++ ){
		name = ( highlights[ i * 2 ].getElementsByTagName( "PT" )[0].text != null ) ? highlights[ i * 2 ].getElementsByTagName( "PT" )[0].text : highlights[ i * 2 ].getElementsByTagName( "PT" )[0].textContent;
		if( highlights[ i * 2 + 1 ] != null ){
			name += " / ";
			name += ( highlights[ i * 2 + 1 ].getElementsByTagName( "PT" )[0].text != null ) ? highlights[ i * 2 + 1 ].getElementsByTagName( "PT" )[0].text : highlights[ i * 2 + 1 ].getElementsByTagName( "PT" )[0].textContent;
		}
		//codeB += "<a href='javascript:void(0);' id='bottomcar" + i + "' class='carousel_btn' onclick='carouselTo(" + i + ");'>&nbsp;</a>";
		codeB += "<img src='images/backgrounds/selection_hover.png' id='bottomcar" + i + "' title='" + stripTags( name.replace( "'" , "&acute;" ) ) + "' alt='" + stripTags( name.replace( "'" , "&acute;" ) ) + "' class='carousel_btn' onclick='stopCar(); carouselTo(" + i + ");' onmouseover='MM_swapImage(this.id, \"\", \"images/backgrounds/selection2.png\", 1);' onmouseout='MM_swapImage(this.id, \"\", \"images/backgrounds/selection_hover.png\", 1);' />";
		carousel_items.push( i );
	}
	if( highlights.length == 0 ){
		//codeB += "<p>There are no highlights in this area at the moment.  Please check again later.</p>";
		document.getElementById( "top_right" ).innerHTML = "<div class='jccheader'>highlights&nbsp;&nbsp;&nbsp;</div><hr><p align='center' style='padding-bottom:10px;'>There are no highlights in this area at the moment.  Please check again later.</p>";
		return;
	}
	
	document.getElementById( "highlights_holder" ).innerHTML += "<div id='bottom_carousel'>" + codeB + "</div><div id='carousel_back' onclick='carouselTo( curcar + ( carousel_items.length - 1 ) )' >&lt;</div><div id='carousel_next' onclick='stopCar(); carouselTo( curcar + 1 )' >&gt;</div>";
	carouselTo( 0 );
	if( highlights.length > 0 ){
		carint = window.setInterval( "carouselTo( curcar + 1 );" , 6000 );
	}
}
//get highlights in a main category
function getHighlightsM(){
	if( xmlHttp.readyState == 4 ){
		getAJAXhighlights(null,"red","red"); return; //this is patch code
	
		highlight_cat = current_category;
		try{stopCar(); }catch(e){}
		var results = xmlHttp.responseXML.documentElement;
		highlights = [];
		highlight_cats = [];
		highlight_cat_links = [];
		highlight_prog_links = [];
		
		getHighLightsInM( results );
		removeDuplicateHighlights();
		buildHighlightsM();
		if( programIndex.length == 0 ){ 
			buildRightNav();
			buildInstructorList( getCategory( current_category ) ); 
		}
					
	}
}
//get highlights in a main category on a dynamic page load
function getHighlightsM_dynamic(results){
	getAJAXhighlights(); return;
	
	try{stopCar(); }catch(e){}
	highlights = [];
	highlight_cats = [];
	highlight_cat_links = [];
	highlight_prog_links = [];
		
	getHighLightsInM( results );
	removeDuplicateHighlights();
	buildHighlightsM();
	if( programIndex.length == 0 ){ 
		buildRightNav();
		buildInstructorList( getCategory( current_category ) ); 
	}
	return;
}
//return the list of highlights in the given categor given a main category
function getHighLightsInM( results ){try{
	var cat = "" + results.getAttribute( "CID" ) + "";
	var catNode = results;
	results = $( results ).find("red:contains('true')").parent();
	for( var i = 0; i < results.length; i++ ){
		//if( results[ i ].getElementsByTagName( "red" )[0].text == "true" || results[ i ].getElementsByTagName( "red" )[0].textContent == "true" ) {
			highlights.push( results[ i ] );
			highlight_cats.push( results[ i ].parentNode.getAttribute( "CID" ) );
			highlight_prog_links.push( "<a href='programs.aspx?catid=" + catNode.getAttribute( "CID" ) + "'>more " + catNode.getAttribute( "T" ) + " programs</a>" );
			highlight_cat_links.push( "<a href='category.aspx?catid=" + catNode.getAttribute( "CID" ) + "'>see the " + catNode.getAttribute( "T" ) + " section</a>" );
		//}
	}
	}catch(e){
		//alert(e);
	}
}
var curcar = 0;
//builds highlights carousel html for a main category
function buildHighlightsM(){	
	var hL = document.getElementById( "highlights_column" ), start, code, isXlist;
	if( transX != null ){ transX.to = 0; } 
	//hL.style.left = document.getElementById( "highlight_scroller" ).style.left = "0px";
	hL.innerHTML = "";
	highlights = sortHighlights( highlights );
	for( var i = 0; i < highlights.length && i < 6; i++ ){
		start = getDatefrom( getText( highlights[ i ].getElementsByTagName( "S" )[0] ) );
		var name = ( ( highlights[ i ].getElementsByTagName( "PT" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "PT" )[0].text : highlights[ i ].getElementsByTagName( "PT" )[0].textContent );
		if( highlights[ i ].getElementsByTagName( "D" )[0] != null ){
			var desc = ( highlights[ i ].getElementsByTagName( "D" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "D" )[0].text : highlights[ i ].getElementsByTagName( "D" )[0].textContent;
		} else { var desc = ""; }
		var pid = highlights[ i ].getAttribute( 'PID' );
		code = "<div style='font-size:10px; width:" + ( 540 ) + "px; position:absolute; left:" + 540 * i + "px; top:-10px' >";
		
		code += "<p>";
		
		isXlist = ( getText( highlights[ i ].getElementsByTagName( "CP" )[0] ).indexOf( highlights[ i ].parentNode.getAttribute( 'T' ) ) == -1 );
		
		code += "<a href='javascript:var NW = window.open(\"cat-content.aspx?catID=" + highlight_cats[ i ] + "&progID=" + pid + "\",\"\",\"scrollbars=yes,resizable=yes,width=560,height=650\");' onclick='javascript:urchinTracker(\"/redtag/prog/" + ( ( isXlist ) ? 'xlist/' : '' ) + "(" + highlight_cats[ i ] + ")" + getText( highlights[ i ].getElementsByTagName( "R" )[0] ) + "\");'>";
		code += "<img src='hLImages/prog-" + pid + ".jpg' align='left' class='highlightM_img' border='0' onerror='this.src=\"images/redtag_default.jpg\";' />";
		code += "</a>";
		
		code += "<div class='jccheaderHL'><a href='javascript:var NW = window.open(\"cat-content.aspx?catID=" + highlight_cats[ i ] + "&progID=" + pid + "\",\"\",\"scrollbars=yes,resizable=yes,width=560,height=650\");' onclick='javascript:urchinTracker(\"/redtag/prog/" + ( ( isXlist ) ? 'xlist/' : '' ) + "(" + highlight_cats[ i ] + ")" + getText( highlights[ i ].getElementsByTagName( "R" )[0] ) + "\");' title='" + name + "' >" + stripTags( constrain( name , 30 ) )  + "</a></div>";//strip tags from title to avoid unending italics, decision by daniel and tatyana 6/7/10
		code += constrain( stripTags( desc ) , 100 ) + "</p>";
		
		code += "</div></div>";
		hL.innerHTML += code;
	}
	var codeB = "";
	carousel_items = [];
	for( var i = 0; i < highlights.length && i < 6; i++ ){
		name = ( highlights[ i ].getElementsByTagName( "PT" )[0].text != null ) ? highlights[ i ].getElementsByTagName( "PT" )[0].text : highlights[ i ].getElementsByTagName( "PT" )[0].textContent;
		//codeB += "<a href='javascript:void(0);' id='bottomcar" + i + "' class='carousel_btn' onclick='carouselTo(" + i + ");'>&nbsp;</a>";
		codeB += "<img src='images/backgrounds/selection_hover.png' id='bottomcar" + i + "' title='" + stripTags( name.replace( "'" , "&acute;" ) ) + "' alt='" + stripTags( name.replace( "'" , "&acute;" ) ) + "' class='carousel_btn' onclick='stopCar(); carouselTo(" + i + ");' onmouseover='MM_swapImage(this.id, \"\", \"images/backgrounds/selection2.png\", 1);' onmouseout='MM_swapImage(this.id, \"\", \"images/backgrounds/selection_hover.png\", 1);' />";
		carousel_items.push( i );
	}
	if( highlights.length == 0 ){
		codeB += "<p>There are no highlights in this area at the moment.  Please check again later.</p>";
	}
	
	document.getElementById( "highlights_holderM" ).innerHTML += "<div id='bottom_carouselM'>" + codeB + "</div><div id='carousel_back' onclick='carouselTo( curcar + ( carousel_items.length - 1 ) )' >&lt;</div><div id='carousel_next' onclick='carouselTo( curcar + 1 )' >&gt;</div>";
	carouselTo( 0 );
	if( highlights.length > 0 ){
		carint = window.setInterval( "carouselTo( curcar + 1 )" , 6000 );
	}
}

function getAJAXhighlights( catid , htype , hstyle ){
	catid = (catid)?catid:current_category;
	htype = (htype)?htype:"H";
	hstyle = (hstyle)?hstyle:"H";
	try{stopCar(); }catch(e){}
	$.ajax({
		   url:"highlights.py?catid=" + catid + "&type=" + htype + "&style=" + hstyle,
		   success:function(data){
			   $("#highlights_holderM, #highlights_holder").html(data);
				//add actionlisteners
				carousel_items = [];
				$(".carousel_btn").each(function(){ 
					var $this = $(this);
					carousel_items.push($this.index()); 
					$this.click(function(){stopCar(); carouselTo($(this).index());});
					$this.mouseover(function(){MM_swapImage(this.id, "", "images/backgrounds/selection2.png", 1);});
					$this.mouseout(function(){MM_swapImage(this.id, "", "images/backgrounds/selection_hover.png", 1);});
				});
				carouselTo( 0 );
				if( $(".carousel_btn").length > 1 ){
					carint = window.setInterval( "carouselTo( curcar + 1 )" , 6000 );
				}
		   }
	});
}
//orders highlights by length
function sortHighlights( items ){
    for (var i = 0; i < items.length; i++) {
        for (var j = i + 1; j < items.length; j++) {
            if ( getText( items[ i ].getElementsByTagName( "S" )[0] ) > getText( items[ j ].getElementsByTagName( "S" )[0] ) ) {
                swap = items[i];
                items[i] = items[j];
                items[j] = swap;
				
                swap = highlight_cats[i];
                highlight_cats[i] = highlight_cats[j];
                highlight_cats[j] = swap;
            }
        }
    }
	return items;
}
//sometimes there are duplicates because of cross listing
function removeDuplicateHighlights(){
	var temp = [], temp2 = [], dup;	
	for( var i = 0; i < highlights.length; i++ ){
		dup = false;
		for( var j = 0; j < temp.length; j++ ){
			if(	highlights[ i ].getAttribute( 'PID' ) == temp[ j ].getAttribute( 'PID' ) ){
				dup = true;
			}
		}
		if( !dup ){
			temp.push( highlights[ i ] );
			temp2.push( highlight_cats[ i ] );
		}
	}
	highlights = temp;
	highlight_cats = temp2;
}
//utility functions
function getHighlightContent( div ){
	for( var i = 0; i < div.childNodes.length; i++ ){
		if( div.childNodes[ i ].className == "highlight_content" ){
			return div.childNodes[ i ];
		}
	}
}
function placeHighlightContent( div ){
	getHighlightContent( div ).style.left = -document.getElementById( "highlights_column" ).offsetLeft - div.offsetLeft + "px";
	document.getElementById( "intro_div" ).style.visibility = 'hidden';
}
//the following two functions are for manual clicks
function highlightsRight(){
	var hL = document.getElementById( 'highlights_column' ), dX = hL.parentNode.offsetWidth - 1; 
	if( dX - hL.offsetLeft < hL.offsetWidth && !isMoving ){ 
		if( 2 * dX - hL.offsetLeft >= hL.offsetWidth ){ document.getElementById( "hL_right" ).style.visibility = "hidden"; }
		transX = XTransformation( hL , hL.offsetLeft - dX , 0 , 20 , 1.6 , 80 , 20 , 'x' ); 
		document.getElementById( "hL_left" ).style.visibility = "visible";
	}
}
function highlightsLeft(){
	var hL = document.getElementById( 'highlights_column' ), dX = hL.parentNode.offsetWidth - 1; 
	if( hL.offsetLeft + dX <= 0  && !isMoving ){ 
		if( hL.offsetLeft + dX >= 0 ){ document.getElementById( "hL_left" ).style.visibility = "hidden"; }
		transX = XTransformation( hL , hL.offsetLeft + dX , 0 , 20 , 1.6 , 80 , 20 , 'x' ); 
		document.getElementById( "hL_right" ).style.visibility = "visible";
	}
}
