$(document).ready(function(){
	
	//HOMEPAGE BRANDIMAGE SCROLLER

    jQuery.preloadImages = function() {
        for(var i = 0; i<arguments.length; i++) {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }
   
    var brandimage1 = $('.brandimg').attr('src');
    var brandimage2 = $('.brandimg2').attr('src');
    var brandimage3 = $('.brandimg3').attr('src');
   
    brandlink0 = $('.brandimg').attr('href');
    brandlink1 = $('.brandimg2').attr('href');
    brandlink2 = $('.brandimg3').attr('href');
   
    $.preloadImages(brandimage1, brandimage2, brandimage3);
   
    var imagecount;
    var currentimage = 0;
    var pause = 0;
   
    imagecount = $("#brandmsg .brand").hide().size();
    $("#brandmsg .brand:eq("+ currentimage +")").show();

    $("#brandmsg").append('<div id="controls"><a id="controls-1" href="#"></a><a id="controls-2" href="#"></a><a id="controls-3" href="#"></a></div><a id="pause-btn" href="#"></a>');
   
    rotateInterval = setInterval(feature_rotate, 5000);

    $("#brandmsg #pause-btn").toggle(function(){
        $(this).css({backgroundPosition:'bottom'});
        clearInterval(rotateInterval)
        pause = 1;
    },
    function(){
        $('#controls a').removeAttr('style');
        $(this).css({backgroundPosition:'top'});
        feature_rotate();
        rotateInterval = setInterval(feature_rotate, 5000);
        pause = 0;
    });

    $('#controls-1').click(function(){
        $(this).css({backgroundPosition:'bottom'}).siblings().removeAttr('style');
        if(!pause){$("#brandmsg #pause-btn").click();}
        $('#brandmsg a:animated').stop().dequeue();
        clearInterval(rotateInterval);
        $("#brandmsg .brand:visible:not(.brandimg)").fadeOut(250);
        $("#brandmsg .brandimg").fadeIn(250);
        $("#brandmsg .brandlink").attr("href", brandlink0);
        return false;
    });

    $('#controls-2').click(function(){
        $(this).css({backgroundPosition:'bottom'}).siblings().removeAttr('style');
        if(!pause){$("#brandmsg #pause-btn").click();}
        $('#brandmsg a:animated').stop().dequeue();
        clearInterval(rotateInterval);
        $("#brandmsg .brand:visible:not(.brandimg1)").fadeOut(250);
        $("#brandmsg .brandimg2").fadeIn(250);
        $("#brandmsg .brandlink").attr("href", brandlink1);
        return false;
    });

    $('#controls-3').click(function(){
        $(this).css({backgroundPosition:'bottom'}).siblings().removeAttr('style');
        if(!pause){$("#brandmsg #pause-btn").click();}
        $('#brandmsg a:animated').stop().dequeue();
        clearInterval(rotateInterval);
        $("#brandmsg .brand:visible:not(.brandimg2)").fadeOut(250);
        $("#brandmsg .brandimg3").fadeIn(250);
        $("#brandmsg .brandlink").attr("href", brandlink2);
        return false;
    });

    function feature_rotate() {
            oldimage = currentimage % imagecount;
            newimage = ++currentimage % imagecount;
           
            if (newimage == 0) {
                $("#brandmsg .brandlink").attr("href", brandlink0);
            }
            if (newimage == 1) {
                $("#brandmsg .brandlink").attr("href", brandlink1);
            }
            if (newimage == 2) {
                $("#brandmsg .brandlink").attr("href", brandlink2);
            }

            $("#brandmsg .brand:eq(" + newimage + ")").fadeIn("slow", function() {
                $("#brandmsg .brand:eq(" + oldimage + ")").fadeOut("slow");
            });
    }
   
    // END HOMEPAGE BRANDIMAGE SCROLLER

   
   // MOST POPULAR SCROLLABLE LINKS
	if ( $("#mostpopular").length > 0 ) {
	
	   $('#mostpopscroll').scrollTop(0).height($('#mostpopscroll li:eq(0)').outerHeight() + $('#mostpopscroll li:eq(1)').outerHeight() + $('#mostpopscroll li:eq(2)').outerHeight()); 

	   $('#mostpopular .link').attr("href", "javascript://");
	   $('#mostpopular .imglink').attr("src", "/img/promo_morejs.gif");
	   
	   var listcount = $('#mostpopscroll li').length;

	   $popi = 3;

	   poplock = 0;

	   $('#mostpopular .link').click(function() {
	       if (!(poplock)) {
		       poplock = 1;
		       $('#mostpopular #mostpopscroll').animate({'scrollTop':$('#mostpopscroll li:eq('+$popi+')').offset().top + $('#mostpopscroll').scrollTop() - $('#mostpopscroll').offset().top},function() {
		           $popi += 3;
		           if ($popi >= listcount) {
		               $popi=0;
					   poplock=0;
		           } else {
		               $('#mostpopular #mostpopscroll').animate({"height": ($('#mostpopscroll li:eq('+$popi+')').offset().top-$('#mostpopscroll').offset().top - 6)},function(){
							poplock=0;
						});
		           }
		       });           
		   }	   
	   });
	}

   // END MOST POPULAR SCROLLABLE LINKS 
	
	// PRINT PAGE

	$('#courseinfo-details .printpage').show();
	
	$('#courseinfo-details .printpage').click(function() {
		window.print();
	});
	
	// END PRINT PAGE
	  
	//OPEN NEW WINDOW
	
	$('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
	
	// BROWSE COURSE SLIDE DOWN
	
	$("#browsecourses-promo #promocoursedept").hide();
	$("#browsecourses-promo .clickjs").css("display", "block");
	
	$("#browsecourses li").addClass("arrowd");
	$("#promocoursedept li").addClass("arrowd");
	
	$(".browsesection li").removeClass("arrowd");
	$(".browsesection").hide();
	
	$("#browsecourses-promo .clickjs").click(function() {
		$("#browsecourses-promo .clickjs").hide();
		$("#browsecourses-promo #promocoursedept").slideDown("slow", function() {
			$("#browsecourses-promo img").fadeIn('fast');
		});
	});
	
	$("#browsecourses-promo img").click(function() {
		$("#browsecourses-promo #promocoursedept").slideUp("slow", function() {
			$("#browsecourses-promo .clickjs").fadeIn('fast');
		});
		$("#browsecourses-promo img").hide();
	});
	
	// END BROWSE COURSE SLIDE DOWN
	
	
	// CLEAR INPUT
	
	$("#searchbox input").focus(function(){
		if (this.value == 'keyword(s) or course code') {
			this.value = ''
		}	
	});
	
	$("#sitesearch").focus(function(){
		if (this.value == 'Keyword(s)') {
			this.value = ''
		}	
	});
	
	$("#searchbox input").blur(function(){
		if (this.value == '') {
			this.value = 'keyword(s) or course code'
		}	
	});
	
	$("#sitesearch").blur(function(){
		if (this.value == '') {
			this.value = 'Keyword(s)'
		}	
	});
	
	// END CLEAR INPUT
	
	$(function() {
		$('a[@rel*=lightbox]').lightBox();
	});
	
	// BROWSE COURSE - SHOW SECTIONS
	
	$("#browsecourses li").click(function() {
		$(".open").slideUp('fast');
		$(".open").parent("li").removeClass("arrowu");
		$(".open").parent("li").addClass("arrowd");
		$(".browsesection").removeClass("open");

		$(this).find(".browsesection").addClass("open");
		
		$(this).find(".open").not(":visible").slideDown('slow');
		$(this).removeClass("arrowd");
		$(this).addClass("arrowu");
	});
	
	$("#promocoursedept li").click(function() {
		$(".open").slideUp('fast');
		$(".open").parent("li").removeClass("arrowu");
		$(".open").parent("li").addClass("arrowd");
		$(".browsesection").removeClass("open");

		$(this).find(".browsesection").addClass("open");
		
		$(this).find(".open").not(":visible").slideDown('slow');
		$(this).removeClass("arrowd");
		$(this).addClass("arrowu");
	});
	
	

	// // END BROWSE COURSE - SHOW SECTIONS
	
	// COURSE VIEWS
		// setup dept-section display
		$(".browsecourse-title .arrowd").show();
		$(".browsecourse-title a").css({"backgroundImage":"url(/img/courses/browsetitle_btm_white.gif)"});
		$(".browsecourse").css("background", "none");
		$(".browsecourse-content").hide();
		
		// set first item to display
		//$(".browsecourse:first").css({ "background-color":"#eeeeed", "backgroundImage":"url(/img/407_btm.gif)", "background-position": "bottom left", "background-repeat":"no-repeat" });
		//$(".browsecourse-content:first").show();
		//$(".browsecourse-title .arrowd:first").hide();
		//$(".browsecourse-title .arrowu:first").show();
		
		$(".browsecoursesect:first").css({ "background-color":"#eeeeed", "backgroundImage":"url(/img/407_btm.gif)", "background-position": "bottom left", "background-repeat":"no-repeat" });
		$(".browsecoursesect-content:first").show();
		$(".browsecoursesect-title .arrowd:first").hide();
		$(".browsecoursesect-title .arrowu:first").show();
		
		//on click and reclick do the below
		$(".browsecourse-title").toggle(function () {
			$(".browsecourse").css("background", "none");
			$(this).find("a").css({"backgroundImage":"url(/img/courses/browsetitle_btm.gif)"});
			$(".browsecourse-content").slideUp('medium');
			$(".browsecourse-title .arrowd").show();
			$(".browsecourse-title .arrowu").hide();
			$(this).parent(".browsecourse").css({ "background-color":"#eeeeed", "backgroundImage":"url(/img/407_btm.gif)", "background-position":"bottom left", "background-repeat":"no-repeat" });
			$(this).next(".browsecourse-content").slideDown('slow');
			$(this).find(".arrowd").hide();
			$(this).find(".arrowu").show();
		}, function() {
			$(this).find("a").css({"backgroundImage":"url(/img/courses/browsetitle_btm_white.gif)"});
			$(this).next(".browsecourse-content").slideUp('medium');
			$(this).parent(".browsecourse").css("background", "none");
			$(this).find(".arrowd").show();
			$(this).find(".arrowu").hide();
		});
	// END COURSE VIEW
	
	// SEARCH FILTER
		$("#filter-title .arrowd").show();
		$("#filter").css("background", "none");
		$("#filter form").hide();
		$("#filter .open").show();
		$("#filter .open").parent("#filter").css({ "background-color":"#eeeeed", "backgroundImage":"url(/img/640_btm.gif)", "background-position":"bottom left", "background-repeat":"no-repeat" });
		$(".open .hrbottom").css({ "marginTop":"0px" });
		
		$("#filter-title").toggle(function () {
			$(this).parent("#filter").css({ "background-color":"#eeeeed", "backgroundImage":"url(/img/640_btm.gif)", "background-position":"bottom left", "background-repeat":"no-repeat" });
				$(this).next("#filter form").slideDown('slow');
				$(this).find(".arrowd").hide();
				$(this).find(".arrowu").show();
				$(".hrbottom").css({ "marginTop":"0px" });
			}, function() {
				$(this).next("#filter form").slideUp('fast');
				$(this).parent("#filter form").css("background", "none");
				$(this).find(".arrowd").show();
				$(this).find(".arrowu").hide();
				$(".hrbottom").css({ "marginTop":"-35px" });
		});
	// END SEARCH FILTER
	
	
	
});
