var init = false;
var baseUrl = "http://www.cimdata-seminare.de/";

$(document).ready(initFunction);
$(window).load(initFunction);

function initFunction()
{
  if(true == init)
     return;

   init = true;
  
  initAkkordions($(".accordionCapsule"));
  initMiniCal($(".miniCalendar"))
  initTest();
  $("#currentCategory").html($("#pagecategory").html());
  
  //$("#createPDF").bind("click",function(){alert(window.location);});
  $("#bookSeminar").bind("click",function(){
  	$.post(baseUrl+"fileadmin/bookseminar.php",{id:$("#currentSeminar").html(),category:$("#currentCategory").html()},showBookPopup);
  });
	$("#promoteSeminar").bind("click",function(){
  	$.post(baseUrl+"fileadmin/promoteseminar.php",{id:$("#currentSeminar").html(),category:$("#currentCategory").html()},showPromotePopup);
  });  
  
  $(".listBook").bind("click",function(){
  	$.post(baseUrl+"fileadmin/bookseminar.php",{id:$(this).find(".seminarid").html(),category:$(this).find(".categoryid").html()},showBookPopup);
  });
  
	$(".seminarBook").bind("click",function(){
  	$.post(baseUrl+"fileadmin/bookseminar.php",{id:$(this).find(".seminarid").html(),category:$(this).find(".categoryid").html()},showBookPopup);
  });  
  $("#createPDF").bind("click",function(){
  	if($(this).parent().hasClass("globalButtonDisabled"))
  		return false;
  	var href = $(this).attr("href")
  	$(this).attr("href",href+"&semid="+$("#currentSeminar").html()+"&categrory="+$("#currentCategory").html()+"&link="+window.location);
  	
  });
  
  $("#printPage").bind("click",function(){
	  	var href = $(this).attr("href")
	  	$(this).attr("href",href+"?tx_customseminar[id]="+$("#currentSeminar").html());	  	
	  	
  });
  
  
  
  $("#calendarSearchSubmit").bind("click",function(){
  	$.post(baseUrl+"/kalendarsuche/",{search:$("#searchInput").val(),page:0},showSearchPopup);
  });
  
	var height = Math.max($("#innerContent").height(),$("#rightBar").height(),$("#inner2").height());
	$("#innerContent").css("height",height);
	var height = Math.max($("#innerContentFull").height(),$("#rightBar").height(),$("#calendarRightBar").height()+80);
	$("#innerContentFull").css("height",height);  
 }

function showSearchPopup(data)
{
	var po = showPopup(data);
	po.find("#poTitle").html("Seminar suchen");
  $(".listBook").bind("click",function(){
  	$.post(baseUrl+"fileadmin/bookseminar.php",{id:$(this).find(".seminarid").html(),category:$(this).find(".categoryid").html()},showBookPopup);
  });
	
}

 
function showBookPopup(data)
{
	var po = showPopup(data);
	//po.find("#poTitle").html("Seminar buchen");
	po.find("#currentCat").bind("click",showCats);
	po.find("#currentSem").bind("click",showSems);
	po.find(".catentry").bind("click",function(){
		$("#catInput").val($(this).attr("id"));
		$("#semInput").val($(this).attr("id","0"));
		$.post(baseUrl+"fileadmin/bookseminar.php",po.find("form").serialize(),showBookPopup);
	});
	po.find("#posubmit").bind("click",function(){	
	  $.post(baseUrl+"fileadmin/bookseminar.php",po.find("form").serialize()+"&action=send",showBookPopup);
	});
	po.find("#podelete").bind("click",function(){po.find("form")[0].reset();});
	po.find(".sementry").bind("click",function(){
		$("#semInput").val($(this).attr("id"));
		$.post(baseUrl+"fileadmin/bookseminar.php",po.find("form").serialize(),showBookPopup);
	});	
}

function showPromotePopup(data)
{
	var po = showPopup(data);
	po.find("#currentCat").bind("click",showCats);
	po.find(".catentry").bind("click",function(){
		$("#catInput").val($(this).attr("id"));
		$("#semInput").val($(this).attr("id","0"));
		$.post(baseUrl+"fileadmin/promoteseminar.php",po.find("form").serialize(),showPromotePopup);
	});
	po.find("#posubmit").bind("click",function(){	
	  $.post(baseUrl+"fileadmin/promoteseminar.php",po.find("form").serialize()+"&action=send",showPromotePopup);
	});
	
	po.find("#currentSem").bind("click",showSems);
	po.find(".sementry").bind("click",function(){
		$("#semInput").val($(this).attr("id"));
		$.post(baseUrl+"fileadmin/promoteseminar.php",po.find("form").serialize(),showPromotePopup);
	});	
}

function showCats()
{
	$("#catContainer").show();
	$(this).unbind("click").bind("click",hideCats);
}
 
function hideCats()
{
	$("#catContainer").hide();
	$(this).unbind("click").bind("click",showCats);
}

function showSems()
{
	
	$("#semContainer").show();
	$(this).unbind("click").bind("click",hideSems);
}
 
function hideSems()
{
	$("#semContainer").hide();
	$(this).unbind("click").bind("click",showSems);
}

function showPopup(data)
{
	if($("#popup").length == 0)	
  	var div = $(document.createElement('DIV'));
  else
  	var div = $("#popup");
  div.attr("id","popup");
  div.html(data);
  div.find("#poClose").bind("click",function(){div.remove();});
  div.appendTo("body"); 
  return div;
}

