$('document').ready( function() {

	// read cookie data and erese it if it's old 
	listData = readVoteData();
	
	if(!isValidData(listData)) {
		resetVoteData();
	} else {
		for (var i = 0; i < listData.votedSongs.length; i++) {
			$('#song_' + listData.votedSongs[i]).find('a[class*="voteButton"]').addClass('voted');
			$('#song_' + listData.votedSongs[i]).find('a[class*="voteButton"]').removeClass('voteButton');
		}
	}
	
	$('.voteable').find('a[class="voteButton"]').click( function() {
		songId = $(this).parent().attr('id').substr(5);
		
		if(!addVote(songId)) {
			$.fn.colorbox({width:"50%", inline:true, href:'#voteCountExceded', open:true}); 
		} else {
			$(this).removeClass('voteButton');
			$(this).addClass('voted');
		}

		
		
		return false;
	});
	
	$('.voteable').find('a[class="voted"]').click( function() {
		return false;
	});
})

function isValidData(listData) {
	if (listData.listId != turboTopNumber || listData.voteDay != getTodayDate()) {
		return false;
	} else {
		return true;
	}
}

function getTodayDate() {
	date = new Date();
	return date.getFullYear() +' '+' '+ (1+date.getUTCMonth()) +' '+' '+ date.getDate()
}

function addVote(songId) {
	listData = readVoteData();
	
	if (listData.votedSongs.length > 5) {
		return false;
	}

	if (listData.votedSongs.length == 1) {
		$.fn.colorbox({width:"50%", inline:true, href:'#firstVotePerDay', open:true}); 
	}

	for (var x = 0; x < listData.votedSongs.length; x++) {
		if (listData.votedSongs[x] == songId)
			return true;
	}
	postData = {
		list: turboTopNumber,
		song: songId,
		checkSession: sessionCheck
	}
	$.post('/action/turbo_top/save_vote', postData, function(data) {
		if (data.voteAdded == true) {
			
		}
	}, "json")

	
	listData.votedSongs.push(songId);
	saveVoteData(listData);
	
	return true;
}

function readVoteData() {
	listId 		= getCookie('turboTopListNumber');
	votedSongs	= getCookie('turboTopVoteData').split(',');
	voteDay		= getCookie('turboTopVoteDate').split(',');
	
	listData = {
		listId: 	listId,
		votedSongs:	votedSongs,
		voteDay:	voteDay
	};
	
	return listData;
}


function saveVoteData(listData) {
	setCookie('turboTopListNumber', listData.listId, 7);
	setCookie('turboTopVoteData', listData.votedSongs, 7);
	setCookie('turboTopVoteDate', getTodayDate(), 7);
}

function resetVoteData() {
	setCookie('turboTopListNumber', turboTopNumber, 7);
	setCookie('turboTopVoteData', '', 7);
	setCookie('turboTopVoteDate', getTodayDate(), 7);
}



function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) 
				c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
