$(document).ready(function() {
    $("#loginWindow").dialog({
    	autoOpen:false,
    	buttons: { "Schließen": function() { $(this).dialog("close"); } },
    	title : "Login Fenster",
    	dialogClass : "loginWindow"
    });
    $(".jQueryButton").button();
    if (featureEvents) {
    	featureNews();
    }
	$( ".jquery-calendar" ).datepicker({
		dateFormat : "dd.mm.yy",
		dayNamesMin : ["So","Mo","Di","Mi","Do","Fr","Sa"],
		firstDay : 1
	});
	initTinyCorrect();
});

function openLogin() {
	$('#loginWindow').dialog('open');
}

/**
 * The name of the index file
 */
var indexName = "index.php";

/**
 * Returns the URL of the index file
 * @return string the url
 */
function getIndexUrl() {
	var basicPath = location.pathname;
	var position = basicPath.indexOf(indexName);
	// if there's no index.php file the root URL is given
	if (position == -1) {
		// if there's no '/' at the end of the basicPath, add it
		if (basicPath.substr(basicPath.length-1,1)!="/") {
			return basicPath+"/"+indexName;
		}
		return basicPath+indexName;
	}
	return basicPath.substring(0,position+indexName.length);
}

function goToIndex() {
	window.location.href = getIndexUrl();
}

/**
 * The specific url extension for the first, second and so on, image and text
 */
urlIDs = {0 : "aquatreffhome", 1 : "kinderschwimmenhome", 2 : "bewegungsinselhome"};

$(document).ready(function() {
	$('.items-row').first().children('div.item').children('p').each(function (index) {
		index = (index-index%2)/2;
		if (urlIDs[index] != undefined) {
			$(this).click(function() {
				document.location.href = getIndexUrl()+"/"+urlIDs[index];
			});
		}
	});
});

function alignBasicMenu() {
	$('.menuBasic').first().children().filter('.menu').first().children().filter('li').each(function (index) {
		if (index%2==1) {
			$(this).css("margin-top","60px");
		}
	});
}

function featureNews() {
	var obj = new VibrationObject(".newsFeatured");
	obj.vibrate();
}

function VibrationObject(selector) {
	  var interval = 3;
	  var duration= 1500;
	  var shake= 15;
	  var vibrateIndex = 0;

	  this.vibrate = function() {
	        vibrateIndex = setInterval(vibrate, interval);
	        setTimeout(stopVibration, duration);
	  };

	  var vibrate = function(){
		  $(selector).stop(true,false).css({
			  position: 'relative',
			  left: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px',
			  top: Math.round(Math.random() * shake) - ((shake + 1) / 2) +'px'
		  });
	  };

	  var stopVibration = function() {
	      clearInterval(vibrateIndex);
	      $(selector).stop(true,false).css({
	    	  position: 'static', left: '0px', top: '0px'
	      });
	  };
}

