/**
 * Denne filen inneholder all javascript som benyttes i applikasjonen, og som ikke ligger i en dedikert fil.
 */
 

$().ready(function() {

    $.postJSON = function(url, data, callback) {
		$.post(url, data, callback, "json");
	};
	
	
	/**
	 * Tooltip
	 */
	 /*
	var positionTooltip = function(event) {
		var tPosX = event.pageX - 5;
		var tPosY = event.pageY + 20;
		$('div.tooltip').css({top: tPosY, left: tPosX});
	};
	
	
	var showTooltip = function(event) {
		$('div.tooltip').remove();
		$('<div class="tooltip">Litt tekst</div>').appendTo('body');
		positionTooltip(event);
	};
	
	
	var hideTooltip = function() {
        $('div.tooltip').remove();
	};*/
	
	
	//$("a").easyTooltip();
	
	
	
	/*****/
	

	$('#popup').
		jqm({
			overlay: 70, 
			modal: true,
			trigger: false
		}).
		jqmAddClose('#popup input.close');
	
	$('#popup input.yes').
		click(function() { 
			window.location.href = confirmLink;
		});
	
	$('a.confirm').click(function(e) {
		e.preventDefault();
		$('#popup').
		jqmShow();
	});
	
	$('#filterContainer .select').change(function() {
		$('#myForm').ajaxSubmit();
		return false;
	});
	
	$('.text, .password, .inputSearch').focus(function() {
		$(this).addClass('textFocus');
	});
	
	$('.text, .password, .inputSearch').blur(function() {
		$(this).removeClass('textFocus');
	});
	

	$('table.list tr:not([th], .noStriping):even').addClass('even');
	$('table.list tr:not([th], .noStriping):odd').addClass('odd');
	
	$('table.dottedList tbody tr:not([th], .noStriping):odd').addClass('stripeLightGrey');
	//$('table.dottedList tbody tr:not([th], .noStriping):even').addClass('listeven');
	
	
	$('#checkAllEvents').click(function() {
		selectRef = document.getElementById('eventList');
		if ($("#checkAllEvents").is(":checked")) {
            for (var i=0; i<selectRef.options.length; i++) {
				selectRef.options[i].selected = true;
			}
        }
        else {     
            for (var i=0;i<selectRef.options.length;i++) {
				selectRef.options[i].selected = false;
			}
        }
	});



	$('.closeWindow').click(function() {
		$('.jqmWindow').jqmHide();
	});
	

	
	$('.changeEventStatus').click(function() {
		eventId = document.getElementById('hdnEventId').value;
		
		$('#windowChangeEventStatus').jqm({ajax: 'presentation/ChangeEventStatus.php?eventId=' + eventId, trigger: 'a.changeEventStatus'});
	});
	
	
	$('.readMore').click(function() {
		$(this).css('display', 'none');
		$('.moreText').show();
	});
	
	addDatePickers();
	
	generateButtons();
	
});


function getContent(url) {
	$('#content').load('presentation/ContentController.php?Site=' + url);
}


$(function() {
	$('#filterActivity').selectCombo('presentation/LinkedCombo.php?search=filterActivity','#filterChampionatCategory');
	$('#filterCircle').selectCombo('presentation/LinkedCombo.php?search=filterCircle','#filterClub');
});


function addDatePickers() {
	$('.date-pick').datePicker();
	$('#start-date').bind(
		'dpClosed',
		function(e, selectedDates) {
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#end-date').dpSetStartDate(d.addDays(0).asString());
			}
		}
	);
	
	$('#end-date').bind(
		'dpClosed',
		function(e, selectedDates) {
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#start-date').dpSetEndDate(d.addDays(0).asString());
			}
		}
	);
	

	/*
	$('#start-date').blur(function() {
		$.post('business/Validator.php', {'date': this.value}, function(data) {
			document.getElementById('start-date').innerText = data;
		});
		return false;
	});
	
	$('#end-date').blur(function() {
		$.post('business/Validator.php', {'date': this.value}, function(data) {
			document.getElementById('end-date').innerText = data;
		});
		return false;
	});*/
	
	
	$('.date-pick').blur(function() {
		var field = $(this);
		
		$.post('business/Validator.php', {'date': this.value}, function(data) {
			field.val(data);
		});
		return false;
	});
	
	
	$('.time').blur(function() {
		var field = $(this);
		
		$.post('business/Validator.php', {'time': this.value}, function(data) {
			field.val(data);
		});
		return false;
	});


}



function generateButtons() {
	$('.btn').each(function(){
		var b = $(this);
		var tt = b.text() || b.val();
		
		if ($(':submit,:button',this)) {
			b = $('<a>').insertAfter(this). addClass(this.className).attr('id',this.id);
			$(this).remove();
		}
		
		b.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span>').text(tt).append('<i></i><span></span>'));
	});
}


function generateSpecificButton(obj) {
	var tt = obj.text() || obj.val();
	
	obj.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span>').text(tt).append('<i></i><span></span>'));
}



/*
$(function() {
	
	$('#start-date').blur(function() {
		// Create a boolean variable to check for a valid IE instance
		var xmlhttp = false;
		
		// Check if we are using IE
		try {
			// If the javascript version is greater than 5
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			// If not, then use the older activeX object
			try {
				// If we are using IE
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (E) {
				// Else we must be using a non-IE browser
				xmlhttp = false;
			}
		}
		
		// If we are using a non-IE browser, create a Javascript instance of the object
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		
		xmlhttp.open("GET", "Business/Validator.php?date=test");
		xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			alert(xmlhttp.responseText);
			//obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
		
		});
});*/





/**
 * Andre javascript-funksjoner
 */
 

function confirmPageLoad(question, pageLink) {
	var answer = confirm(question);
	if (answer) {
		window.location = pageLink; 
	}
}



function formatEmailAdresse (user, host, domain, linktext) {
	return( "<a href=" + "mail" + "to:" + user + "@" + host + "." + domain + ">" + linktext + "</a>")
}



