function show(id)
{
	if($(id))
		$(id).style.display = "block";
}

function hide(id)
{
	if($(id))
		$(id).style.display = "none";
}

var ProjectPlus = Class.create({
	
	//ID of element to update
	sTabDataID		: "tabbladData",
	
	//ID of active tab
	iActiveTabID 	: null,
	
	//module URI 
	sModuleURI 		: null,

	//project ID
	iProjectID		: null,

	//printlink element
	sPrintLinkID	: null,
	
	/**
	 * initialize the tabblad
	 * 
	 * @param string sModuleURI
	 * @param integer iProjectID
	 * @param integer iDefaultTabID
	 */
	initialize: function(sModuleURI, iProjectID, iDefaultTabID) 
	{
		this.iProjectID = iProjectID;
		this.sModuleURI = sModuleURI;
		
		//get the default tab
		var iFirstTabID = this.getFirstTabID(iDefaultTabID);
		
		//change the hash
		window.location.hash = "#"+iFirstTabID;
		
		//let the executer load our tab
		new PeriodicalExecuter(this.changeTab.bind(this), 0.005);
	},
	
	/**
	 * periodicale exexution function to change the tab content 
	 * with help of the location hash
	 * 
	 * @return void
	 */
	changeTab: function()
	{
		aURI = window.location.hash.split("#");
		
		if(aURI[1] && aURI[1] != this.iActiveTabID)
		{
			this.loadTabblad(aURI[1]);
		}
	},
	
	/**
	 * load a tabblad
	 * 
	 * does an ajax-request to receive tab information for a iTabbladID
	 * 
	 * @param integer iTabbladID
	 * @return void
	 */
	loadTabblad: function(iTabbladID)
	{
		//only get tabblad if it isn't the same as the active tabID
		if(iTabbladID != this.iActiveTabID)
		{	
			if(this.sPrintLinkID)
			{
				this.setPrintLink(iTabbladID);
			}
			
			//first show a loading.gif
			$(this.sTabDataID).innerHTML = "<img src='/images/tabblad_loading.gif' /> laden...";
		
			//set classname to ''
			if(this.iActiveTabID)
			{
				$('tabLeft_'+this.iActiveTabID).className = 'tabLeft';
				$('tabMid_'+this.iActiveTabID).className = 'tabMid';
				$('tabRight_'+this.iActiveTabID).className = 'tabRight';
			}

			$('tabLeft_'+iTabbladID).className = 'tabLeftH';
			$('tabMid_'+iTabbladID).className = 'tabMidH';
			$('tabRight_'+iTabbladID).className = 'tabRightH';

			//update the div with ID = 'tabbladData' 
			new Ajax.Updater(this.sTabDataID, this.sModuleURI+'/getTabblad/'+this.iProjectID+'/'+iTabbladID, {evalScripts:true});	
		
			//and set the actievTabID;
			this.iActiveTabID = iTabbladID;
		}
	},
	
	/**
	 * get the ID of the first tab to show
	 * checks the location hash to see if there is an active tab, else it returns the given default
	 * 
	 * @param iDefault
	 * @return integer
	 */
	getFirstTabID : function(iDefault)
	{
		aURI = window.location.hash.split("#");
		
		if(aURI[1])
		{
			return aURI[1];
		}
		
		return iDefault;
	},
	
	setPrintLinkID: function(sElID)
	{

		this.sPrintLinkID = sElID;	

		this.setPrintLink(this.iActiveTabID);
	},
	
	setPrintLink: function(iTabbladID)
	{
		$(this.sPrintLinkID).href = this.sModuleURI+'/print/'+this.iProjectID+'/'+iTabbladID;
	}
	
});

function submitForm(sModuleURI, iFormID, sDivToUpdate)
{
	//only get tabblad if it isn't the same as the active tabID
	//first show a loading.gif
	var sFormData = $(iFormID).serialize();
	
	$(sDivToUpdate).innerHTML = "<img src='/images/tabblad_loading.gif' /> laden...";

	//update the div with ID = 'tabbladData' 
	new Ajax.Request(sModuleURI, 
						{ 
							evalScripts:true,
							method: 'post',
							parameters: { _formAJAXPost: sFormData },
							onSuccess: function(transport)
							{
								$(sDivToUpdate).innerHTML = transport.responseText;
							}
						}
					);
}	

