var animating = false;
var currentbarid = null;
var rtime = new Date(1, 1, 2000, 12,00,00);
var timeout = false;
var delta = 100;


function setDepartmentId(jdepartmentid)
{
  $.post(ajaxfile, { action: 'setDepartmentId',departmentid: jdepartmentid }, function(output)
  {
    /*
    if (trim(output) == 'success')
    {
      // do something after success
    }
    */
  });
}

function getMostRecentPoll()
{
$("#pollTab").load(ajaxfile +
  "?action=getMostRecentPoll");
}

function getMostRecentAgendaItem()
{
  $("#calendarTab").load(ajaxfile +
  "?action=getMostRecentAgendaItem");
}



function getReactionForm(reactionToObjectID)
{

	$(".reactionForm").html("");
	
  $("#reactionForm_" + reactionToObjectID).load(ajaxfile +
  "?action=getReactionForm" +
	"&reactionToObjectID=" + reactionToObjectID);
}

function checkEmail(address)
 {
   var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
   if (filter.test(address)) return true;
   else return false;
 }

function submitShoutBoxResponse(shoutBoxID)
{

  var showAllResponses = false;
	
	if($("#showAllResponses_" + shoutBoxID).val() == true)
	{
   showAllResponses = true;
	}	
	
	$.getScript(ajaxfile + 
	 "?action=submitShoutBoxResponse" +
	  "&shoutBoxID=" + shoutBoxID +
		"&showAllResponses=" + showAllResponses +
		"&sbnn=" + $("#shoutBoxNickName_" + shoutBoxID).val() +
		"&sbe=" + $("#shoutBoxEmail_" + shoutBoxID).val() +
		"&sbt=" +   encodeURI($("#shoutBoxComment_" + shoutBoxID).val()),
		function()
		{ 
			 getShoutBoxResponses(shoutBoxID,false);
		}
	 );	
}

function submitContactForm()
{
	if (checkContactForm()) 
	{
  	$("#emailForm").submit();
  }
}

function checkContactForm()
{
	var boolCorrect = true;
	
	if($("#firstname").val() == "")
	{
		$("#firstname").css("backgroundColor","#F99");
		$("#firstnameMsg").show();
		boolCorrect = false;
	}else
	{
		$("#firstname").css("backgroundColor","#FFF");
		$("#firstnameMsg").hide();
	}
	
	if(!checkEmail($("#email").val()))
  {
    $("#email").css("backgroundColor","#F99");
		$("#emailMsg").show();
    boolCorrect = false;
  }else
  {
    $("#email").css("backgroundColor","#FFF");
		$("#emailMsg").hide();
  }
	
	if(!$('input[name=iWant]:radio:checked').val())
  {
    $("#iwanttd").css("backgroundColor","#F99");
    boolCorrect = false;
  }else
  {
    $("#iwanttd").css("backgroundColor","#FFF");
  }
	
	return boolCorrect;
}


function getShoutBoxResponses(shoutBoxID,recursive,shoutBoxResponseID)
{
	
//alert("getShoutBoxResponses" + shoutBoxID + " "  + recursive + ajaxfile + " >" + $("#shoutBoxResponses_"+shoutBoxID).html());
		
  var showAllResponses = false;
  
  if($("#showAllResponses_" + shoutBoxID).val() == true)
  {
   showAllResponses = true;
  }
	
	//alert($("#shoutBoxResponses_"+shoutBoxID));
	
 $("#shoutBoxResponses_"+shoutBoxID).load(
   ajaxfile + 
   "?action=getShoutBoxResponses" +
	  "&showAllResponses=" + showAllResponses +
	  "&sbrid=" + shoutBoxResponseID +
    "&shoutBoxID=" + shoutBoxID,
    function()
    {
      if(recursive)
      {
				//setTimeout("getShoutBoxResponses("+shoutBoxID+",true,"+shoutBoxResponseID+");",3000);
      }
    }
   );
}


function goToRegion(regionID)
{
	$.getScript(ajaxfile + 
	"?action=goToRegion" +
	"&regionID=" +regionID 
	 );
}

