/**
 * Contains all custom the jQuery functions.
 */

$(document).ready(function() {
	$("#matcher").live({
		click: function() {
			if($('#sil-matcher').length){
				$('#sil-matcher').remove();
			}
			$(this).append('<div id="sil-matcher"></div>');
			getSIL('matcher');
		},
		mouseenter: function(){
			if($('#sil-matcher').length){
				$('#sil-matcher').remove();
			}
			$(this).append('<div id="sil-matcher"></div>');
			getSIL('matcher');
		},
		mouseleave: function(){
			$('#sil-matcher').remove();
		}
	});
	$("#tabell").live({
		click: function() {
			if($('#sil-tabell').length){
				$('#sil-tabell').remove();
			}
			$(this).append('<div id="sil-tabell"></div>');
			getSIL('tabell');
		},
		mouseenter: function(){
			if($('#sil-tabell').length){
				$('#sil-tabell').remove();
			}
			$(this).append('<div id="sil-tabell"></div>');
			getSIL('tabell');
		},
		mouseleave: function(){
			$('#sil-tabell').remove();
		}
	});
	$(".spelare").live({
		click: function() {
			var id = $(this).attr("id");
			if($('.spelarram').length){
				$('.spelarram').remove();
			}
			$(this).append('<div class="spelarram"></div>');
			getPlayer(id);
		},
		mouseenter: function(){
			var id = $(this).attr("id");
			if($('.spelarram').length){
				$('.spelarram').remove();
			}
			$(this).append('<div class="spelarram"></div>');
			getPlayer(id);
		},
		mouseleave: function(){
			$('.spelarram').remove();
		}
	});
});
function getSIL( page ) {
	$.ajax({
		type: "GET",
		url: "include/sil.php",
		data: { section: page },
		datatype: "html",
		beforeSend:function(){
			// this is where we append a loading image
			$('#sil-'+page).append('<div class="loading"><img src="images/pb-loader.gif" alt="Loading..." /></div>');
		},
		success: function(data) {
			// successful request; do something with the data
			$('#sil-'+page).empty();
			$('#sil-'+page).append (data);
		},
		error:function(){
			$('#sil-'+page).append('<p class="error"><strong>Oops!</strong> Något gick fel. Försök igen senare.</p>');
		}
	});
}
function getPlayer ( id ) {
	$.ajax({
		type: "GET",
		url: "include/getplayerinfo.php",
		data: { id: id },
		datatype: "html",
		beforeSend:function(){
			// this is where we append a loading image
			$('.spelarram').append('<div class="loading"><img src="images/pb-loader.gif" alt="Loading..." /></div>');
		},
		success: function(data) {
			// successful request; do something with the data
			$('.spelarram').empty();
			$('.spelarram').append (data);
		},
		error:function(){
			$('.spelarram').append('<p class="error"><strong>Oops!</strong> Något gick fel. Försök igen senare.</p>');
		}
	});
}
