var bShowactive = true;
var iFadespeed	= 10;
var iActual = 0;
var sActiveCompany = 'c_0';
var sActiveNumber = '';
var bCompanyclickRunning = false;
var bNumberclickRunning = false;

function visitSite()
{
	// Load the current image
	img_obj = getObj('portfolioImage');
	
	if(img_obj)
	{
		var sFilename = img_obj.src;
		var aFilename = sFilename.split("/");
		var sUrl = aFilename[(aFilename.length - 1)];
		
		sUrl = "http://" + sUrl.replace(".jpg","");
		
		window.open(sUrl);
	}
}

function log(sValue)
{
	addLog(sValue);
}

function addLog(sValue)
{
	logObj = getObj('logger');
	
	if(isObj(logObj))
	{
		logObj.innerHTML = sValue + "<br>" + logObj.innerHTML;
	}
}

function GetSliderHeight()
{	
	return 350;
}

function InitSlider(iBlock, iProject)
{
	sActiveCompany = 'c_' + iProject;
	
	var from = iActual * GetSliderHeight();
	var to = iBlock * GetSliderHeight();

	if(to == 0)
	{
		if(from == 0)
		{
			from = GetSliderHeight();
		}
	}
	
	iActual = iBlock;
	
	//
	if(from > to)
	{
		slideBoxFromTo('up', from, to);
	}
	else
	{
		slideBoxFromTo('down', from, to);
	}
}

// @param type: down / up
function SlideToObject(sType, iTotal)
{
	var from = iActual * GetSliderHeight();
	var to = 0;
	
	if(sType == 'down')
	{
		iActual++;
		if(iActual > iTotal)
		{		
			iActual = 0;
		
			to = iActual * GetSliderHeight();
			
			slideBoxFromTo('up', from, to);
		}
		else
		{
			to = iActual * GetSliderHeight();
			
			slideBoxFromTo('down', from, to);
		}
	}
	else
	{	// prev
		iActual--;
		if(iActual < 0)
		{
			iActual = iTotal;
			
			to = iActual * GetSliderHeight();
			
			slideBoxFromTo('down', from, to);
		}
		else
		{
			to = iActual * GetSliderHeight();
			
			slideBoxFromTo('up', from, to);
		}
	}
}

function unloadActiveCompany()
{
	temp_obj = getObj(sActiveCompany);
	
	if(temp_obj)
	{
		//temp_obj.setAttribute("class", "");
		temp_obj.className = "CustomerInnerMenuItem";
	}
}

function unloadActiveNumber(sFirstActiveNumber)
{
	if(sActiveNumber == "")
	{
		temp_obj = getObj(sFirstActiveNumber);
	}
	else
	{
		temp_obj = getObj(sActiveNumber);
	}
	
	if(temp_obj)
	{
		//temp_obj.setAttribute("class", "numberItem");
		temp_obj.className = "numberItem";
	}
}

function replaceAll(text, strA, strB)
{
    return text.replace( new RegExp(strA,"g"), strB );    
}

