
$j(document).ready( function() {
    //////////////////////////////////
    // For header.php

    var toggleButtons = $j('#dropdownmenu .switch, #dropdownmenu2 .switch');

    toggleButtons.click(function () {
        if (this.parentNode.className == 'opened') {
            this.parentNode.className = '';
        } else {
            this.parentNode.className = 'opened';
        }
    });
    
    //////////////////////////////////
    // degrees.php and degree_levels.php
    
    if ($j('#toggle_link').length > 0) {
        $j('#toggle_link').click(function() {

			var toggleBG = false;
		
            var toggle_button = $j('#hidden_text_toggle');
            toggle_button.toggleClass('hidden_text_toggle_hidden hidden_text_toggle_showing');
		
            $j('#hidden_text').slideToggle("fast",function(){
			
				if($j('#colleges .background').length > 0){
					toggleBG = true;			
				}			
			
				if (toggle_button.hasClass('hidden_text_toggle_hidden')) {
					$j("#hidden_text_toggle_msg").text('click to read more');
					$j(window).scrollTop(0);
					if(toggleBG)
						$j("#colleges .background").css('height','274px');
				}
				else {
					$j("#hidden_text_toggle_msg").text('click to hide');
					if(toggleBG)
						$j("#colleges .background").css('height',$j('#hidden_text').height() + 290 + 'px');				

				}				
			});


        });
		

		
    }

    if ($j("a.toggle_link").length > 0) {
        $j("a.toggle_link").click(function() {
            /*
            var container = $j(this).closest(".select_list_left");
            $j(".ext_text", container).slideToggle("slow");
            */
            var extTextContainer = $j(this).closest(".ext_text_toggle").prev();
            extTextContainer.slideToggle("slow");

            var toggle_button = $j(this).children("span");
            toggle_button.toggleClass('hidden_text_toggle_hidden hidden_text_toggle_showing');

            if (toggle_button.hasClass('hidden_text_toggle_hidden')) {
                $j(".click_to_open", toggle_button).text('click to read more');
                $j(window).scrollTop(0);
            }
            else {
                $j(".click_to_open", toggle_button).text('click to hide');
            }
        });
    }

    //////////////////////////////////
    // For articles.php

    var ulElement = $j('.featured_article_slider');
    if (ulElement.length > 0) {
        initFeaturedArticles(ulElement);
    }

});

//////////////////////////////////
// For articles.php

