$(document).ready(function(){

	//LOADING POPUP
	//Click the button event!
	$(".eventToday").click(function(){
        var params = splitParams($(this).attr("class"));
        
		//centering with css
		centerPopup();
		
		//load popup
		loadPopup(getParam(params, 1));
		
		$("#popupContact").fadeIn("slow");

	});

	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if (e.keyCode==27 && popupStatus==1)
		{
			disablePopup();
		}
	});
});

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(index){
	//loads popup only if it is disabled
	if(popupStatus==0){
		fillPopupBox(index);
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		$("#popupContact").resize();
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		emptyPopupBox();
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

function emptyPopupBox()
{
    $(".eventHeader").innerHTML = "";
    $(".eventBox").innerHTML = "";    
}

function fillPopupBox(index)
{
    var txtHeader = "<table id=\"tblSlide\" width=\"100%\">\n";
	var txtFooter = "</table>";
	var popupBox = "";
    var arrEventDay = index.split("|");
    var eventCount = 1;
    
	for(var i in arrEvents[index] )
	{
		//if (eventCount > 1)
		//{
			popupBox += "<tr><hr/></td></tr>";
		//}
		
		//eventDesc = eventType + "|" + title + "|" + location + "|" + description + "|" + link + "|" + externalLink + "|" + icalLink
        var eventDesc = arrEvents[index][i].split("|");
        var eventType = eventDesc[0];
        var title = eventDesc[1];
        var location = eventDesc[2];
        var description = eventDesc[3];
        var link = eventDesc[4];
        var externalLink = eventDesc[5];
        var icalLink = eventDesc[6];

        popupBox += "<tr>";
        popupBox += "<td width=\"100%\"><h3>";
        if (typeof(eventType) != "undefined" && eventType != null && eventType.length > 0)
        {
        	popupBox += eventType;
		}
        if (typeof(link) != "undefined" && link != null && link.length > 0)
        {
        	var target = "";
        	if (externalLink)
        	{
        		target = "target=\"_blank\""
        	}
        	
            popupBox += "<a href=\"" + link + "\" " + target + ">" + title + "</a>"; //teaser text with link
        }
        else
        {
            popupBox += title; //teaser text
        }
        popupBox += icalLink;
        popupBox += "</h3>";
        popupBox += "</td>";
        
        popupBox += "</tr>\n"; //teaser text with link
        
        if (typeof(location) != "undefined" && location != null && location.length > 0)
        {
        	popupBox += "<tr><td>" + location + "</td></tr>\n"; //location
        }
        
        if (typeof(description) != "undefined" && description != null && description.length > 0)
        {
        	popupBox += "<tr><td>" + description + "</td></tr>\n"; //description
        }
        
        eventCount++;
    }
    popupBox = txtHeader + popupBox + txtFooter;
    
    $("#eventHeader").html(arrEventHeader[index]);
    $("#eventBox").html(popupBox);    
}

function changeMonth(month, year)
{
    $("#calendarMonth").val(month);
    $("#calendarYear").val(year);    
    $("#calendarForm").submit();
}

