/* 
 Standard index banner scrolling. Requires jQuery and page_setup.js
 */

jQuery(document).ready(function() {
    initScrollingTeasers('#primary-content .main-teaser', '.standard-teaser');
    $('.teaser-scroller #teasers-scroller-next,.teaser-scroller #teasers-scroller-prev').ifixpng();

});

function initScrollingTeasers(parent_selector, child_selector){
    var teasers = jQuery(parent_selector).find(child_selector);
    var wrapper = jQuery(parent_selector);
		wrapper.addClass('teaser-scroller');
		
    if (teasers.length > 1){
        teasers.wrapAll('<div class="teasers-wrapper"></div>');
        teaserswrapper = jQuery(wrapper.find('.teasers-wrapper'));
        
        //set width and height
        var total_width = 0;
        var max_height = teaserswrapper.height();
        jQuery(teasers).each(function(i){
            var thiswidth = jQuery(this).width();
            
            if((i == 0) || (i == (jQuery(teasers).length-1))){
                thiswidth = thiswidth + 36;
                jQuery(this).css('width',thiswidth + 'px');
            };
            
            total_width += thiswidth + 2;
            var hoverwrapper = jQuery(this).find('.hover-wrapper')
            hoverwrapper.css('height', max_height +'px');
						wrapper.css('height', max_height +'px');
            //hoverwrapper.unbind('mouseover');
            //hoverwrapper.unbind('click');
        });
        teaserswrapper.css('width',total_width+'px');
        
        //some styling stuff
        var bgcolor = wrapper.css('background-color');
        teasers.css('background-color', bgcolor);
        wrapper.css('background-color', '#ffffff');
        
        //Add scrollers
        var prev = jQuery('<a id="teasers-scroller-prev" class="prev" href="#prev" style="height:'+max_height+'px">Previous</a>');
        var next = jQuery('<a id="teasers-scroller-next" class="next" href="#next" style="height:'+max_height+'px">Next</a>');
        teaserswrapper.after(prev);
        teaserswrapper.after(next);
        jQuery('#teasers-scroller-next').click(function(eventObj){
            scrollingTeasersScrollTo(currentTeaserScrollerIndex+1,teasers, '.teaser-scroller ' +child_selector);
            eventObj.preventDefault();
            eventObj.stopPropagation();
        });
        jQuery('#teasers-scroller-prev').click(function(eventObj){
            scrollingTeasersScrollTo(currentTeaserScrollerIndex-1,teasers, '.teaser-scroller ' +child_selector);
            eventObj.preventDefault();
            eventObj.stopPropagation();
        });
        
        //scrolling
        scrollingTeasersScrollTo(0, teasers, '.teaser-scroller ' +child_selector);
    }
}

function scrollingTeasersScrollTo(indexnum, teasers, teasers_selector){
    var selected = jQuery(teasers[indexnum]);
    
    //scrollers
    var scrollers_width = 0;
    if(indexnum == 0){
        jQuery('#teasers-scroller-prev').css('top','-1000px');
        jQuery('#teasers-scroller-next').css('top','0');
        scrollers_width = jQuery('#teasers-scroller-next').width();
    }else if (indexnum == (teasers.length-1)){
        jQuery('#teasers-scroller-prev').css('top','0');
        jQuery('#teasers-scroller-next').css('top','-1000px');
        scrollers_width = jQuery('#teasers-scroller-prev').width();
    }else{
        jQuery('#teasers-scroller-prev').css('top','0');
        jQuery('#teasers-scroller-next').css('top','0');
        scrollers_width = jQuery('#teasers-scroller-prev').width() + jQuery('#teasers-scroller-next').width();
    }
    
    //slide it
		var shift_width = (jQuery(teasers[0]).width()-jQuery('#teasers-scroller-next').width())+2;
    if(indexnum > currentTeaserScrollerIndex){
	      //next
        jQuery('.teaser-scroller .teasers-wrapper').animate({"left": "-=" + shift_width + 'px'}, { queue:true, duration:'200' });
    }else if (indexnum < currentTeaserScrollerIndex){
        //prev
        jQuery('.teaser-scroller .teasers-wrapper').animate({"left": "+=" + shift_width + 'px'}, { queue:true, duration:'200' });
    }
    
    currentTeaserScrollerIndex = indexnum;
}