function initFeaturedArticles(ulElement) {

    var liCollection = $j('li', ulElement);
    var liCount = liCollection.length;
    var liWidth = liCollection.outerWidth();

    var divContainer = $j(".featured_article_slider_container");
    var visibleLiCount = Math.floor(divContainer.innerWidth() / liWidth);
    var arrowDiv = $j("#featured_nav_inner");

    var timerId = null;
    var leftmostItem = 1;
    var mouseIsOverRotatingControl = false;
    //var mouseIsOverArrowControl = false;

    updateStatusMessage(0);

    if (liCount <= visibleLiCount) {
        return;
    }

    liCollection.get(0).className = 'selected';

    liCollection.bind('mouseover', function() {
        mouseIsOverRotatingControl = true;

        if (timerId != null) {
            window.clearTimeout(timerId);
            timerId = null;
        }

        liCollection.each( function() {
            this.className = '';
        });

        var ix = liCollection.index(this);
        showAtTop(ix);
    });

    ulElement.bind('mouseout', function() {
        mouseIsOverRotatingControl = false;
        
        if (timerId == null) {
            startFeaturedArticlesTimer(0, 'left');
        }
    });
/*
    arrowDiv.bind('mouseover', function () {
        mouseIsOverArrowControl = true;
    });

    arrowDiv.bind('mouseout', function () {
        mouseIsOverArrowControl = false;
    });
*/
    // The containing <ul> must be wide enough to fit all of its <li>s in a single row.
    ulElement.css('width', (liCount * liWidth) + 'px');

    // Start rotation.

    var scrollDuration = 1750;
    var defaultTimerDelay = 7000;
    
    startFeaturedArticlesTimer(defaultTimerDelay, 'left');

    function startFeaturedArticlesTimer(delay, direction) {
        var func = (direction == 'left') ? rotateFeaturedArticlesLeft : rotateFeaturedArticlesRight;
        timerId = window.setTimeout(func, delay);
    }

    function rotateFeaturedArticlesLeft() {

        if (timerId != null) {
            window.clearTimeout(timerId);
            timerId = null;
        }

        ulElement.animate(
            {left: '-=' + liWidth},
            {
                duration: scrollDuration,
                complete: function() {
                    rotateItemsLeft();
                    ulElement.css('left', 0);
                    var previousLeftmostItem = updateStatusMessage(1);
                    updateSelection(previousLeftmostItem);
                    startFeaturedArticlesTimer(defaultTimerDelay, 'left');
                }
            }
        );
       
    }

    function rotateFeaturedArticlesRight() {

        if (timerId != null) {
            window.clearTimeout(timerId);
            timerId = null;
        }

        ulElement.animate(
            {left: '+=' + liWidth},
            {
                duration: scrollDuration,
                complete: function() {
                    rotateItemsRight();
                    ulElement.css('left', 0);
                    var previousLeftmostItem = updateStatusMessage(-1);
                    updateSelection(previousLeftmostItem);
                    startFeaturedArticlesTimer(defaultTimerDelay, 'left');
                }
            }
        );
    }

    function rotateItemsLeft() {
        var detachedLi = $j('li:first', ulElement).detach();
        ulElement.append(detachedLi);
    }

    function rotateItemsRight() {
        var detachedLi = $j('li:last', ulElement).detach();
        ulElement.prepend(detachedLi);
    }

    function updateStatusMessage(shiftAmount) {
        var previousLeftmostItem = leftmostItem;

        leftmostItem += shiftAmount;

        if (leftmostItem < 1) {
            leftmostItem += liCount;
        }
        else if (leftmostItem > liCount) {
            leftmostItem -= liCount;
        }

        var rightmostItem = leftmostItem + visibleLiCount - 1;
        if (rightmostItem > liCount) {
            rightmostItem -= liCount;
        }

        //var statusMsg = 'Showing ' + leftmostItem + ' - ' + rightmostItem + ' of ' + liCount;
        //$j('#featured_status').text(statusMsg);

        return previousLeftmostItem;
    }

    function updateSelection(previousLeftmostItem) {
        if (!mouseIsOverRotatingControl) {
            liCollection.get(previousLeftmostItem - 1).className = '';
            liCollection.get(leftmostItem - 1).className = 'selected';

            showAtTop(leftmostItem - 1);
        }
    }

    function showAtTop(ix) {
        var div = $j("#featured_articles_html > div").get(ix);
        var innerHtml = $j(div).html();
        $j(".content_featured_left_inner")[0].innerHTML = innerHtml;
    }

    // Set slider control click handler.
    $j('#featured_nav_inner a.arrowleft, #featured_nav_inner a.arrowright').click(function() {

        if (timerId != null) {
            window.clearTimeout(timerId);
            timerId = null;
        }

        if ($j(this).hasClass('arrowleft')) {
            startFeaturedArticlesTimer(0, 'right');
        }
        else {
            startFeaturedArticlesTimer(0, 'left');
        }
        
        return false;
    });
}

//////////////////////////////////
// Other

/* NOT USED 9/13/10
function toggleSection(id){
	var section = $('submenu' + id);

	if(section.style.display == 'block'){
		section.style.display = 'none';
		$('section' + id).style.backgroundPosition = '0px 0px';
		$('expandbox' + id).style.backgroundPosition = '0px 0px';
	}else{
		section.style.display = 'block';
		$('section' + id).style.backgroundPosition = '0px -33px';
		$('expandbox' + id).style.backgroundPosition = '0px -15px';
	}
}
*/

