// AJAX

/* Be Fan in MatchBox */
function beFanOf(url, id) {
	showBeFanLoader(id);
	$("#matchBoxFanButtons_" + id).load(url, function(html) {
		hideBeFanLoader(id);
	});
}
function showBeFanLoader(id) {
	$('#matchBoxFanButtons_' + id + ' .beFanLink').hide();
	$('#matchBoxLoader_' + id).show();
}
function hideBeFanLoader(id) {
	$('#matchBoxLoader_' + id).hide();
	// $('#matchBoxFanButtons_' + id).show();
}

// Match card
function matchBeFanOf(url) {
	// $('.beFanButtonShort').hide();
	// $(this).show();
	var myLoader = $(this)
			.append(
					'<img src="/images/ajax-loader-small.gif" class="smallButtonLoader" />');
	$.get(url, function(data) {
		$(this).addClass('selected');
		$(this).click(function() {
			return false;
		});
	});
}

function initmatchBeFanOfButtons() {
	$('.beFanButtonShort')
			.click(
					function() {
						$('.beFanButtonShort').hide();
						$(this).show();
						var myLoader = $(this)
								.append(
										'<img src="/images/ajax-loader-small.gif" class="smallButtonLoader" />');
						$.get($(this).attr('href'), function(data) {
							$('.beFanButtonShort').click(function() {
								return false;
							});
							$('.beFanButtonShort').addClass('selected');
							$('.beFanButtonShort').text('Fandim');
							$('.smallButtonLoader').hide();
						});
						return false;
					});

	$('.betButton').click(function() {
		// $(this).parent().html('<span class="betPlaced
		// tip'+$(this).attr('rel')+'">Můj tip</span>');
			$('.betButton').hide();
			$(this).show();
			$(this).removeClass('betButton');
			$(this).addClass('betPlaced');
			$(this).text('Můj tip');
			showFaceBox($(this).attr('href'));
			return false;
		});
}

function initMatchButtons() {
	// alert('init');
	$('.toCalendar')
			.click(
					function() {
						$(this).css( {
							opacity : 0.5
						});
						var myLoader = $(this)
								.append(
										'<img src="/images/ajax-loader-small.gif" class="smallButtonLoader" />');

						$.get($(this).attr('href'), function(data) {
							jQuery("img", myLoader).hide();
							jQuery("span", myLoader).text("V kalendáři");
							myLoader.css( {
								opacity : 1
							});
							$(this).removeAttr('href');
							$(this).click(function(e) {
								e.preventDefault();
								return false;
								// do other stuff when a click happens
								});
						});
						$(this).click(function() {
							return false;
						});
						return false;
					});

	initMatchInvitations();
	initBets();
}

/* Center script */
jQuery.fn.center = function(absolute) {
	return this.each(function() {
		var t = jQuery(this);

		t.css( {
			position : absolute ? 'absolute' : 'fixed',
			left : '50%',
			top : '30%',
			zIndex : '105'
		}).css( {
			marginLeft : '-' + (t.outerWidth() / 2) + 'px',
			marginTop : '-' + ((t.outerHeight() / 2) - 100) + 'px'
		});

		if (absolute) {
			t.css( {
				marginTop : parseInt(t.css('marginTop'), 10)
						+ jQuery(window).scrollTop(),
				marginLeft : parseInt(t.css('marginLeft'), 10)
						+ jQuery(window).scrollLeft()
			});
		}
	});
};

function initMatchInvitations() {
	$('.inviteFriends').click(function() {
		showFaceBox($(this).attr('href'));
		return false;
	});
}
function initBets() {
	$('.betButton').click(
			function() {
				$(this).parent()
						.html(
								'<span class="betPlaced tip' + $(this).attr(
										'rel') + '">Můj tip</span>');
				showFaceBox($(this).attr('href'));
				return false;
			});
}

function showFaceBox(url) {
	$("#facebox .content")
			.html(
					'<img src="/images/ajax-loader-small.gif" style="margin: 40px auto;text-align: center; display: block;" />');
	$('#facebox').show();
	$('#facebox').center();
	$('#facebox_overlay').show();
	$("#facebox .content").load(url, function(html) {
		// alert('loaded');
			// initEventForm();
		});
	$('#facebox_overlay').click(function() {
		hideFaceBox();
	});
	$('#facebox .close').click(function() {
		hideFaceBox();
	});
}