function HandleItemClick(sCompanyId, sUrl, sBg)
{
	if(bCompanyclickRunning == false)
	{
		bCompanyclickRunning = true;
		
		unloadActiveCompany();
		
		sActiveNumber = '';
		
		temp_obj = getObj(sCompanyId);
		
		if(temp_obj)
		{
			//temp_obj.setAttribute("class", "CustomerMenuItem CustomerMenuItemActive");
			temp_obj.className = "CustomerInnerMenuItem CustomerMenuItemActive";
		}

		sActiveCompany = sCompanyId;
		
		// Make the bg div equal to each other
		t_one = getObj("pageBackgroundOne");
		t_two = getObj("pageBackgroundTwo");
		
		if(t_one && t_two)
		{	
			// Sync the two background panels by image
			t_two.style.backgroundImage = t_one.style.backgroundImage

			// Fade the top layer in
			FadeObject('pageBackgroundTwo', 'in', 100, 1, 10, 
				function()
				{	
					if(sBg != "")
					{
						// Load the picture in the bottom t_one layer
						if(t_one)
						{
							var sImageUrl = "url("+sBg+")";
							// Replace the spaces to make it a valid html url
							sImageUrl = replaceAll(sImageUrl," ","%20");
							t_one.style.backgroundImage = sImageUrl;
						}
					}
				}
			);
		}
		
		// Fade the portfolio image out
		
		FadeObject('portfolioImage', 'out', 0, 4, 3,
			function()
			{
				// Show the loader
				VisibleOn('loader');
				
				// Load the selected customer
				LoadPage(sUrl, 'to',
					function()
					{
						// Hide the loader
						VisibleOff('loader');
						
						var fCallbackPFI = function()
						{
							FadeObject('portfolioImage', 'in', 100, 4, 3,
								function()
								{
									bCompanyclickRunning = false;
								}
							);
						};
						
						if(sBg != '')
						{
							// Make the top layer invisible					
							FadeObject('pageBackgroundTwo', 'out', 0, 3, 3,fCallbackPFI);
						}
						else
						{
							// Call the callback
							setTimeout(fCallbackPFI, 0);
						}
					}
				);
			}
		);	
	}
}

function handleNumberClick(sNumberId, sImageUrl, sNewWidth, sFirstActiveNumber)
{
	if(bNumberclickRunning == false)
	{
		bNumberclickRunning = true;

		unloadActiveNumber(sFirstActiveNumber);
		
		temp_obj = getObj(sNumberId);
		
		if(temp_obj)
		{
			//temp_obj.setAttribute("class", "numberItem activeNumberItem");
			temp_obj.className = "numberItem activeNumberItem";
		}
		
		sActiveNumber = sNumberId;

		// Load the current image
		img_obj = getObj('portfolioImage');
			
		// Define the new image
		var tempImage = new Image();
		
		// Set the onload of the image // Handle the fade out and fade in
		tempImage.onload = function()
		{
			// Fade out the current image
			FadeObject('portfolioImage', 'out', 0, 4, 3,
				function()
				{
					// Set the loaded image as current
					img_obj.src = tempImage.src;
					
					// Fade the image in
					FadeObject('portfolioImage', 'in', 100, 4, 3,
						function()
						{
							bNumberclickRunning = false;
						}
					);
				}
			);
		};
		
		// Load the new image
		tempImage.src = sImageUrl;
	}
}

function FadeObject(sObjectId, sType,  iTo, iSleep, iStep, fCallback)
{
	//log("sobjectid " + sObjectId + " type " + sType + " ito " + iTo + " sleep " + iSleep + " istep " + iStep);
	temp_obj = getObj(sObjectId);
	
	if(temp_obj)
	{
		fade(temp_obj, iTo, iSleep ,iStep);
		
		// Check if there is a callback
		if(fCallback != undefined)
		{
			CheckIfFadeIsDone(sObjectId, sType, iTo, fCallback);
		}
	}
	else
	{
		if(fCallback != undefined)
		{
			// Call the callback
			setTimeout(fCallback,0);
		}
	}
}

// This method will check every 20 milli second if the object has the opacity
function CheckIfFadeIsDone(sObjectId, sType, iTo, fCallback)
{
	// Load the actual opacity of the fade object
	var iActualFade = getOpacityByObjectId(sObjectId);
	
	switch(sType)
	{
		case 'in': // 0 - 100
			if(iActualFade >= (iTo - 1))
			{
				// Call direct the callback
				//log("Call callback");
				setTimeout(fCallback,0);
			}
			else
			{
				// Check the object again after 20 milliseconds
				//log("object: " + sObjectId + " Actual fade: " + iActualFade + " Ito: " + iTo);
				setTimeout(function() {CheckIfFadeIsDone(sObjectId, sType, iTo, fCallback);}, 20);
			}
		break;
		case 'out': // 100 - 0
			if(iActualFade <= (iTo + 1))
			{
				// Call direct the callback
				//log("Call callback");
				setTimeout(fCallback,0);
			}
			else
			{
				// Check the object again after 20 milliseconds
				//log("object: " + sObjectId + " Actual fade: " + iActualFade + " Ito: " + iTo);
				setTimeout(function() {CheckIfFadeIsDone(sObjectId, sType, iTo, fCallback);}, 20);
			}
		break;
	}
}

