/* ---------------------------- 
[Global Javascript]

Project: 	Motlow College
Version:	1.0
Last change:	05/19/11 [templates created, lg]
Assigned to:	Lonnie Griffin [lg]
Primary use:	Web

------------------------------- */

$(document).ready(function() {
	
	//Search 
	 $("#uctop_keywords").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
           	 $("#uctop_btnsearch").click();
            return false;
        } else {
            return true;
        }
    });
	
	//Slideshow
	$('#slides').cycle({
		fx:    'fade', 
    	speed:  500,
		timeout: 7000
	});
	
	//Administrative Images
	$('#administrative-images').cycle({
		fx:    'fade', 
    	speed:  500,
		timeout: 5500,
		pause: 1
	});
	
	//Randomize My Motlow Story
	$('.my-motlow-story').hide();
	$('.my-motlow-story').eq( Math.floor(Math.random()*$( '.my-motlow-story' ).length) ).show();
	
	//Scroller
	$("#btn-up").animate({opacity: "0.5"}, 150);
	var scrollIncrement = 63;
	var scrollHeight = ($('#scrollcontent img').length-3)*scrollIncrement;
	var scrollSpeed = 150;
	$("#btn-up").click(function() {
		$("#btn-down").animate({opacity: "1"}, scrollSpeed);
		if ($("#scrollcontent").position().top < 0 && !$("#scrollcontent").is(":animated")) { 
			$("#scrollcontent").animate({top: "+="+scrollIncrement+"px"});
		};
		if ($("#scrollcontent").position().top == -scrollIncrement) {
			$(this).animate({opacity: "0.5"}, scrollSpeed);
		};
		return false;
	
	});
	
	$("#btn-down").click(function() {
		$("#btn-up").animate({opacity: "1"}, scrollSpeed);
		if ($("#scrollcontent").position().top != -scrollHeight && !$("#scrollcontent").is(":animated")) { 
			$("#scrollcontent").animate({top: "-="+scrollIncrement+"px"});
		};
		if ($("#scrollcontent").position().top == -scrollIncrement) {
			$(this).animate({opacity: "0.5"}, scrollSpeed);
		};
		return false;
	});
	
	$("#scroller a").click(function() {
		$("#slides").cycle("pause");
		var slideNum = $("#scroller a").index(this);
		$("#slides").cycle(slideNum);
		return false;
	});

	//Main dropdowns
	$("#menu a[rel]").mouseover(function() {
		$("#dropdown-our-college,#dropdown-administration,#dropdown-student-resources,#dropdown-athletics,#dropdown-news-and-events").hide();
		if ($(this).attr('rel')) {
			$("#dropdown-"+$(this).attr('rel')).show();
		}
	});
	$("#menu").mouseleave(function() {
		$("#dropdown-our-college,#dropdown-administration,#dropdown-student-resources,#dropdown-athletics,#dropdown-news-and-events").fadeOut("fast");
	});
	
	//Quicklinks dropdown
	$("#dropdown-quicklinks").hide();
	$("#btn-quicklinks").mouseover(function() {
		$("#dropdown-quicklinks").slideDown();
	});
	$("#quicklinks-wrapper").mouseleave(function() {
			$("#dropdown-quicklinks").slideUp();
	});
	
	//Clear search on focus
	$("#search input").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
			this.style.color = "#000000";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			this.style.color = "#666666";
		}
	});
	
	//News & events
	$("#events-wrapper").hide();
	$("#news").addClass("active");
	$("#news").click(function() {
	if (!$(this).hasClass("active")) {
		$("#events-wrapper").hide();
		$("#news-wrapper").slideUp();
		$("#news-wrapper > .entry").hide().fadeIn();
		$("#news-wrapper").slideDown();	
		$("#news").addClass("active");
		$("#events").removeClass("active");
	}
	return false;
	});
	$("#events").click(function() {
	if (!$(this).hasClass("active")) {
		$("#news-wrapper").hide();
		$("#events-wrapper").slideUp();
		$("#events-wrapper > .entry-events").hide().fadeIn();
		$("#events-wrapper").slideDown(0);	
		$("#news").removeClass("active");
		$("#events").addClass("active");
	}
	return false;
	});
	
	//Preload images for rollover
	$('.js-hover').preload();
	
	//Image rollovers
	$(".js-hover").each(function() {
        if ($(this).attr("src").match(/-on\.(.+)$/i)) {
            $(this).removeClass("js-hover");
        }
    });
    $(".js-hover").hover(function() {
        s = $(this).attr("src").replace(/\.(.+)$/i, "-on.$1");
        $(this).attr("src", s);
    }, function() {
        s = $(this).attr("src").replace(/-on\.(.+)$/i, ".$1");
        $(this).attr("src", s);
	})
	
});