function postNewsItemReaction(newsItemID)
{	
	$.getScript(ajaxfile + 
	"?action=postNewsItemReaction" +
	"&reactionToObjectID=" + newsItemID +
	"&nirn=" + encodeURI($("#nirn_"+newsItemID).val()) +
	"&nire=" + encodeURI($("#nire_"+newsItemID).val()) +
	"&nirl=" + encodeURI($("#nirl_"+newsItemID).val()) +
	"&nirc=" + encodeURI($("#nirc_"+newsItemID).val()),
	function()
	{
		
		//$("#newsItemReactionFrom_" + newsItemID).remove();

	});
}

function getNewsItemReactions(newsItemID)
{
 $("#newsItemReactions_"+newsItemID).load(
 ajaxfile + 
 "?action=getNewsItemReactions" +
  "&reactionToObjectID=" + newsItemID
 );
}

function submitPoll(answerID,comment)
{	
  
	if(answerID > 0)
	{
  	$.getScript(ajaxfile +
  	"?action=submitPoll" +
  	"&answerID=" +	answerID +
		"&isHomePage=" +  isHomePage +
		"&answerComment=" +  comment);
  }else
	{
		notifyUser("Ongeldige keuze");
	}
}

function getPollRandom()
{
  $("#pollTab").load(ajaxfile +
	"?action=getPollRandom");
}

function getPolls()
{
  $("#pollTab").load(ajaxfile +
  "?action=getPolls");
}

function getAgendaItems()
{
  $("#calendarTab").load(ajaxfile +
  "?action=getAgendaItems");
}



function notifyUser(message)
{
	alert(message);
}


// use timeout to prevent some browsers to update too many times during resizing
function resizeend()
{
  if (new Date() - rtime < delta)
  {
    setTimeout(resizeend, delta);
  }
  else
  {
    timeout = false;
    positionButtonBar();
  }       
}

// reposition the bottom button bar
function positionButtonBar()
{
  $('#mainBottomDiv').css({top:($(window).scrollTop()+$(window).height()-$('#mainBottomDiv').height())+'px'});
}

// adjust background height department view
function resizeDepartmentView()
{
  // increase blue background to accomodate departmentbox content
  var dbheight = $('#departmentboxcontent').height();
  var winheight = $(window).scrollTop()+$(window).height();
  
  if (winheight > dbheight)
  {
    $('#mainBGDivdepartment').height(winheight);
    $('#departmentboxcontent').height(winheight-107);
  }
  else
  {
    $('#mainBGDivdepartment').height(dbheight+107);
  }
}

//adjust background height other view
function resizeOtherView()
{
  // increase blue background to accomodate otherbox content
  var obheight = $('#otherboxcontent').height();
  var winheight = $(window).scrollTop()+$(window).height();
  
  if (winheight > obheight)
  {
    $('#mainBGDivother').height(winheight);
    $('#otherboxcontent').height(winheight-77);
  }
  else
  {
    $('#mainBGDivother').height(obheight+77);
  }
}

function animateBarPositions(jbarid, jdepartmentid)
{
  if ((!animating) && (currentbarid != jbarid))
  {
    // set departmentid in jsp session
    setDepartmentId(jdepartmentid);
    
    moveBars(jbarid, true);
  }
	
	
	if(jbarid == "bar4")
	{
    $("#IWantIAm").attr("src",webRoot + "/images/IAm.png");
	}else
	{
		$("#IWantIAm").attr("src",webRoot + "/images/iwant.png");
	}
}

function setBarPositions(jbarid)
{
  moveBars(jbarid, false);
}

