// JavaScript Document
//Editable OPTIONS
var showThumbs = 5;
var startFade = .35;
var fadeTime = 300;
var charMax = 93;


//VARIABLE CONSTANTS

$(document).ready(function() {
    setupFeeder();

});

//SETUP NEWS FEED(S)
function setupFeeder() {
	//ITERATE THROUGH EACH FEED + EACH FEEDS IMAGES
    $('.feedImages').each(function (f) {

        var theFeed = $(this).parent().parent()

        var totalFeeds = $(this).children('ul').children('li').length;

        if (totalFeeds > 0) {
            changeText(theFeed, 0);
        }

        //$(this).children('ul').children('li').each(function (i) {
        for (var feeds = 0; feeds < totalFeeds; feeds++) {
            //ADD CONTROL FOR EACH IMAGE
            if (feeds != 0) {
                addBox = "<div class='iBox'></div>";
            } else {
                addBox = "<div class='iBox selBox'></div>";
            }

            if (feeds == (totalFeeds - 1)) {
                addBox += "<div class='clear'></div>";
            }
            $(this).parent().siblings('.feedControls').children('.selectInfo').append(addBox);


            //ALPHA IMAGES
            adjustAlpha(theFeed, 0, 0);
        };
    });
	
	
	//CLICK FUNCTIONALITY
	$('.selectInfo').children('.iBox').click(function() {
		var boxIndex = $(this).index();
		var theFeed = $(this).parent().parent().parent();
		
		//CHANGE SELBOX
		$(theFeed).children('.feedControls').children('.selectInfo').children('.selBox').removeClass('selBox');
		$(this).addClass('selBox');
		
		moveFeed(theFeed, boxIndex);
	});
	
	$('.feedImages').children('ul').children('li').click(function() {
		var imageIndex = $(this).index();
		var theFeed = $(this).parent().parent().parent().parent();

		//CHANGE SELBOX
		$(theFeed).children('.feedControls').children('.selectInfo').children('.selBox').removeClass('selBox');
		$(theFeed).children('.feedControls').children('.selectInfo').children('.iBox:eq('+imageIndex+')').addClass('selBox');
		
		moveFeed(theFeed, imageIndex);
	});
	
	//NEXT FUNCTIONALITY
	$('.feedControls .next').click(function() {
		var theFeed = $(this).parent().parent();
		var selectInfo = $(theFeed).children('.feedControls').children('.selectInfo');
		var theIndex = $(selectInfo).children('.iBox').index($(selectInfo).children('.selBox'));
		var totalFeeds = $(selectInfo).children('.iBox').length;
		
		theIndex = theIndex+1;
		if (theIndex >= totalFeeds) {
			theIndex = 0;	
		}
		
		//CHANGE SELBOX
		$(theFeed).children('.feedControls').children('.selectInfo').children('.selBox').removeClass('selBox');
		$(theFeed).children('.feedControls').children('.selectInfo').children('.iBox:eq('+theIndex+')').addClass('selBox');

		moveFeed(theFeed, theIndex);
		
	});
	
}

function moveFeed(targetFeed, posIndx) {
	//alert($(targetFeed).attr('class'));
	newPos = 0 - (posIndx) * 143;
	
	//ANIMATE TO THIS
	$(targetFeed).children('.feeds').children('.feedImages').animate({ left: newPos+'px' }, 400);
	
	//CHANGE TEXT
	changeText(targetFeed, posIndx);
	
	//CHANGE OPATICY FOR ALL TO REFLECT POSITION CHANGE
	adjustAlpha(targetFeed, posIndx, fadeTime);
	
}

function changeText(targetFeed, posIndx) {
	var theImage = $(targetFeed).children('.feeds').children('.feedImages').children('ul').children('li:eq('+posIndx+')').children('img');
	var thePhrase = $(theImage).attr('alt');
	var theLink = $(theImage).next('a').attr('href');
	
	//CUT TEXT AT CHARACTER MAX
	var charTotal = thePhrase.length;
	if (charTotal > charMax) {
		thePhrase = thePhrase.substring(0, charMax);
		thePhrase = thePhrase + "...";
	}
	//alert(thePhrase);
	$(targetFeed).children('.feeds').children('.feedLinkTo').children('a').attr('href', theLink);
	$(targetFeed).children('.feeds').children('.feedText').html(thePhrase);
}

function adjustAlpha(targetFeed, posIndx, animTime) {
		$(targetFeed).children('.feeds').children('.feedImages').children('ul').children('li').each(function (i) {
			//var theAlpha = (showThumbs - i)/showThumbs -.15;
			var theAlpha = startFade / (i - posIndx);
			if (i<=posIndx) {
				theAlpha = 1;
			}
			$(this).children('img').animate({ opacity: theAlpha }, animTime);
			//$(this).children('img').css({ opacity: theAlpha });											 
		});
}
