//slideshow setup and start
var rand_slide = Math.floor(Math.random()*jQuery('#topwide .slide').length)+1;
jQuery('#topwide .slide:nth-child('+rand_slide+')').show().addClass('selected');
jQuery('#slide_nav li:nth-child('+rand_slide+')').addClass('selected');
var slideshow = setInterval('nextSlide()',5000);
function nextSlide() {
    if (jQuery('#topwide .slide.selected').next().length > 0) {
        jQuery('#topwide .slide.selected').fadeOut('slow').removeClass('selected').next().fadeIn('slow').addClass('selected');
        jQuery('#slide_nav li.selected').removeClass('selected').next().addClass('selected');
    } else {
        jQuery('#topwide .slide.selected').fadeOut('slow').removeClass('selected');
        jQuery('#slide_nav li.selected').removeClass('selected');
        jQuery('#topwide .slide:first-child').fadeIn('slow').addClass('selected');
        jQuery('#slide_nav li:first-child').addClass('selected');
    }
}

//slide show events
$(document).ready(function() {
    jQuery('#slide_nav li').mouseover(function(){
        // alert('olci');
        clearInterval(slideshow);
        var slide_num = jQuery('#slide_nav li').index(jQuery(this))+1;
        jQuery(this).addClass('selected').siblings().removeClass('selected');
        jQuery('#topwide .slide:nth-child('+slide_num+')').fadeIn('slow').siblings().fadeOut('slow');
    });
});

//news ticker

var NewsScroller = {

    dotsOff: function(onDot){	// removes the hover state and fades all dots out if not .dot_on
    //if (!onDot); onDot = jQuery('.dot_hover');
    //jQuery('.dot_hover').not('.dot_on').stop(true).removeClass('dot_hover').animate( {opacity: 0.5 }, {queue: false, duration: 270} );
    },

    dotOn: function(onDot){ // fades a dot up
        //onDot.addClass('dot_hover');
        onDot.stop().animate( {
            opacity: 1
        }, {
            queue: false,
            duration: 150
        });
    },

    dotClicked: function(clickDot){
        //clickDot.siblings('.dot_on').removeClass('dot_on').addClass('dot_off');
        //clickDot.addClass('dot_on').removeClass('dot_off'); // sets the dot to the clicked-state, red background
        this.dotsOff();
        this.dotOn(clickDot);
        this.newsScroll(clickDot.data( 'showNews' ));
    },

    newsScrollInit: function() {
        // insert buttons
        // add hover state which clears and restarts the timeout
        // call the setTimeout to trigger the animation

        var thisParent = this;
        jQuery('.homenews').each(function(i) {

            var dotBox = jQuery('<li>'+(i+1)+'</li>').fadeTo(100,100).appendTo('#bottomWideCenter .navlinks ul');

            dotBox.data('showNews', i );
            dotBox.hover( function() {
                thisParent.dotOn(dotBox);
            }, function() {
                thisParent.dotsOff( );
            });
            dotBox.click( function() {
                thisParent.dotClicked( dotBox );
            });
        })

        jQuery('#bottomWideCenter').hover(  function(){
            thisParent.doNews = false;
            window.clearTimeout( thisParent.newsScrolling );
        }, function() {
            thisParent.newsAdvance();
            thisParent.doNews = true;
        })
        this.newsScrollStart();

    },

    newsScrollStart: function() {
        var thisParent = this,
        clickThis = jQuery('#bottomWideCenter .navlinks .dot_on');

        if (clickThis.length < 1)
            clickThis = jQuery('#bottomWideCenter .navlinks .dot_off:last');

        jQuery('#bottomWideCenter .navlinks .dot_off').animate( {
            opacity: 0.5
        }, 1000 ).eq(0).animate( {
            opacity: 0.5
        }, 0, function() {
            thisParent.dotClicked( clickThis );
        })

        this.newsAdvance();
        thisParent.doNews = true;
    },

    newsAdvance: function() {
        var thisParent = this,
        clickMe = jQuery('#bottomWideCenter .navlinks .dot_on').prev();
		
        if ( thisParent.doNews ) {
            if ( clickMe.length > 0 ) {
                this.dotClicked( clickMe );
            } else {
                this.dotClicked( jQuery('.navlinks .dot_off').eq( jQuery('.navlinks .dot_off').length - 1 ) );
            }
        }
		
        window.clearTimeout(thisParent.newsScrolling);
        thisParent.newsScrolling = window.setTimeout( function(){
            thisParent.newsAdvance()
        }, 10000);
    },

    newsScroll: function(i) {
        var thisParent = this;

        var newsWidth = jQuery('#bottomWideCenter').innerWidth(),
        newScrollLeft; // = Math.ceil( jQuery('#newsscroller').parent().scrollLeft()/newsWidth + 6 ) % 5 * newsWidth; // 6 is number of news items + 1

        newScrollLeft = ( jQuery('#newsscroller .homenews').length - 1 - i ) * newsWidth * -1; // reversed since the items appear float: left
        jQuery('#newsscroller').animate( {
            marginLeft: newScrollLeft
        }, {
            queue: false,
            duration: 1250
        });
    }
}

jQuery(function(){
    NewsScroller.newsScrollInit();
});