function moveBars(jbarid, janimating)
{
  currentbarid = jbarid;
  animating = janimating;
  
  $('.barcontent').hide();
  $('.barphoto').hide();
  //$('.bariwant').hide();
  
  var bar1top = 186;
  var openoffset = 271;
  var closedoffset = 35;
  var speed = 750;
  
  var bariwanttop = bar1top + 9;
  var bar2top = bar1top + openoffset;
  var bar3top = bar2top + closedoffset;
  var bar4top = bar3top + closedoffset;
        
  if (jbarid == 'bar2')
  {
    bar2top = bar1top + closedoffset;
    bariwanttop = bar2top + 9;
    bar3top = bar2top + openoffset;
    bar4top = bar3top + closedoffset;
  }
  else if (jbarid == 'bar3')
  {
    bar2top = bar1top + closedoffset;
    bar3top = bar2top + closedoffset;
    bariwanttop = bar3top + 9;
    bar4top = bar3top + openoffset;
  }
  else if (jbarid == 'bar4')
  {
    bar2top = bar1top + closedoffset;
    bar3top = bar2top + closedoffset;
    bar4top = bar3top + closedoffset;
    bariwanttop = bar4top + 9;
  }
  
  if (janimating)
  {
    // animate the bars
		$('#memo').animate({ top: bariwanttop + 30 }, speed );
		
    $('#bariwant1').animate({ top: bariwanttop}, speed );
    $('.movebar2').animate({ top: bar2top}, speed );
    $('.movebar3').animate({ top: bar3top}, speed );
    $('.movebar4').animate({ top: bar4top}, speed, function()
    {
      // animation complete
      showContent(jbarid);
      showPhoto(jbarid, janimating);
    });
  }
  else
  {
    // set bar tops
		$('#memo').css('top',bariwanttop + 30);
		
    $('#bariwant1').css('top',bariwanttop);
    $('.movebar2').css('top',bar2top);
    $('.movebar3').css('top',bar3top);
    $('.movebar4').css('top',bar4top);
    showContent(jbarid);
    showPhoto(jbarid, janimating);
  }
}

function showPhoto(jbarid, janimating)
{
  var speed = 400;
  var barphotoid = '#barphoto1';
  if (jbarid == 'bar2')
    barphotoid = '#barphoto2';
  else if (jbarid == 'bar3')
    barphotoid = '#barphoto3';
  else if (jbarid == 'bar4')
    barphotoid = '#barphoto4';

  if (janimating)
  {
    $(barphotoid).fadeIn(speed, function()
    {
      // fadein complete
      animating = false;
    });
  }
  else
  {
    $(barphotoid).show();
  }
}

function showContent(barid)
{
  var barcontentid = '#barcontent1';
  if (barid == 'bar2')
    barcontentid = '#barcontent2';
  else if (barid == 'bar3')
    barcontentid = '#barcontent3';
  else if (barid == 'bar4')
	{
		$("#IWantIAm").attr("src",webRoot + "/images/IAm.png");
		barcontentid = '#barcontent4';
	}

  $(barcontentid).show();
}


function showmenuitems(barid)
{
  
}


function openPopup(url)
{

  window.open(url,'name','height=300,width=500');

}

function opentab(tabid)
{
  $('.bluetab').removeClass('open closed').addClass('closed');
  $('#bluetab'+tabid).removeClass('closed').addClass('open');


  $('#bluetabimg1').attr('src',webRoot + 'images/btn_news'+((tabid==1)?'_act':'')+'.png');
  $('#bluetabimg2').attr('src',webRoot + 'images/btn_calendar'+((tabid==2)?'_act':'')+'.png');
  $('#bluetabimg3').attr('src',webRoot + 'images/btn_poll'+((tabid==3)?'_act':'')+'.png');
  
	$(".tab").css("display","none");
	
  if (tabid==1)
    $('#newsTab').css("display","");
  else if (tabid==2)
    $('#calendarTab').css("display","");
  else if (tabid==3)
    $('#pollTab').css("display","");
		
		
}

function toggleSelectBox(jbarid)
{
  $('#barselectopen'+jbarid).toggle();
  $('#barselectclose'+jbarid).toggle();
}

function searchsite(url)
{
  var searchterm = $('#searchterm').val();
  if (searchterm.length>0 && searchterm != 'Zoeken')
  {
    window.location.href = url + "?action=search&searchterm="+escape(searchterm);
  }
}