function hideFaceBox() {
	$('#facebox').hide();
	$('#facebox_overlay').hide();
}

function loadFaceBox(url, params) {
	$("#facebox .content")
			.html(
					'<img src="/images/ajax-loader-small.gif" style="margin: 40px auto;text-align: center; display: block;" />');
	$("#facebox .content").load(url, params, function(html) {
		alert(params);
		// alert('loaded');
			// initEventForm();
			FB.XFBML.parse(document.getElementById('facebox'));
			FB.Canvas.setSize();
		});
}

function initEventForm() {
	$("#facebox .content")
			.html(
					'<img src="/images/ajax-loader-small.gif" style="margin: 40px auto;text-align: center; display: block;" />');
	$.post($('#createEvent').attr('action'), $('#createEvent').serialize(),
			function(data) {
				$('#facebox .content').html(data);
				FB.XFBML.parse(document.getElementById('facebox'));
				FB.Canvas.setSize();
			});
}

function changePage(url) {
	showLoader();
	$("#ajaxContent").load(url, function(html) {
		hideLoader();
		var ele = $("#ajaxContent");
		ele.innerHTML = html;

	});
}
function changePageWithContainer(url) {
	showMatchLoader();
	$("#matchList").load(url, function(html) {
		hideMatchLoader();
		var ele = $("#matchList");
		ele.innerHTML = html;
		FB.XFBML.parse(document.getElementById('matchList'));
		FB.Canvas.setSize();
	});
}
function showLoader() {
	if ($('#slider').length > 0) {
		$('#slider').data('nivo:vars').stop = true;
	}
	$('#contentLoader').show();
}
function hideLoader() {
	$('#contentLoader').hide();
}
function showMatchLoader() {
	$('#matchListLoaderIndicator').show();
}
function hideMatchLoader() {
	$('#matchListLoaderIndicator').hide();
}

function route(url) {
	$.ajax( {
		type : 'POST', // Transfer metod settings (POST, GET)
		url : url, // name of process script
		cache : false, // disable cache, because of actual data ( we don´t wont
						// data from cache)
		data : ( {
			hodnota : $('input[name=neco]').val(),
			id : 0
		}), // transfer attributes with values
		success : function(msg) { // If everything right, result will be in
									// the msg varriable
			// display result in specific DIV element
			$('#right').html(msg);
		}
	});
};
// param illust say that homepage is gallery/illustrations
function routeWithParameter(url, illust) {
	$.ajax( {
		type : 'POST', // Transfer metod settings (POST, GET)
		url : url, // name of process script
		cache : false, // disable cache, because of actual data ( we don´t wont
						// data from cache)
		data : ( {
			hodnota : $('input[name=neco]').val(),
			illust : illust
		}), // transfer attributes with values
		success : function(msg) { // If everything right, result will be in
									// the msg varriable
			// display result in specific DIV element
			$('#right').html(msg);
		}
	});
};

function generatePopup(param) {
	$(document).init(function() {
		$.ajax( {
			type : 'POST', // Transfer metod settings (POST, GET)
			url : 'gallery/popup/', // name of process script
			cache : false, // disable cache, because of actual data ( we don´t
							// wont data from cache)
			data : ( {
				hodnota : $('input[name=neco]').val(),
				id : param
			}), // transfer attributes with values
			success : function(msg) { // If everything right, result will be
										// in the msg varriable
				// display result in specific DIV element
				$('#popup_actuality').html(msg);
			}
		});
	});
};

function generateImagePopup(param,param1) {
	$(document).init(function() {
		$.ajax( {
			type : 'POST', // Transfer metod settings (POST, GET)
			url : 'gallery/imagepopup/', // name of process script
			cache : false, // disable cache, because of actual data ( we don´t
							// wont data from cache)
			data : ( {
				hodnota : $('input[name=neco]').val(),
				id : param,
				id_gal : param1
			}), // transfer attributes with values
			success : function(msg) { // If everything right, result will be
										// in the msg varriable
				// display result in specific DIV element
				$('#popup_image_detail').html(msg);
			}
		});
	});
};
