// disable text selection of the arrow buttons, because it is ugly if they get selected.
// http://chris-barr.com/entry/disable_text_selection_with_jquery/
$(function(){
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	$('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect'
});


// this code requires JQuery

$(document).ready(function() {
    
    // Activate NEWS slides.
    //console.log(how_many_news_items);
    initialize_slideshow($("#home_news"));
    $('.left_arrow').disableTextSelect();
    $('.right_arrow').disableTextSelect();    

});

function initialize_slideshow(slides_div){
    // controls on homepage news box
    
    // hide all slides except the first
    all_slides = $(slides_div).find(".panel_slide").hide();
    $(all_slides[0]).show();
    //how_many_news_items = items_to_hide.length + 1;
    update_slide_tracker(slides_div);
    // activate right arrow, it uses ".slideshow_container" to figure out what slideshow it's associated with.
    slides_div.find(".right_arrow").click(function(){
        document.momo = (this);
        my_slides_div = $(this).parents(".slideshow_container");
        //console.log(my_slides_div);
        increment_slide_tracker(my_slides_div);
    });
    // activate left arrow, it uses ".slideshow_container" to figure out what slideshow it's associated with.
    slides_div.find(".left_arrow").click(function(){
        document.momo = (this);
        my_slides_div = $(this).parents(".slideshow_container");
        //console.log(my_slides_div);
        decrement_slide_tracker(my_slides_div);
    });
}

function increment_slide_tracker(slides_div){
    all_slides = slides_div.find(".panel_slide")
    how_many_slides = all_slides.length;
    current_slide = slides_div.attr("rel");
    next_slide = Number(current_slide) + 1;
    if (next_slide >= how_many_slides){
        next_slide = 0
    }
    all_slides.hide();
    slides_div.attr("rel",next_slide);
    $(all_slides[next_slide]).show();
    update_slide_tracker(slides_div);
}

function decrement_slide_tracker(slides_div){
    all_slides = slides_div.find(".panel_slide")
    how_many_slides = all_slides.length;
    current_slide = slides_div.attr("rel");
    if (current_slide == 0){
        next_slide = all_slides.length -1;
    } else {
        next_slide = current_slide - 1;
    }
    all_slides.hide();
    slides_div.attr("rel",next_slide);
    $(all_slides[next_slide]).show();
    update_slide_tracker(slides_div);
}

function update_slide_tracker(slides_div){
    currently_active = Number(slides_div.attr("rel")) + 1;
    total = slides_div.find(".panel_slide").length;
    slides_div.find(".controls .arrow_labels").html(currently_active + " of " + total);
}