function FadeObjectToBg(objectId, iTo)
{
	temp_obj = getObj(objectId);
	
	if(temp_obj)
	{
		fade(temp_obj, iTo, 5 ,2);
	}
}

function FastFadeObjectToBg(objectId, iTo)
{
	temp_obj = getObj(objectId);
	
	if(temp_obj)
	{
		fade(temp_obj, iTo, 5 ,1);
	}
}

function UltraFastFadeObjectToBg(objectId, iTo)
{
	temp_obj = getObj(objectId);
	
	if(temp_obj)
	{
		fade(temp_obj, iTo, 1 ,50);
	}
}

function FadeObjectTo(objectId, iTo)
{
	temp_obj = getObj(objectId);
	
	if(temp_obj)
	{
		fade(temp_obj, iTo, 5 ,1);
	}
}

function MakeVisible(objectId)
{
	temp_obj = getObj(objectId);
	
	if(temp_obj)
	{
		temp_obj.style.visibility = 'visible';
	}
}

function MakeHidden(objectId)
{
	temp_obj = getObj(objectId);
	
	if(temp_obj)
	{
		temp_obj.style.visibility = 'hidden';
	}
}

function getObj(objectId)
{
	return document.getElementById(objectId);
}

function isObj(obj)
{
	if(obj) return true;
	return false;
}

function fadeObj(objectId, iFrom, iTo)
{
	fadeObject = document.getElementById(objectId);
	
	if(fadeObject)
	{
		new Effect.Opacity(objectId, { from: iFrom, to: iTo, duration: 0.2 });
	}
}

function fadeOver(objectId)
{
	fadeObject = document.getElementById(objectId);
	
	if(fadeObject)
	{
		//Effect.Fade(objectId, { duration: 3.0 });
		new Effect.Opacity(objectId, { from: 1.0, to: 0.0, duration: 0.2 });
		
		//$(objectId).fade({ duration: 1.0, from: 0, to: 1 });
	}
}

function fadeOut(objectId)
{
	fadeObject = document.getElementById(objectId);
	
	if(fadeObject)
	{
		new Effect.Opacity(objectId, { from: 0.0, to: 1.0, duration: 0.5 });
		//$(objectId).fade({ duration: 1.0, from: 1, to: 0 });
	}
}

function fadeObjectIn(objectId, activeId)
{
	bShowactive = false;
	
	fadeObject = document.getElementById(objectId);
	activeObject = document.getElementById(activeId);
	
	// Hide the active object
	if(activeObject)
	{
		fadeTransparencyDown(activeObject, 0, iFadespeed);
	}
	
	if(fadeObject)
	{
		fadeTransparencyUp(fadeObject, 100, iFadespeed);
	}
}

function fadeObjectOut(objectId, activeId)
{
	fadeObject = document.getElementById(objectId);
	activeObject = document.getElementById(activeId);
	
	if(fadeObject)
	{
		fadeTransparencyDown(fadeObject, 0, iFadespeed);
	}
	
	if(activeObject)
	{
		setTimeout("showActive('"+activeId+"')", 1500);
	}
	
	bShowactive = true;
}

function showActive(activeId)
{
	activeObject = document.getElementById(activeId);
	
	if(bShowactive == true)
	{
		// Show the active photo
		if(activeObject)
		{
			fadeTransparencyUp(activeObject, 100, iFadespeed);
		}
	}
}

function loadImage(sObjectId, sImagePath)
{
	imgObj = document.getElementById(sObjectId);
	
	if(imgObj)
	{
		imgObj.src = sImagePath;
	}
}

function checkInput(obj, value)
{
	if(obj)
	{
		setMessage("");
		
		if(obj.value == value)
		{
			obj.value = "";
		}
		else if(obj.value == "")
		{
			obj.value = value;
		}
	}
}

function isEmpty(obj)
{
	if(obj.value == "") return true;
	return false;
}