function initAkkordions(objects)
{

	contractAllAccordion(false);
	for(var i = 0; i < objects.length;i ++)
	{
		var current = $(objects[i]);
		current.find(".accordionTitle").unbind("click");
		current.find(".accordionTitle").bind("click",{firstClick : 1},extendAccordion);
	}
}

function extendAccordion(event)
{
	if(event.data && event.data.first)
	{
		contractAllAccordion(false);
	}
	else
		contractAllAccordion(true);
	var height = 0;
	$(this).parent().children().each(function(c,e){
		if(!isNaN(parseInt($(e).height())))
			height +=	parseInt($(e).height())
		/*if(!isNaN(parseInt($(e).css("margin-top")) != NaN))
			height += parseInt($(e).css("margin-top"));
		if(!isNaN(parseInt($(e).css("margin-bottom")) != NaN))
			height += parseInt($(e).css("margin-bottom"));*/
	});
	height += 5;	
	$(this).css("background-image","url('fileadmin/images/seminare-active.gif')");
	$(this).parent().animate({"height":height});
	$(this).unbind("click");
	$(this).bind("click",contractAccordion);}

function contractAccordion()
{
	$(this).css("background-image","url('fileadmin/images/seminare-inactive.gif')");
	$(this).parent().animate({"height":"20px"});
	
	$(this).unbind("click");
	$(this).bind("click",extendAccordion);
}


function initTest()
{
	contractAll(false);
	var current = $(".seminarEntry.extended");
	$("#currentSeminar").html(current.attr("id"));	
	$("#currentCategory").html($("#pagecategory").html());	
	if(current.length != 0)
	{
		$("#createPDF").parent().removeClass("globalButtonDisabled");
		$("#createPDF").parent().addClass("globalButton");	
	}
	
	var height = 0;
	current.children().each(function(c,e){
		if(!isNaN(parseInt($(e).height())))
			height +=	parseInt($(e).height())
		/*if(!isNaN(parseInt($(e).css("margin-top")) != NaN))
			height += parseInt($(e).css("margin-top"));
		if(!isNaN(parseInt($(e).css("margin-bottom")) != NaN))
			height += parseInt($(e).css("margin-bottom"));*/
	});
	height += 5;	
	current.find(".seminarBook").show();
	current.find(".seminarTitle").css("background-image","url('fileadmin/images/seminare-active.gif')");
	current.animate({"height":height});
	current.find(".seminarTitle").unbind("click");
	current.find(".seminarTitle").bind("click",contractSeminar);	
}

function extendSeminar()
{
	contractAll(true)
		
	$("#currentSeminar").html($(this).parent().attr("id"));	
	$("#currentCategory").html($("#pagecategory").html());
	$("#createPDF").parent().removeClass("globalButtonDisabled");
	$("#createPDF").parent().addClass("globalButton");
	var height = 0;
	$(this).parent().children().each(function(c,e){
		if(!isNaN(parseInt($(e).height())))
			height +=	parseInt($(e).height())
		
		if(!isNaN(parseInt($(e).css("margin-top"))))
			height += parseInt($(e).css("margin-top"));
		if(!isNaN(parseInt($(e).css("margin-bottom"))))
			height += parseInt($(e).css("margin-bottom"));
	});
	height += 5;	
	$(this).css("background-image","url('fileadmin/images/seminare-active.gif')");
	var tmp = $(this);
	$(this).parent().animate({"height":height},"normal",function(){tmp.parent().find(".seminarBook").show();});
	$(this).unbind("click");
	$(this).bind("click",contractSeminar);
}

function contractSeminar()
{
	$(this).parent().find(".seminarBook").hide();
	$("#createPDF").parent().addClass("globalButtonDisabled");
	$("#createPDF").parent().removeClass("globalButton");

	$(this).css("background-image","url('fileadmin/images/seminare-inactive.gif')");
	
	$(this).parent().animate({"height":"20px"});
	
	$(this).unbind("click");
	$(this).bind("click",extendSeminar);
}

function contractAll(animate)
{
	$(".seminarEntry").each(function(c,e){
	  if(animate == true)
	  {
			$(e).animate({"height":"20px"});
			$(e).find(".seminarBook").hide();
		}
		else
		{
			$(e).css("height","20px");
			$(e).find(".seminarBook").hide();
		}
		$(e).find(".seminarTitle").unbind("click");
		$(e).find(".seminarTitle").bind("click",extendSeminar);
	});
}

function contractAllAccordion(animate)
{
	$(".accordionCapsule").each(function(c,e){
	  if(animate == true)
			$(e).animate({"height":"20px"});
		else
			$(e).css("height","20px");
		$(e).find(".accordionTitle").unbind("click");
		$(e).find(".accordionTitle").bind("click",extendAccordion);
	});
}

function initMiniCal(objects)
{
	for(var i = 0; i < objects.length;i ++)
	{
		var current = $(objects[i]);
		
		current.find(".miniprev").unbind("click").bind("click",{direction:-1,id:current.parent().attr("id")},changeMini);
		current.find(".mininext").unbind("click").bind("click",{direction:1,id:current.parent().attr("id")},changeMini);
	}
}

function changeMini(event)
{
	var month = parseInt($(this).parent().find(".minimonth").html(),10)+event.data.direction;
	
	var year = parseInt($(this).parent().find(".miniyear").html(),10);
	if(month == 13)
	{
		year++;
		month = 1;
	}
	if(month == 0)
	{
		year--;
		month = 12;
	}

	var id = event.data.id;
	$.post(baseUrl+"minikalendar/calendar//mini/1/"+month+"/"+year+".html",{},function(data){$("#"+id).html(data);initMiniCal($(".miniCalendar"));});
}