/*
 * layout functions
 */
	
var allText;
var allOptions;
	
function readMoreText(ID)
{
	sReturn = allText;
	
	document.getElementById('hideText_'+ID).style.display = "block";
		
	document.getElementById('createMoreText_'+ID).innerHTML =  sReturn;
}

function createMoreText(text, options, ID)
{

	if(isNaN(options))
	{
		sReturn = text;
	}
	else
	{
		allText = text;
		allOptions = options;
		
		var sReturn = "";
		
		//aantal zinnen
		sentences = text.split(".");
		
		if(sentences.length < (options-1))
			sReturn = text;
		else
		{
		
			//loop door de zinnen totdat aantal van opties is bereikt
			for(y=0;y<=options-1;y++)
				sReturn += sentences[y]+".";
			
			//als de tekst niet gelijk is aan standaard tekst && aantal zinnen groter is dan de opties
			if(sReturn != text && sentences.length > (options-1))
				sReturn += "<div><a class='cursor' onclick='readMoreText("+ID+");'>Lees meer ></a></div>";
		}	
	}
	
	//de link om meer te lezen verbergen
	document.getElementById('hideText_'+ID).style.display = "none";
	
	//nieuwe tekst in onze div zetten
	document.getElementById('createMoreText_'+ID).innerHTML =  sReturn;
}

/*
 *	functie om een text zo breed te maken als een image
 */
function truncateText(text, imgID)
{
	var newImage = new Image;
	newImage.src = $('image_'+imgID).src;

	var truncatedText = "";
	
	if(newImage.width < 1)
		newImage.width = '100';
			
	//1 letter = 0,16px	
	if(text.length > (newImage.width * 0.15))
	{
		for(var y=0; y <= (newImage.width * 0.15); y++)
		{
			if(text.charAt(y))
				truncatedText  = truncatedText + text.charAt(y);
		}
	}
	else
	{
		truncatedText = text;
	}

	$('truncatedText_'+imgID).innerHTML =  truncatedText;
}


/*	Functies voor het fotoboek
 *
 */	
var oldPicNavigator = false;
var oldPicNavigatorCat = false;
var currentCatID;
var currentPicID;

function showPicNavigator(catID, picID, lastPicID)
{
	currentCatID = catID;
	currentPicID = picID;
	
	//eerste foto? dan de 'vorige link' verbergen
	if(picID == 1)
		$('picNavigatorLeft_'+catID).style.visibility = 'hidden';
	else
		$('picNavigatorLeft_'+catID).style.visibility = 'visible';
	
	//laatste foto? dan de 'volgende link' verbergen
	if(picID == lastPicID)
		$('picNavigatorRight_'+catID).style.visibility = 'hidden';
	else
		$('picNavigatorRight_'+catID).style.visibility = 'visible';
	
	//image we want
	var sNewImage= $('large-image_'+currentCatID+'_'+currentPicID).src;

        $('activePic').src = sNewImage;

	//push the description
	$('picNavigatorDescription').innerHTML = $('longPicDescription_'+currentCatID+'_'+currentPicID).innerHTML;
	
	//if there is an navigatorcat hide it
	if(oldPicNavigatorCat)
		oldPicNavigatorCat.style.display = 'none';
	
	//store our new navigatorcat
	oldPicNavigatorCat = $('picNavigatorCat_'+catID);
	
	//hide the new one
	show('picNavigatorCat_'+catID);
	
	//and show our layer
	show('picNavigator');
}

/*	
 *	functie om volgende foto te laten zien
 */
function navPic(direction)
{
	if(direction == "prev")
		var nextPic = (currentPicID - 1);
	else if(direction == "next")
		var nextPic = currentPicID + 1;
		
	if($('image_'+currentCatID+'_'+nextPic))
	{
		currentPicID = nextPic;
		$('image_'+currentCatID+'_'+nextPic).onclick();
	}		
}
