/* Lägger till en bild för att illustrera en pågående process */

$(document).ready(function(){

	$('<div id="processing">&nbsp;</div>')
		.ajaxStart(function() { $(this).show(); })
		.ajaxStop(function() { $(this).hide(); })
		.appendTo('#div_menu');
})

/* Lägger till data från en specifik url till en specifik div */

function appendData(url, div)
{
	$.ajax({
		type: "GET",
		url: "php/" + url,
		success: function(resultSet)
		{
			$("div#" + div).html(resultSet)
		}
	});
} 

/* Ändrar attributet för ett element */

function changeAttribute(id, attribute, value)
{
	if(value != 'remove')
		$(id).attr(attribute, value);
	else
		$(id).removeAttr(attribute);
}
        
/* Binder formulär efter att DOM-strukturen har förändrats */

function bindBehaviors(form, div, sida, url) {

	var options = {
		target: 'div#' + div,
		url: url,
		beforeSubmit: showRequest,
		success: showResponse,
		type: 'get',
		data: { page: sida }
	};

 	$(form).ajaxForm(options); 

}

function initCalendar() {

	var x = getElementsByClass(popUpCal.inputClass, document, 'input');
	var y = document.getElementById(popUpCal.calendarId);
	// set the calendar position based on the input position
	for (var i=0; i<x.length; i++) {
		if($(x[i]).attr("readonly") != true)
		{	
			x[i].onfocus = function () {
				popUpCal.selectedMonth = new Date().getMonth();
				popUpCal.selectedYear = new Date().getFullYear();
				setPos(this, y); // setPos(targetObj,moveObj)
				y.style.display = 'block';
				popUpCal.drawCalendar(this); 
				popUpCal.setupLinks(this);
			}
		}
		else
			x[i].onfocus = function () { }
	}
}

function showRequest(formData, jqForm, options) { 
	var queryString = $.param(formData); 
	return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
} 

function addFormField(input_id, div, kalender_user, kalender_typ) {
	var id = document.getElementById(input_id).value;
	$("#" + div).append("<p id='row" + id + "'><label for='txt" + id + "'>Deltagare " + id + " <input type='text' size='40' name='txt[]' id='txt" + id + "'> <a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;' class='lank'>Ta bort</a><p>");

	id = (id - 1) + 2;
	document.getElementById(input_id).value = id;

	id = id-1;
	$('#txt'+id).suggest("php/kalender/return_kalender_boka_personer.php?kalender_user="+kalender_user+"&kalender_typ="+kalender_typ,{
		onSelect: function() {alert("Du valde: " + this.value)}
	});
}

function removeFormField(input_id) {
	$(input_id).remove();
}

/* Specifika funktioner */

function checkDataForm() {

	if($('#koppla_jobb input:radio:checked').length>0 && $('#koppla_jobb input:checkbox:checked').length>0)
	{
		changeAttribute('#button_koppla_jobb', 'disabled', 'remove');
		changeAttribute('#startdatum', 'readonly', 'remove');
		changeAttribute('#slutdatum', 'readonly', 'remove');
		initCalendar();
	}
	else
	{
		changeAttribute('#button_koppla_jobb', 'disabled', 'disabled');
		changeAttribute('#startdatum', 'readonly', 'readonly');
		changeAttribute('#slutdatum', 'readonly', 'readonly');
		initCalendar();
	}

}

/* Popup */

function showDiv(div, pageX, pageY){

	//getting height and width of the div
	var height = $('#' + div).height();
	var width = $('#' + div).width();
	alert(pageY);
	//calculating offset for displaying popup message
	leftVal=Math.max(pageX-(width/2),0)+"px";

	topVal=pageY-height+"px";

	//show the popup message
	$('#' + div).css({left:leftVal,top:topVal}).fadeIn(500);

}

$(function () {
	$('.bubbleInfo').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var info = $('.popup', this).css('opacity', 0);


		$([trigger.get(0), info.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;

				info.css({
					top: -90,
					left: -33,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}

			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});

			}, hideDelay);

			return false;
		});
	});
});

