var animatingLeft = false;
var animatingRight = false;
var on = false;
var speed = 1000;
var magicCount = 0;
var rightPage = 0;
var leftPage = 0;    
var totalPages = 0;
var leftCount = 0;
var rightCount = 0;

var imgWidth = 0;
var imgHeight = 0;
var imgX = 0;
var imgY = 0;
var initIt = false;

var onTwo = false;

$(document).ready(function(){
    $('a[rel*=facebox]').facebox(); 
    
    $('#jcrop_target').Jcrop({
        //minSize: [333, 250],
        onChange: showPreview,
        onSelect: showPreview,
        onSelect:    showCoords,
        bgColor:     'black',
        bgOpacity:   .4,
        setSelect:   [ 50, 50, 500, 500 ],
        aspectRatio: 4 / 3
    });

    /*
    $('#linkHolder a').bind('mouseenter', function(){
        itsOn();
    });
    
    $('#linkHolder a').bind('mouseleave', function(){
        itsOff();
        rightVerTwo();
    });
    */
    
    $('.subNav a').live('click', function(){
        $('.subNavOn').removeClass('subNavOn');
        $(this).addClass('subNavOn');
        $('.contentHolder').html($('#'+this.id+'Content').html());
    });
    
	init();
});

function init(){
    totalPages = totalCount / 20; 
	rightPage = Math.floor(totalCount / 20 / 2);
    //rightPage = Math.floor(Math.random()*parseInt(totalPages)) - 1;
    leftPage = Math.floor(totalCount / 20 / 2);
	//getMore('right');
    rightVerTwo();
}

function left(){
	if(animatingLeft == false){
		animatingLeft = true;
		if(parseInt(magicCount) < parseInt(leftCount * -1) + 10){
			getMore('left');
		}
		
		magicCount = magicCount - 1;
		
		on = true;
		$('#picHolder').animate({
			left: '+=333px'	
		}, 
		speed,
        'linear', 
		function(){
			animatingLeft = false;
			if(on == true && animatingRight == false){
				left();
			}
		});
	}
}

function right(){
	if(animatingRight == false){
		animatingRight = true;
        if(parseInt(magicCount) > parseInt(rightCount) - 10){
            getMore('right');
        }

		magicCount = magicCount + 1;
		
		on = true;
		$('#picHolder').animate({
			left: '-=333px'	
		}, 
		speed,
        'linear', 
		function(){
			animatingRight = false;
			if(on == true && animatingLeft == false){
				right();
			}
		});
	}
}

function goLeft(){
    itsOn();
    checkButtons();
    if(parseInt(magicCount) < parseInt(leftCount * -1) + 10){
        getMore('left');
    }
    
    magicCount = magicCount - 4;
    
    $('#picHolder').animate({
        left: '+=1332px'    
    }, 
    speed,
    'linear', 
    function(){

    }); 
}

function goRight(){
    itsOn();
    checkButtons();
    if(parseInt(magicCount) > parseInt(rightCount) - 10){
        getMore('right');
    }

    magicCount = magicCount + 4;
    
    $('#picHolder').animate({
        left: '-=1332px'    
    }, 
    speed,
    'linear', 
    function(){

    });
}

function rightVerTwo(){

        if(parseInt(magicCount) > parseInt(rightCount) - 10){
            getMore('right');
        }

        magicCount = magicCount + 1;
        
        $('#picHolder').animate({
            left: '-=333px'    
        }, 
        speed,
        'linear', 
        function(){
            if(onTwo == false){
                setTimeout("rightVerTwo()", 500);
            }
        });

}

function checkButtons(){
    if($('.pauseButton').is(':visible')){
        $('.playButton').toggle();
        $('.pauseButton').toggle();
    }
}

function itsOn(){
    onTwo = true;
    $('.playButton').toggle();
    $('.pauseButton').toggle();
}

function itsOff(){
    onTwo = false;
    $('.playButton').toggle();
    $('.pauseButton').toggle();
}

function killTimer(){
	on = false;	
}

function getMore(direction){
    if(direction == 'left'){
        var page = parseInt(leftPage) - 1;
        leftPage = page;
        if(leftPage == -1){
            leftPage = totalPages;
            page = totalPages;
        }
        var sort = 'desc';
    }
    else{
        var page = parseInt(rightPage) + 1;
        rightPage = page;
        if(rightPage > totalPages){
            rightPage = 0;
            page = 0;
        }
        var sort = 'asc';
    }

	$.getJSON('http://clicketysplit.com/api2/service/lifeline_timeline_get.json?callback=?&page='+page+'&per_page=20&sort='+sort, function(response){
		html = '';
		for(x in response[0].rows){
			if(direction == 'left'){
				leftCount = leftCount + 1;
				var position = leftCount * 333 * -1; 
			}
			else{
				var position = rightCount * 333;
                rightCount = rightCount + 1;
			}
			var i = response[0].rows[x];
            
            if(i.amount > 0){
                var info = i.name + ' $' + i.amount;
            }
            else{
                var info = i.name;
            }
            
			html += '<div class="thumbs" style="background-image: URL(http://lifelinetohaiti.smi.sh/photos/'+i.cropped_loc+'); left: '+position+'px;"><span>'+info+'</span></div>';	
		}

        if(direction == 'left'){
            $('#picHolder').prepend(html);
            if(initIt == true){
                //cleanUpRight();
            }
        }
        else{
            $('#picHolder').append(html);
            if(initIt == true){
                //cleanUpLeft();
            }
        }
        
        initIt = true; 
        
	});
}

function cleanUpRight(){
    for(var i = 0; i < 20; i++){
        $('.thumbs:last').remove();
        rightCount = rightCount - 1;
    }
}

function cleanUpLeft(){
    for(var i = 0; i < 20; i++){
        $('.thumbs:first').remove();
        leftCount = leftCount - 1;
    }
    
     // add positioning to make up for lost space 
    $('#picHolder').css({
        left: '+=6660px'    
    });
    
    // go thru rest of thumbs and fix their positioning
    $('.thumbs').each(function(){
        $(this).css({
            left: '-=6660px'    
        });
    });
}

function showPreview(coords)
{
    var rx = 333 / coords.w;
    var ry = 250 / coords.h;

    var img = $('#jcrop_target');
    var magicWidth = img.width();
    var magicHeight = img.height();
    
    $('#preview').css({
        width: Math.round(rx * magicWidth) + 'px',
        height: Math.round(ry * magicHeight) + 'px',
        marginLeft: '-' + Math.round(rx * coords.x) + 'px',
        marginTop: '-' + Math.round(ry * coords.y) + 'px'
    });
};

function showCoords(c){
  // variables can be accessed here as
  // c.x, c.y, c.x2, c.y2, c.w, c.h
  imgWidth = c.w;
  imgHeight = c.h;
  imgX = c.x;
  imgY = c.y
};

function cropImg(){
    $.getJSON('http://lifelinetohaiti.smi.sh/jcrop/crop.php?w='+imgWidth+'&h='+imgHeight+'&x='+imgX+'&y='+imgY+'&id='+id, function(response){
        if(response.code < 1){
            var redirectTo = $('#redirect').val();
            redirectTo = redirectTo + '&file=' + response.location + '&fb_loc=' + response.fb_loc;
            window.location = redirectTo;
        }   
        else{
            alert('Error Cropping');
        }
    });
}

function closeFaceBox(){
    itsOff();
    rightVerTwo();
    $(document).trigger('close.facebox');
}
