////////////////////////////////////////////////////////////////////////////////////////////////////
// Stuff for the popup and message box with a locked (disabled) webpage
//
// IMPORTANT: these functions don't work without prototype.js!!
//

////////////////////////////////////////////////////////////////////////////////////////////////////
// Contanten
var MT_INFORMATION = 0;
var MT_YESNO = 1;
var MT_CUSTOM = 99;

////////////////////////////////////////////////////////////////////////////////////////////////////
// Global variables
var popupWindow = null;
var lockedWindow = null;
var config = null;
var configInitiated = false;

////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialise the config variables
// 
function addedProductHandler(text) {
	//$('idBestellijst').show();
	arrButtons = new Array();
	arrButtons[0] = '<button class="buttonback" id="btnCancel" onclick="hidePopup();">Verder winkelen</button>';
	arrButtons[1] = '<button class="button" id="btnOk" onclick="showBestellijst(); hidePopup();">Naar kassa</button>';
	showMessageBox(text, arrButtons, 'btnOk');
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Show the custom message box after a product is deleted from the shopping card
// This is an event handler that's fired after a product is deleted succesfully
//
function deletedProductHandler(transport) {
	showBestellijst();
	exit;
	//$('idBestellijstImg').show();
	
	arrButtons = new Array();
	arrButtons[0] = '<button id="btnOk" onclick="showBestellijst(); hidePopup();">Terug naar de bestellijst</button>';
	showMessageBox(transport.responseText, arrButtons, 'btnOk');
}

function showBestellijst() {
	window.location="?PageName=ShowBestellijst&pageid=-1";
};

/**
 * Olaf Luijks []
 * 
 * Stel een vraag
 */
function articleInfoHandler(text) {
	arrButtons = new Array();
	arrButtons[0] = '<button id="btnOk" onclick="sendArticleInfoEmail();">Terug naar de bestellijst</button>';
	showMessageBox(text, arrButtons, 'btnOk');
}

function sendArticleInfoEmail() {
	/* window.location="?PageName=ShowBestellijst&pageid=-1"; */
};


function initPopup(idParent, opacity) {
	config = new Object();
	config.mainContainer = $(idParent);
	if (!config.mainContainer) {
		alert("Error in website: main container is invalid!");
		return;
	}
	config.opacity = opacity;
	configInitiated = true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Lock the window (by show a div element over all other elements with opacity)
//
function lockWindow() {
	// Initieer de lockedWindow
	if (lockedWindow == null) {		
		lockedWindow = new Element('div', { id:'idDisabler' });
		lockedWindow.setStyle({
			position: 'absolute',
			top: 0,
			left: 0,
			display: 'none',
			zIndex: +600
		});
		lockedWindow.setOpacity(config.opacity);
		document.body.appendChild(lockedWindow);
	}
	
	// Wat zijn de dimensies van mijn viewport
	mydims = document.viewport.getDimensions();
	
	// Zet de breedte en hoogte van hetgeen dat gelocked moet worden
	//lockHeight = $('idTheEnd').positionedOffset().top;
	//alert(lockHeight + ',' + config.mainContainer.getHeight());
	lockHeight = config.mainContainer.getHeight();
	if (lockHeight < mydims.height) { lockHeight = mydims.height; }
	else { lockHeight = lockHeight + 20; }
	lockedWindow.setStyle({ width:mydims.width+'px', height:lockHeight+'px' });
	lockedWindow.show();
	
	hideSelectBoxes();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Unlock the window (by removing the div element)
//
function unlockWindow() {
	if (lockedWindow != null) {
		lockedWindow.hide();
	}
	
	showSelectBoxes();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Hide the popup window AND the lockedWindow
// Noot: The lock window is used to disbable all actions, buttons, links on the website 
//       except the popup window
// Noot: for IE6: we have to show all hidden selectboxes because they won't go to the background!
//
function hidePopup() {
	if (popupWindow != null) {
		popupWindow.hide();
	}
	
	unlockWindow();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Show the popup AND lockedWindow
// Noot: The lock window is used to disbable all actions, buttons, links on the website 
//       except the popup window
// Noot: for IE6: we have to hide all hidden selectboxes because they won't go to the background!
//
function showPopup(strContents) {
	if (!configInitiated) {
		alert("Error in website: use of popup without initialisation");
		return;
	}
	
	lockWindow();
	
	// Initieer de popupWindow
	if (popupWindow == null) {
		popupWindow = new Element('div', { id:'idPopupWindow' });
		popupWindow.setStyle({
			position: 'absolute',
			top: 0,
			left: 0,
			display: 'none',
			zIndex: +700
		});
		document.body.appendChild(popupWindow);
	}
	
	// Wat zijn de dimensies van mijn viewport
	mydims = document.viewport.getDimensions();
	myoffsets = document.viewport.getScrollOffsets();
	
	// Zet de message (contents) in messagewindow en toon deze
	popupWindow.innerHTML = strContents;
	popupWindow.setStyle({
		'left': Math.floor((mydims.width - popupWindow.getWidth()) / 2) + 'px',
		'top':  Math.floor(myoffsets.top + (mydims.height - popupWindow.getHeight()) / 2) + 'px'
	});
	
	popupWindow.show();
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Show a messagebox with custom buttons
//
function showMessageBox(strMessage, arrButtons, idDefaultButton) {
	// Stel de buttons samen
 	closeButton = '<p class="buttons">';
	closeButton = closeButton + arrButtons[0]; // Er moet minimaal 1 button zijn!!!
	for (idx = 1; idx < arrButtons.length; idx++) {
		closeButton = closeButton + '&nbsp;' + arrButtons[idx];
	}
	closeButton = closeButton + '</p>';
	
	// en toon de popup (messagebox)
	showPopup(strMessage + closeButton);
	
	if (idDefaultButton != undefined) {
		$(idDefaultButton).focus();
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Show a message box with default buttons
//
function showMessage(strMessage, iMessType, strOkAction) {
	// Initieer de default waarden
	if (iMessType == undefined) iMessType = 0;
	if (strOkAction == undefined) strOkAction = '';
	
	// Voeg de buttons toe aan de messagewindow inhoud
	arrButtons = new Array();
	idx = 0;
	if (iMessType == MT_INFORMATION) {
		arrButtons[idx] = '<button class="button" id="btnOk" onClick="hidePopup();'+strOkAction+'" accesskey="s">Sluiten</button>';
	} else if (iMessType == MT_YESNO) {
		arrButtons[idx] = '<button class="button" id="btnOk" onClick="hidePopup();'+strOkAction+'" accesskey="j">Ja</button>';
		idx++;
		arrButtons[idx] = '<button class="button" id="btnCancel" onClick="hidePopup();" accesskey="n">Nee</button>';
	}
	
	// Toon de messagebox
	showMessageBox('<p style="padding:5px; cursor:pointer;" onClick="hidePopup();">'+strMessage+'</p>', arrButtons, 'btnOk');
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Show a message box with default buttons
//
function showImageMessage(imgurl, maxWidth, maxHeight) {
	//strMessage = '<table style="width:912px;"><tr><td style="text-align:center;"><img alt="" src="'+imgurl+'" onload="if (this.width>912) {this.style.width=\'912px\';}" /></td></tr></table>';
	myImage = new Image();
	myImage.src = imgurl;
	
	strMessage = '<img alt="" src="'+imgurl+'"';
	if (maxWidth) { 
		strMessage = strMessage + ' width="' + maxWidth + '"';
	}

	if (maxHeight) {
		strMessage = strMessage + ' height="' + maxHeight +'"';
	}
	strMessage = strMessage + ' />';
	
	if (myImage.complete) {
		showMessage(strMessage);
	}else{
		myImage.onload = function(event) {
			showMessage(strMessage);
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Hide all selectboxes
//
function hideSelectBoxes() {
	// Only for ie6
	if (navigator.userAgent.search(/MSIE 6/) == -1) {
		return;
	}
	
	arrElems = $$('select');
	
	for(idx = 0; idx < arrElems.length; idx++) {
		arrElems[idx].hide();	
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Show all selectboxes
//
function showSelectBoxes() {
	// Only for ie6
	if (navigator.userAgent.search(/MSIE 6/) == -1) {
		return;
	}
	
	arrElems = $$('select');
	
	for(idx = 0; idx < arrElems.length; idx++) {
		arrElems[idx].show();	
	}
}

function updateShoppingCardRow(s) {
	new Ajax.Updater('idAantalArtikelen', s+'/bestellijst_overzicht.php', { method:'get' } );
}

// IFrame in site van dealers moet de iframe resizen. Dat gaat alleen via javascript
var iFrameHeight = -1;
var objHiddenFrame = null;
var strWebsite = '';
	
function resizeTopWindow() {	
	if (objHiddenFrame && strWebsite != '') {
			objHiddenFrame.src = "http://"+strWebsite+"/gotowebshop.html?resizeframe=" + iFrameHeight;
	}
}

function initHiddenFrame(idFrame, website) {
	if (document.getElementById(idFrame)) { 
		objHiddenFrame = document.getElementById(idFrame);
		strWebsite = website;
		setInterval(
			function () {
				if ($('wrap')) {
					iHeight = $('wrap').getHeight();
					if (iHeight != iFrameHeight) {
						iFrameHeight = iHeight;
						resizeTopWindow();
					}
				}
			},
			500
		);
	}
}

var iFrameHeightAanbiedingen = -1;
var objHiddenFrameAanbiedingen = null;
var strWebsiteAanbiedingen = '';
	
function resizeTopWindowAanbiedingen() {
	if(objHiddenFrameAanbiedingen && strWebsiteAanbiedingen != ''){		
		objHiddenFrameAanbiedingen.src = "http://"+strWebsiteAanbiedingen+"/gotowebshopaanbiedingen.html?resizeframe=" + iFrameHeightAanbiedingen;		
	}
}

function initHiddenFrameAanbiedingen(idFrameAanbiedingen, websiteAanbiedingen) {
	if (document.getElementById(idFrameAanbiedingen)) { 
		objHiddenFrameAanbiedingen = document.getElementById(idFrameAanbiedingen);
		strWebsiteAanbiedingen = websiteAanbiedingen;		
		setInterval(
			function () {
				if ($('wrapAanbiedingen')) {
					iHeightAanbiedingen = $('wrapAanbiedingen').getHeight();
					if (iHeightAanbiedingen != iFrameHeightAanbiedingen) {
						iFrameHeightAanbiedingen = iHeightAanbiedingen;
						resizeTopWindowAanbiedingen();						
					}
				}
			},
			500
		);
	}
}

