var animTime = 400;
var hideTimer = null;
var modalURL = null;

function loadIntoAlternativeContainer (url) {
	$.ajax({
	 url: url,
	 type: 'GET',
	 dataType: 'html',
	 cache: false,
	 complete: function (XMLHttpRequest, textStatus) {
		showAlternativeContainer (XMLHttpRequest.responseText, url);
	 }
	});
}

function showAlternativeContainer (data, url) {
	modalURL = url;
	if (hideTimer) {
		clearTimeout (hideTimer);
	}
	$('#modal_bg').fadeIn (animTime);
	$('#modal').html (data);
	$('#modal').addClass ('ajax_success');
	$('#modal_frame').slideDown (animTime, function () {
		focusFirstInputOf ($('#modal') );
	});
}

function hideAlternativeContainer () {
	$('#modal').removeClass ('ajax_success');
	$('#modal_bg').fadeOut (animTime);
	$('#modal_frame').slideUp (animTime, function () {
		$('#modal').html ("");
	});
}

function hideAlternativeContainerDelayed (ms) {
	if (hideTimer) {
		clearTimeout (hideTimer);
	}
	hideTimer = setTimeout ('hideAlternativeContainer()', ms);
}

function focusFirstInputOf (element) {
	var firstInput = $("input:text", element).get (0);
	if (firstInput) {
		firstInput.focus ();
	}
}

function openPopup(page) {
	var popup = window.open (page, 'popup', 'resizable=yes,scrollbars=yes,menu=yes,width=600,height=600,left=100,top=10');
	popup.focus();
}

function hideHidables () {
	$('.hidable').css ('display', 'none');
}

function updateSiteTitle (content) {
	document.title = content + " | tracksandfields.com";
}

function updateVoteCount (track_id) {
	$.ajax({
	 url: '/track/vote_count?id=' + track_id,
	 type: 'GET',
	 dataType: 'html',
	 cache: false,
	 context: $('.vote_count_' + track_id),
	 complete: function (XMLHttpRequest, textStatus) {
		$(this).slideUp (function () {
			$(this).html (XMLHttpRequest.responseText);
			$(this).slideDown ();
		});
	 }
	});
}

function pauseAllPlayers (exceptID) {
	$( 'object' ).each( function() {
		if (typeof( this.pauseSound ) == 'function') {
			this.pauseSound( exceptID );
		}
	});
}

function updateStorageSpaceInfo () {
	$.ajax({
	 url: '/account/storage_info',
	 type: 'GET',
	 dataType: 'html',
	 cache: false,
	 context: $('.storage_info'),
	 complete: function (XMLHttpRequest, textStatus) {
		$(this).html (XMLHttpRequest.responseText);
	 }
	});
}

function updateRequestCounter (type, model_id) {
	$.ajax({
	 url: '/request/' + type + '_count?id=' + model_id,
	 type: 'GET',
	 dataType: 'html',
	 cache: false,
	 context: $('.' + type + '_count_' + model_id),
	 complete: function (XMLHttpRequest, textStatus) {
		$(this).html (XMLHttpRequest.responseText);
	 }
	});
}

/**
* prepareHttpAnchors()
*
* Sorgt dafür, dass externe Links in einem neuen Fenster geöffnet werden
*/
function prepareHttpAnchors() {
	if (document.links) {
		for (var i = 0; i < document.links.length; ++i) {
			var link = document.links[i].href;

			if (link.indexOf("http://") >= 0 && ((link.indexOf("tracksandfields") == -1))) {
				document.links[i].onclick =
					function() {
						var link = this.href;
						var new_window = window.open(link, "New window");
						new_window.focus();
						return false;
					};
			}
		}
	}
}


$(document).ready(function() {
	prepareHttpAnchors();
});