function _getElement(id)
{
	if (document.getElementById && document.getElementById(id)) {
		return document.getElementById(id);
	}
	else if (document.all && document.all[id]) {
		return document.all[id];
	}
	else {
		return false;
	}
}

function setBlockDisplay(id,display_type)
{
	_getElement(id).style.display = display_type;
}

function toggleBlock(id)
{
	if (_getElement(id).style.display == 'none') {
		_getElement(id).style.display = 'block';
		_getElement(id+'pic').src = '/pics/dtable.opened.png';
	}
	else {
		_getElement(id).style.display = 'none';
		_getElement(id+'pic').src = '/pics/dtable.closed.png';
	}
}

function showHideHomePageProgram(program_id)
{
	toggleBlock('details_'+program_id);
	toggleBlock('overview_'+program_id);
}

function addToFavorites(urlAddress,pageName) { 
	if (window.sidebar) {
		window.sidebar.addPanel(pageName,urlAddress,"");
	} else if( window.external ) {
		window.external.AddFavorite(urlAddress,pageName); 
	}
		
	return true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function saveQuestion(question_id,user_id)
{
	
	var url = "/applejacks/ajax.save_question.php";
	var params = "question_id=" + question_id + "&user_id=" + user_id;
	
	var ajax = new Ajax.Request(
		url,
		{
			parameters: params,
			method: "post",
			evalScripts: true
		}
	
	);
	
	alert('Question Saved to your My Profile page');
	
}

// MODIFIED 9/13/10
function voteYes(answer_id,user_id)
{
	var url = "/applejacks/ajax.answer_vote_positive.php";
	var params = "answer_id=" + answer_id + "&user_id=" + user_id;
    
    $j.ajax({
        type: "POST",
        url: url,
        data: params
    });
    
	var rank_count = $j('#rank_span_'+answer_id).html();
	rank_count = parseInt(rank_count);
	rank_count++;
    
	$j('#rank_span_'+answer_id).html(rank_count);
	$j('#vote_span_'+answer_id).html('Thanks for voting!');
	
	return false;
}

// MODIFIED 9/13/10
function voteNo(answer_id,user_id)
{
	var url = "/applejacks/ajax.answer_vote_negative.php";
	var params = "answer_id=" + answer_id + "&user_id=" + user_id;
    
    $j.ajax({
        type: "POST",
        url: url,
        data: params
    });

	var rank_count = $j('#rank_span_'+answer_id).html();
	rank_count = parseInt(rank_count);
	rank_count--;

	$j('#rank_span_'+answer_id).html(rank_count);
	$j('#vote_span_'+answer_id).html('Thanks for voting!');
	
	return false;
}

function loginAlert()
{
	alert('You must log in before you do this.');
	
}

function answerScroll()
{
	
	Effect.ScrollTo('your_answer_box',{offset:-65});
	
}

// MODIFIED 9/13/10
function subscribeToNewsletterOnly() {
	var url = "/applejacks/ajax.subscribe_newsletter_only.php";
	var params = "email=" + $j("#subscribe_email").val();

    $j.ajax({
        type: "POST",
        url: url,
        data: params,
        success: function(serverResponse) {
            if (serverResponse) {
                alert(serverResponse);
            }
            else {
                $j("#subscribe_email").val("Thank you for subscribing.");
            }
        }
    });
}

// MODIFIED 9/13/10
function textCounter(field, countFieldId, maxlimit) {
    if (field.value.length > maxlimit) { // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    }
    else { // otherwise, update 'characters left' counter
        var msg = (maxlimit - field.value.length) + ' characters left';
        $j('#' + countFieldId).html(msg);
    }
}

function showAllArticles(id){
    $j("#content_inner").load(
        "/ajax.articles.php",
        {action: 'display_all', 'cat_id':id}
    );
}

