var votes = new Array();
var VOTELISTLENGTH=20;

$(document).ready(function(){
	$('.vote-col-b').click(addToBallot);
});

function searchVotes(){
	var searchString = "searchTerm=" + $('#search-input').val();
	
	$.ajax({
		type:"GET",
		url: "/interact/shows/top_20_celebs_gone_good/search.jhtml",
		data: searchString,
		dataType:"html",
		success: function(html){
			$('#dynamic-vote-table').html(html);
			$('.vote-col-b').click(addToBallot);
			
			//disable items again
			for(var n=0; n<votes.length; n++){
				var itemID = votes[n];
				var item = $('#vote-table').find('#' + itemID);
				var btnAdd = item.find('.vote-col-b');
				
				if(item){
					item.find('.vote-item-video').find('a img').attr('src', '/shows/events/hip_hop_honors/_2009/img/vote_camera.png');
					btnAdd.unbind('click',addToBallot);
				}					
			}
		}
	});
}

function clearSearch(){
	$('#search-input').val('');
	//reset
	searchVotes();
}

function addToBallot(){
	var header = $(this).find('.vote-item-header').text();
	var artist = $(this).find('.vote-item-artist').text();
	
	if(header.length >27){
		header =  header.substring(0, 25) + "...";
	}	
	
	var nextPos = votes.length;
	if(nextPos<VOTELISTLENGTH){
		//Change video image src && disable button
		$(this).prev().find('a img').attr('src', '/shows/events/hip_hop_honors/_2009/img/vote_camera.png');		
		$(this).unbind('click',addToBallot);
	
		//Add ID to array				
		votes[nextPos] = $(this).parent().attr('id');
		
		var targetDiv = $('#ballot-item' + (nextPos+1));
		var targetClass = targetDiv.attr('class');
		targetDiv.find('.ballot-item-text').html('<div class="vote-item-header">' + header + '</div><div class="vote-item-artist">' + artist + '</div>');
		
		var btnDelete = targetDiv.find('.ballot-item-delete img');
		btnDelete.show();

		//Drag/Drop
		targetDiv.draggable();
		targetDiv.draggable('option', 'appendTo', '#ballot-col');
		targetDiv.draggable('option', 'axis', 'y');
		targetDiv.draggable('option', 'containment', 'parent');
		targetDiv.draggable('option', 'grid', [0,46]);
		targetDiv.draggable('option', 'cursor', 'pointer');
		targetDiv.draggable('option', 'snap', '.draggable');
		//Must use custom helper instead of clone, to fix firefox 2 problems
		targetDiv.draggable('option', 'helper', function(event){
			var tmpHtml = targetDiv.html();
			return $('<div id="clone" class="' + targetClass + '">' + tmpHtml + '</div>');
		});
		targetDiv.bind('dragstop', move);		
	}
}

function deleteFromBallot(pos){
	//Change video image src && enable button
	var itemID = votes[pos-1];
	var item = $('#vote-table').find('#' + itemID);
	var btnAdd = item.find('.vote-col-b');
	item.find('.vote-item-video').find('a img').attr('src', '/shows/events/hip_hop_honors/_2009/img/vote_camera2.gif');
	btnAdd.bind('click',addToBallot);

	//Move items up
	moveItemsUp(pos,(votes.length+1));
	
	//remove item from array
	var curIndex = pos-1;
	votes.splice(curIndex,1);
}

function showThanks(){
	var msg = $('#submit-error');
	msg.hide();
	
	var strVote = "cmd=tally&style=XMLResults&pollid=100greatest!top20_celebsgone&";
	for(var n=0; n<votes.length; n++){
		var voteID = votes[n].substring(10);
		strVote += 'songlist=' + voteID;		
			
		if(n<(votes.length-1)){
			strVote += "&";
		}		
	}
	
	if(votes.length >0){
		$.ajax({
			type:"GET",
			url: "/shows/events/hip_hop_honors/_2009/vote/sendVote.jhtml",
			data: strVote,
			success: function(res){
				$('#vote-col').hide();
		
				$('#vote-instructions').hide();
				$('#thanks-text').show();
				
				$('#button-container').html('<a href="vote.jhtml"><img src="/interact/shows/top_20_celebs_gone_good/theme/img/vote_again.gif" alt="Submit" /></a>');
				$('.ballot-item-delete img').hide();
			},
			error: function(msg){
				alert("Error: Vote could not be sent.");
			}
		});
	
	} else{
		msg.show();
	}
}

