/// <reference path="jquery-1.4.2.min.js"/>
/// <reference path="jquery.timers.js"/>
/*
@author Dan Gidman (danatcofo at gmail dot com)
released under GNU General Public License v3
http://www.gnu.org/licenses/gpl.html
version 2.4
*/

(function(a){var b="$autoscroll",c="$tAutoscroll",d="$autoscrollInterval",e="$edgeAutoscroll",f="$lastEdgeAutoscroll",g={up:90,left:180,right:360,upleft:135,upright:45,downleft:225,downright:315,leftup:135,rightup:45,leftdown:225,rightdown:315,down:270},h={init:function(c){return this.each(function(){if(k(this))a(this).data(b,n(a.extend(k(this),c)));else{a(this).data(b,n(a.extend({e:false,fr:false},a.fn.autoscroll.defaults.settings,c)));a(this).hover(function(){var b=k(this);if(b){if(b.scroll){j(this);a(this).stop(true)}b.e=true}},function(){var a=k(this);if(a&&a.e){if(a.scroll)i(this);a.e=false}});i(this)}})},destroy:function(){return this.each(function(){if(k(this)){j(this);a(this).stop(true).data(d,null).data(b,null).data(c,null)}})},delay:function(c){return this.each(function(){var d=k(this);if(d&&d.scroll){a(this).autoscroll("pause");a(this).oneTime(c||a.fn.autoscroll.defaults.delay,b,function(){a(this).autoscroll("resume")})}})},fastforward:function(a){return m(this,"f",a)},rewind:function(a){return m(this,"r",a)},pause:function(){return this.each(function(){var b=k(this);if(b&&b.scroll){b.scroll=false;a(this).stop(true);j(this)}})},resume:function(){return this.each(function(){var a=k(this);if(a){a.scroll=true;i(this)}})},reverse:function(){return this.each(function(){var b=k(this);if(b&&(b.direction+=180)>360)b.direction-=360;a(this).stop(true)})},toggle:function(){return this.each(function(){var b=k(this);if(b)if(b.scroll)a(this).autoscroll("pause");else a(this).autoscroll("resume")})},get:function(){return this.each(function(){if(k(this))return k(this)})},addpausesource:function(b){if(typeof b=="undefined")return this;if(!(b instanceof jQuery))if((b=a(b)).length==0)return this;var c=this.selector;b.each(function(){a(this).hover(function(){a(c).each(function(){var b=k(this);if(b){if(b.scroll){j(this);a(this).stop(true)}b.e=true}})},function(){a(c).each(function(){var a=k(this);if(a&&a.e){if(a.scroll)i(this);a.e=false}})})});return this}},i=function(b){a(b).data(d,setInterval(function(){var d,e=k(b);if(e){if(typeof e.onEvaluate=="function")e.onEvaluate.apply(b);if(!r(b)){if(e.scroll&&!e.e&&!e.fr){if((d=a(b).data(c))&&!l(d,e))a(b).stop(true).animate(q(e.step,e.direction,b),1e3,"linear");else a(b).animate(q(e.step,e.direction),1e3,"linear")}}a(b).data(c,a.extend(true,{},e))}},50))},j=function(b){clearInterval(a(b).data(d))},k=function(c){return a(c).data(b)},l=function(a,b){return a.step===b.step&&a.direction===b.direction&&a.scroll===b.scroll},m=function(b,c,d){var e=n(a.extend(a.fn.autoscroll.defaults.ffrw,d));return b.each(function(){var b=k(this);if(b){var d=b.scroll,f=b.direction;b.fr=true;if(c=="r"&&(f+=180)>360)f-=360;if(b.scroll)j(this);a(this).stop(true).animate(q(e.step,f),e.speed,"swing",function(){k(this).fr=false;if(b.scroll)i(this)})}})},n=function(b){if(a.inArray(typeof b.scroll,["undefined","boolean"])<0)a.error("scroll is not a boolean");if(a.inArray(typeof b.step,["undefined","number"])<0&&isNaN(b.step=Number(b.step)))a.error("step is not a valid number");if(b.direction)b.direction=p(b.direction);return b},o=function(a){return a*Math.PI/180},p=function(b){if(typeof b==="string"&&isNaN(Number(b)))if(g[b])b=g[b];else if(b.indexOf("rad")==b.length-1&&!isNaN(b=Number(b.substring(0,b.length-1))))b=b*180/Math.PI;if(isNaN(b=Number(b)))a.error("Invalid direction on jQuery.autoscroll");while(b<0)b+=360;return b},q=function(b,c){var d=Math.round(b*Math.cos(c=o(c))),e=Math.round(b*Math.sin(c))*-1,f=arguments[2];return f&&a(f).length>0?{scrollTop:f.scrollTop+e,scrollLeft:f.scrollLeft+d}:{scrollTop:(e<0?"-=":"+=")+Math.abs(e),scrollLeft:(d<0?"-=":"+=")+Math.abs(d)}},r=function(b){var c=k(b);if(c&&typeof c.onEdge=="function"){var d=a(b).data(e)||{x:null,y:null},g=c.step,h=b.scrollLeft,i=o(c.direction),j=Math.round(g*Math.cos(i)),l=h+j,m=b.scrollTop,n=Math.round(g*Math.sin(i))*-1;nt=m+n,_e=a(b).data(f);if(h>l&&h==0){if(!d.x||d.x!="left")d.x="left"}else if(h<l&&h==b.scrollWidth-b.clientWidth){if(!d.x||d.x!="right")d.x="right"}else d.x=null;if(m>nt&&m==0){if(!d.y||d.y!="top")d.y="top"}else if(m<nt&&m==b.scrollHeight-b.clientHeight){if(!d.y||d.y!="bottom")d.y="bottom"}else d.y=null;try{if(!_e||d.x&&d.x!=_e.x||d.y&&d.y!=_e.y){c.onEdge.apply(b,[d]);return true}}catch(p){throw p}finally{a(b).data(f,a.extend(true,{},d)).data(e,d)}}return false};a.fn.autoscroll=function(b,c){if(h[b])return h[b].apply(this,Array.prototype.slice.call(arguments,1));else if(typeof b==="object"||!b)return h.init.apply(this,arguments);else a.error("Method "+b+" does not exist on jQuery.autoscroll")};a.fn.autoscroll.defaults={settings:{step:50,scroll:true,direction:"down"},delay:5e3,ffrw:{speed:"fast",step:100}}})(jQuery);


