$j(document).ready(function(){
    var active, pos, w, newPos;
    if ($j(".nav a.active").length > 0) {
        active = $j(".nav a.active");
        pos = active.position();
        w = active.width();
        newPos = w + pos.left - 18;

         $j("#active_star").css({left:newPos}).show();
    }

    $j(".nav a").hover(
        function(){
            var pos = $j(this).position();
            var w = $j(this).width();
             
            var newPos = w + pos.left + 150;
            //console.log(newPos)
            $j("#star").css({left:newPos}).show();
            var left = parseInt($j("#active_star").css('left'), 10);
            if (left ==  newPos) {
                $j("#active_star").hide();
            }
        }, 
        function(){
           $j("#star").hide();
           var left = parseInt($j("#active_star").css('left'), 10);
           var newPos = parseInt($j("#star").css('left'), 10);
           if (left ==  newPos) {
                $j("#active_star").show();
            }
        }
    )

    if ($j(".nav a.active").length > 0) {
        active = $j(".nav a.active");
        pos = active.position();
        w = active.width();
        newPos = w + pos.left +167;

        $j("#active_star").css({left:newPos}).show();
    }
})

//Sets up scroll panes on any module that requires scrolling page content.  Takes height of container and amount of elements to generate pages.
function setUpScrollPanes() {
	var contentScroller = $j('.scroller').data('totalScrolled', 0);
	$j('.content_module').each(function() {
			rebindDownArrow($j(this)); 
			
			var sectionHeight = parseInt($j(this).find('.section_content').height(), 10);
			var elemHeight = 0;
			$j(this).find('.extraElement').each(function() {
				elemHeight += $j(this).outerHeight( true );
			});
			
			if (elemHeight <= sectionHeight) {
				$j(this).find('.nav_arrow_down').hide().unbind('click');
				$j(this).find('.nav_arrow_up').hide().unbind('click');
			}
				
		});
	
	var disableArrows = contentScroller.filter(function() {
			var content = $j(this).find('.btn_215_100_left');
			var len = $j(content).length;
			var height=$j(content).height();
			if($j(this).height() < len*height) {
				return false;
			} else {
				return true;
			}
		}).parent().parent();
	/*$j(disableArrows).find('.nav_arrow_down').hide().unbind('click');
	$j(disableArrows).find('.nav_arrow_up').hide().unbind('click');*/
	
	$j('.nav_arrow_up').addClass('nav_arrow_up_disabled').data('active', false);
	
}
// helper for setUpScrollPanes() function renables the up arrow if the user clicks down or content is above it.
function rebindUpArrow(selector) {
	$j(selector).find('.nav_arrow_up').data('active', true).click(function() {
			
			var container=$j(this).parent().find('.section_content')
			var scrollAmount = $j(container).height();
			var totalHeight = $j(container).css({'height' : 'auto'}).height();
			$j(container).css({'height' : scrollAmount});
			
			var scrolled = $j(this).parent().find('.scroller').data('totalScrolled') - scrollAmount;
			
			var buttonUp = $j(this).parent().find('.nav_arrow_up');
			var buttonDown = $j(this).parent().find('.nav_arrow_down');
			
			$j(this).parent().find('.section_content').animate({
					marginTop : '+=' + scrollAmount
				}, 600, 'linear', function() {
				}); 
			if(scrolled - scrollAmount < 0) {
				$j(buttonUp).data('active', false).addClass('nav_arrow_up_disabled').unbind('click');
			}
			if($j(buttonDown).data('active') == false) {
				$j(buttonDown).removeClass('nav_arrow_down_disabled');
				rebindDownArrow(selector);
			}
			
			$j(this).parent().find('.scroller').data('totalScrolled', scrolled);
			
		});
	
}

// helper for setUpScrollPanes() function renables the down arrow if the user clicks down or content is below it.
function rebindDownArrow(selector) {
	
	$j(selector).find('.nav_arrow_down').data('active', true).click(function(e) {
			var container=$j(this).parent().find('.section_content')
			var scrollAmount = $j(container).height();
			var totalHeight = $j(container).css({'height' : 'auto'}).height();
			$j(container).css({'height' : scrollAmount});
			var buttonUp = $j(this).parent().find('.nav_arrow_up');
			var buttonDown = $j(this).parent().find('.nav_arrow_down');
			
			var scrollArea = $j(this).parent().find('.scroller');
			
			var scrolled = $j(scrollArea).data('totalScrolled') + scrollAmount;
			
			$j(this).parent().find('.section_content').animate({
					marginTop : '-=' + scrollAmount
				}, 600, 'linear', function() {
				});
			
			if(scrolled + scrollAmount >= totalHeight) {
				$j(buttonDown).data('active', false).addClass('nav_arrow_down_disabled').unbind('click');
			}
			
			if($j(buttonUp).data('active') == false) {
				$j(buttonUp).removeClass('nav_arrow_up_disabled');
				rebindUpArrow(selector);
			}
			$j(this).parent().find('.scroller').data('totalScrolled', scrolled); 
			
		});
}