function move(event, ui){
	event.preventDefault();

	var yPos = ui.position.top;
	var curPos = $('.ballot-item1, .ballot-item2').index(this) +1;
	
	var header = $(this).find('.vote-item-header').text();
	var artist = $(this).find('.vote-item-artist').text();
	
	var closestPos = -1;
	var offset = 100;
	
	//Find closest offset
	for(var n=1; n<11; n++){
		var curID = 'ballot-item' + n;
		var curOffsetTop = document.getElementById(curID).offsetTop;
		
		if(Math.abs(yPos - curOffsetTop) < offset)
		{
			closestPos = n;
			offset = Math.abs(yPos - curOffsetTop);
		}		
	}
	
	//Only droppable in a currently occupied position
	if(closestPos != -1 && closestPos <= votes.length){
		var closestDiv = $('#ballot-item' + closestPos);
		var tmp1 = new Array();
		var tmp2 = new Array();
		var tmp3 = [votes[curPos-1]]
		var tmp4 = new Array();
		if(curPos < closestPos){
			tmp1 = votes.slice(0,(curPos-1));
			tmp2 = moveItemsUp(curPos, closestPos);
			tmp4 = votes.slice(closestPos);
			votes = tmp1.concat(tmp2).concat(tmp3).concat(tmp4);	
		} else if(curPos > closestPos){
			tmp1 = votes.slice(0,(closestPos-1));
			tmp2 = moveItemsDown(closestPos, curPos);
			tmp4 = votes.slice(curPos);
			votes = tmp1.concat(tmp3).concat(tmp2).concat(tmp4);
		}
		
		if(curPos != closestPos){
			closestDiv.find('.ballot-item-text').html('<div class="vote-item-header">' + header + '</div><div class="vote-item-artist">' + artist + '</div>');
		}
	}

}

function moveItemsDown(start,end){
	var items = new Array();
	var pos = 0;
	
	for(var p=end; p>start; p--){
		if(p != 1){
			var curDiv = $('#ballot-item' + p);						
			var prevDiv = $('#ballot-item' + (p-1));
			var newHtml = prevDiv.find('.ballot-item-text').html();
			var valDisplay = prevDiv.find('.ballot-item-delete img').css('display');
			
			curDiv.find('.ballot-item-text').html(newHtml);
			curDiv.find('.ballot-item-delete img').css('display', valDisplay);
			
			if(newHtml.indexOf('EMPTY') != -1){
				curDiv.removeClass('draggable');
				curDiv.unbind('dragstop', move);
			}
			
			//Add To Array
			items[pos] = votes[p-2];
			pos++;
		}
	}
	
	return items.reverse();
}

function moveItemsUp(start,end)
{
	var items = new Array();
	var pos = 0;
	
	for(var x=start; x<end; x++){
		var curDiv = $('#ballot-item' + x);	
		if(x<VOTELISTLENGTH){						
			var nextDiv = $('#ballot-item' + (x+1));
			var newHtml = nextDiv.find('.ballot-item-text').html();
			var valDisplay = nextDiv.find('.ballot-item-delete img').css('display');
			
			curDiv.find('.ballot-item-text').html(newHtml);
			curDiv.find('.ballot-item-delete img').css('display', valDisplay);
			
			if(newHtml.indexOf('EMPTY') != -1){
				curDiv.removeClass('draggable');
				curDiv.unbind('dragstop', move);
			}
		}else{
			curDiv.find('.ballot-item-text').html('<b>(EMPTY)</b>');
			curDiv.find('.ballot-item-delete img').hide();
			curDiv.unbind('dragstop', move);
		}	
		
		//Add To Array
		items[pos] = votes[x];
		pos++;
	}
	
	return items;
}