//     INIT()
function createTicker() {
    ticker = $("#news-ticker div.news-ticker-wrap");
    
    $(ticker).find('ul').prepend('<li>Apply for an <a href="http://business.fiu.edu/landon/scholarships.cfm">Undergraduate</a> or a <a href="http://business.fiu.edu/chapman/graduate_scholarships.cfm">Graduate</a> Scholarship Today!  <span class="sep">•</span></li>');
    
    
    ticker.fadeIn("slow", function() { // Fade in (only occurs after the first run)
        ticker.addClass("full-opacity"); // force full opacity
    }); 
    ticker.scrollLeft(0);  // Reset scroll position on reload
    ticker.removeClass("full-opacity");  // remove forced full opacity so we can fade in on the next iteration
    
    // Determine the width of all of the lis.  The containing ul is hardcoded with a huge width.
    var tickerContentWidth = 0;
    $('#news-ticker ul.news-ticker-content li').each(function() {
     tickerContentWidth += $(this).outerWidth();
    });
    
   
 ticker.delay(1500).autoscroll({
        direction: "right",
        step: 30,
        onEvaluate: function () {
            // At the end of the tickers content, destroy the instance, and recursively create it again to keep it running forever.
            if (this.scrollLeft > ( tickerContentWidth ) ) {
                ticker.autoscroll("destroy");
                ticker.hide(); // hide it so we can fade it in
                createTicker(); 
            }
        },
        onEdge : function(edge) {
            // Edge detection not usable due to 99999em width of ticker.  Wasnt as reliable as the technique used within onEvaluate anyway.
        }
    });


    $("a.ticker-rewind").click(function () {
        ticker.autoscroll("rewind");
        ticker.addClass("full-opacity"); // fixes issue with ticker being partially opaque when clicking ff/rw buttons during fade in
    });

    $("a.ticker-fastforward").click(function () {
        ticker.autoscroll("fastforward");
        ticker.addClass("full-opacity");
    });
        
}