// Used on guided_search.html to navigate through selections.  Arrows appear/dissapear as req.
function scrollSelections(){
    //container height of holding html element
    var containerHeight=465;	

    $j('.nav_arrow_down').each(function(){
            var currentArrow=$j(this);
            var whatToScroll='.'+$j(currentArrow).parent().attr('class') +'_scroller';	
            var originalHeight=$j(whatToScroll).height();

            var timeOut=0;	
            if(originalHeight<containerHeight){
                    $j(currentArrow).css({'display' : 'none'}).parent().children('.nav_arrow_up').css({'display' : 'none'});
            }
            else{
            //if global iphone var (set in iphoneCheck())is set to false use mousedown otherwise use iphone/ipad functionality
            if(!iphone){
                    $j(currentArrow).mousedown(function(){
                            scrollDown(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
                            });
            }
            else{
                    $j(currentArrow).bind('touchstart', function(){
                            scrollDown(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
                    });			
            }		
            }
    });
    $j('.nav_arrow_up').each(function(){
            var currentArrow=$j(this);
            var whatToScroll='.'+$j(currentArrow).parent().attr('class') +'_scroller';	
            var originalHeight=$j(whatToScroll).height();
            var timeOut=0;	
            $j(currentArrow).addClass('nav_arrow_up_disabled');
            //if global iphone var (set in iphoneCheck())is set to false use mousedown otherwise use iphone/ipad functionality
            if(!iphone){
                    $j(currentArrow).mousedown(function(){
                            scrollUp(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
                            });
            }
            else{
                    $j(currentArrow).bind('touchstart', function(){
                            scrollUp(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut);
                    });			
            }
    });
}
//helper for scrollSelections() function.  Scrolls content up
function scrollUp(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut){
    $j(currentArrow).bind('mouseup mouseleave touchend', function() {
                            clearInterval(timeOut);
            });
            timeOut= setInterval(function(){
            var currentHeight=parseInt($j(whatToScroll).css('top'), 10);
            if(currentHeight<0){
            $j(currentArrow).parent().children('.nav_arrow_down').removeClass('nav_arrow_down_disabled');
            $j(whatToScroll).animate({'top' : '+='+15}, 1);
            }
            else{
                    $j(whatToScroll).stop(true).css({'top' : 0});
                    clearInterval(timeOut);
                    $j(currentArrow).addClass('nav_arrow_up_disabled');
            }
    }, 10);
}
        
//helper for scrollSelections() function.  Scrolls content down
function scrollDown(currentArrow, whatToScroll, originalHeight, containerHeight, timeOut){	
    $j(currentArrow).bind('mouseup mouseleave touchend', function() {
                            clearInterval(timeOut);
            });
            timeOut= setInterval(function(){
            var currentHeight=parseInt($j(whatToScroll).css('top'), 10);
            if(currentHeight>(originalHeight-containerHeight)*-1){
            $j(currentArrow).parent().children('.nav_arrow_up').removeClass('nav_arrow_up_disabled');
            $j(whatToScroll).animate({'top' : '-='+15}, 1);
            }
            else{
                    $j(whatToScroll).stop(true).css({'top' : (originalHeight-containerHeight)*-1});
                    clearInterval(timeOut);
                    $j(currentArrow).addClass('nav_arrow_down_disabled');
            }
    }, 10);
}



function setTheStyleToggle(control, target) {
    var controlDivID = "#"+control;
    var targetDivID = "#"+target;
    
    $j(document).find(controlDivID).click(function(){
        if ($j(document).find(targetDivID).css('display') == "block") {
            $j(document).find(targetDivID).css({'display' : 'none'});
        } else {
            $j(document).find(targetDivID).css({'display' : 'block'});
        }
    });
} 




/* FAQ Accordion scripts*/

var lastOpened = null;
var origHeight = null;
	
String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

$j(document).ready(function(){
    origHeight = $j('.item').outerHeight();
    //console.log(origHeight)
    $j('.btn img[alt="Expand"]').live('click', function(){
        if (lastOpened!=null) {
            swapImg(lastOpened, "expand");
            accHide(lastOpened);
	}
	
        //origHeight = $j(this).parent().parent().parent().find(".content").outerHeight() + 28 + 50
        //console.log(origHeight)
        
	swapImg(this, "collapse");
	accShow(this);
			
	lastOpened = this;
    });
		
    $j('.btn img[alt="Collapse"]').live('click', function(){
        swapImg(this, "expand");
        accHide(this);

	lastOpened = null;
    });
});
	
function swapImg(target, mode) {
    var imgSrc = $j(target).attr("src");
    $j(target).attr("src", imgSrc.substring(0,imgSrc.lastIndexOf("-"))+"-"+mode+".png");
    $j(target).attr("alt", mode.capitalize());
}

function accShow(target) {
    var p = $j(target).parent().parent().parent();
    var c = p.find(".content");
    var h = p.height()+c.outerHeight()+parseInt(p.css("marginBottom"));

    p.animate({height:h}, 500);
    c.fadeIn(1000);
}

function accHide(target) {
    var p = $j(target).parent().parent().parent();
    var c = p.find(".content");

    p.animate({height:origHeight}, 300);
    c.fadeOut(300);
}
