
$(function(){
	startup();
});


function startup(){
	$("#info-message").hide();
	
	$('a[rel*=facebox]').facebox();
	
	$(window).resize(function() {
		resizeBars(true);
	});
	
	resizeBars();
	//updateBars();
	
	$("#about").click(function(){
		$("#info-message").stop().slideToggle();
			setTimeout(function () { 
			$("#info-message").stop().slideUp();
			}, 4000);
	});
	
	$('a.share').click(function(){
		
		
	$(this).parent().find('.share-hover').toggle();
		return false;
	});
	
}


function resizeBars(resize){
	offset = 0;
	w = parseInt($('#wrapper').width() / 100);
	var bottom = -5000;
	
	if(resize){
		bottom = 0;
	}
	
	$('#info-message').css('left',parseInt(($('#wrapper').width()/2)-($('#info-message').width()/2)));
	$('#logo').css('left',parseInt(($('#wrapper').width()/2)-($('#logo').width()/2)));
	$('#facebox').css('left', $(window).width() / 2 - ($('#facebox .content').width() / 2))
	$('#facebox').css('top', $(window).height() / 2 - ($('#facebox .content').height() / 2))
	
	$('.box').each(function(index){
		
		nw = parseInt( w *  $(this).attr('id').split('|')[3]  );
		
		$(this).css({
			'width' : nw,
			'left' : offset,
			'bottom' :bottom
		});
	
		$(this).find('img').width(nw);

		offset += nw;
		
		if(!$.browser.msie){
		
			$(this).css('opacity',$(this).attr('_title'));	
		}
		
		
		if(!resize){
			$(this).animate({ 
		       'bottom' : 0
		      }, 1500 ,'swing',function(){
			//console.log('done');
			});
		}
		
	});
	
	$('#wrapper').height($(document).height());
	
}


function linkBars() {
	$('#bars img').click(function(){
		
	$.post("vote.php",{country_id: $(this).attr('id').split('|')[2] } ,
	
		function(returned_data){
			if(returned_data == 'success'){
			//	console.log('vote');
			}
		});
		
	})
}


function updateBars(){
		$('.box').animate({ 
	       'bottom' : -5000
	      }, 1500 ,'swing',function(){
	//	console.log('done');
 
	
	if(!$.browser.mozilla){
			$.get("./",{},function(data){
				$("#content").html(data);
				startup();
			});
	} else {
		document.location = "./";
	
	}
	
	});	
}

function vote(){
	
	$('#votebutton a').click(function(){
		
		$('#votebutton').fadeOut();
	//	console.log('asdasd');
		$.post("vote.php",{country_id: $(this).attr('id')} ,

			function(returned_data){
				if(returned_data == 'success'){
					$('#votebutton').html('<span class="title">Vote Cast</span>').hide().fadeIn();
					
					setTimeout(function () { 
						$(document).trigger('close.facebox');
					  	updateBars();
					}, 2000);
					
				}	else if('already'){
						$('#votebutton').html('<span class="title" style="font-size: 10px; position: relative; top: -3px;">You just voted for them, come back later or send this to a friend to get them voting</span>').hide().fadeIn();
							setTimeout(function () { 
								$(document).trigger('close.facebox');
							  	
							}, 4000);
				} else {
					$('#warning').remove();
					$('#votebutton a').before('<p id="warning" style="text-align: center; padding-top: 15px">Whoops! Something went wrong, please try again.</p>').hide();
					$('#warning').hide().fadeIn().delay(2000).fadeOut('fast',function(){
						$('#votebutton a').fadeIn();
					});
					
					
				}
			});
			
		
	});
	
}