function isEmail(obj)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (filter.test(obj.value)) return true;
	return false;
}

function isPostcode(obj)
{
	var filter = /^[1-9]{1}[0-9]{3}[a-zA-Z]{2}$/;
	
	if (filter.test(obj.value)) return true;
	return false;
}

function isSameAsTitle(obj)
{
	if(obj.value == obj.title) return true;
	return false;
}

function focusObject(obj)
{
	obj.focus();
}

function setMessageById(value, messageId)
{
	objMessage = document.getElementById(messageId);
	
	if(objMessage)
	{
		objMessage.innerHTML = value;
	}
}

function setMessage(value, objectId)
{
	objMessage = document.getElementById(objectId);
		
	if(objMessage )
	{
		objMessage.innerHTML = value;
	}
}

function sendContactRequest()
{
	objBericht	= document.getElementById('t_bericht');
	objNaam 	= document.getElementById('t_naam');
	objEmail 	= document.getElementById('t_email');
	
	if(objBericht && objNaam && objEmail)
	{
		if(isEmpty(objBericht) || isSameAsTitle(objBericht))
		{
			focusObject(objBericht);
			setMessage("U bent vergeten het <strong>bericht</strong> in te vullen!","message");
			return;
		}
		
		if(isEmpty(objNaam) || isSameAsTitle(objNaam))
		{
			focusObject(objNaam);
			setMessage("U bent vergeten uw <strong>naam</strong> in te vullen!","message");
			return;
		}
		
		if(isEmpty(objEmail) || isSameAsTitle(objEmail))
		{
			focusObject(objEmail);
			setMessage("U bent vergeten uw <strong>e-mailadres</strong> in te vullen!","message");
			return;
		}
		
		if(!isEmail(objEmail))
		{
			focusObject(objEmail);
			setMessage("Het opgegeven <strong>e-mailadres</strong> is onjuist!","message");
			return;
		}
		
		document.contactForm.submit();
	}
}

function VisibleOn(objectId)
{
	vObject = document.getElementById(objectId);
	
	if(vObject)
	{
		vObject.style.visibility = 'visible';
	}
}

function VisibleOff(objectId)
{
	vObject = document.getElementById(objectId);
	
	if(vObject)
	{
		vObject.style.visibility = 'hidden';
	}
}

Effect.BlindRight = function(element) {
  element = $(element);
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, 100, Object.extend({
    scaleContent: false,
    scaleY: false,
    scaleFrom: 0,
    scaleMode: {originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: true,
    afterSetup: function(effect) {
      effect.element.makeClipping().setStyle({
        width: '0px',
        height: effect.dims[0] + 'px'
      }).show();
    },
    afterFinishInternal: function(effect) {
      effect.element.undoClipping();
    }
  }, arguments[1] || { }));
};

Effect.BlindLeft = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false,
      scaleY: false,
      scaleMode: 'box',
      scaleContent: false,
      restoreAfterFinish: true,
      afterSetup: function(effect) {
        effect.element.makeClipping().setStyle({
          height: effect.dims[0] + 'px'
        }).show();
      },
      afterFinishInternal: function(effect) {
        effect.element.hide().undoClipping();
      }
    }, arguments[1] || { })
  );
};

function getOpacityByObjectId(sObjectId)
{
	temp_object = document.getElementById(sObjectId);
	
	if(temp_object)
	{
		if(this.getComputedStyle) 
		{ 
			getOpacity = function(temp_object) 
			{ 
				return parseFloat(this.getComputedStyle(temp_object, null).getPropertyValue('opacity')) * 100;
			}; 
		} 
		else if(temp_object.filters) 
		{ 
			getOpacity = function(temp_object) 
			{ 
			// return temp_object.filters['DXImageTransform.Microsoft.Alpha'].Opacity; 
				try 
				{
					return temp_object.filters.alpha.Opacity; 
				}
				catch(e){}
			}; 
		} 
		else 
		{ 
			getOpacity = function() {return 100;}; 
		} 
		return getOpacity(temp_object); 
	}
	else
	{
		return 100;
	}
} 