/**
 * file default.js
 *
 * contains javascript for Verachtert website
 *
 * @require MooTools version 1.2.2
 */


window.addEvents({
	'domready': function() {
		initExternalLinks();
		initMainnav();
		initPhotogallery();
		defaultInputText();
		deleteProduct();
		changeOrder();
		initHistoryBack();
	},
	'load': function() {
	}
});



/**
 * initExternalLinks
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author CSD (clientsidedevelopers[AT]efocus.nl)
 * @uses <a href="http://www.efocus.nl/" class="external">eFocus</a>
 */
function initExternalLinks() {
	var arrExternalLinks = $$('a.external');
	if (arrExternalLinks.length == 0) return false;
	
	arrExternalLinks.each(function(elExternalLink) {
		elExternalLink.addEvent('click', function(event) {
			event.stop();
			window.open(this.get('href'));
		});
	});
}


/**
 * initMainnav
 *
 * initializes Main navigation mouse behaviour
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @author Lowen Fan <lowen(AT)efocus.nl>
 * @return void
 */
function initMainnav() {
	if(!document.getElement('ul.mainnav')) return false;
	
	document.getElements('ul.mainnav li').each(function(item) {
		if(item.getElement('ul')) {
			item.getElement('ul').fade('hide');
			item.addEvents({
				'mouseenter': function() {
					item.getElement('ul').fade('show');
					
				},
				'mouseleave': function() {
					item.getElement('ul').fade('hide');
				}
			});
		}
	});
	
	document.getElements('ul.mainnav').each(function(item) {
		if(item.getElement('ul')) {
			
			var arrVideoplayers = document.getElements('div.videoplayer')
			
			item.getElement('ul').fade('hide');
			item.addEvents({
				'mouseenter': function() {
					arrVideoplayers.each(function(elVideoplayer) { 
						elVideoplayer.setStyle('visibility', 'hidden')
					});
					
				},
				'mouseleave': function() {
					arrVideoplayers.each(function(elVideoplayer) { 
						elVideoplayer.setStyle('visibility', 'visible')
					});
				}
			});
		}
	});	
}




/**
* defaultInputText
*
* toggles default text in text inputfields
*
* @author Klaas Dieleman <klaas{AT}efocus.nl>
* @return void
*/
 
function defaultInputText() {
	var inputfields = $$('.defaulttext');
	
	inputfields.each(function(item) {
		item.defaultText = item.value;
		item.addClass('default');
		
		item.addEvents({
			'focus': function() {
				if (item.value == item.defaultText) item.value = '';
				item.removeClass('default');
			},
			'blur': function() {
				if (item.value == '') {
					item.value = item.defaultText;
					item.addClass('default');
				}
			}
		});
	});
}


/**
* initPhotogallery
*
* toggles photos on occasion detailpage
*
* @author Klaas Dieleman <klaas{AT}efocus.nl>
* @author Rouhun Fan <lowen{AT}efocus.nl>
* @return void
*/
 
function initPhotogallery() {
	if(!document.getElement('div.photo_occasion')) return false;
	
	var photos = $$('div.photo_occasion img.photo');
	var thumbs = $$('div.photo_occasion ul.thumbnails img');
	
	photos.hide();
		
	if (thumbs.length == 0) return false;

	photos.hide();
	photos[0].show();
}


/**
* changeSearchType
*
* toggles product and variant on and of in search form depending on type
*
* @author Anneke <Anneke{AT}efocus.nl>
* @return void
*/
 
function changeSearchType(i) {

	if(i == 'occasion') {
		
		// hide pulldowns (product en variant)
		$('pulldown4').setStyle('display', 'none');
		$('pulldown5').setStyle('display', 'none');
		
	} else {
		
		// show pulldowns (product en variant)
		$('pulldown4').setStyle('display', 'block');
		$('pulldown5').setStyle('display', 'block');
	}
	
}

/**
* showProductspecifications
*
* toggles productspecification
*
* @author Anneke <Anneke{AT}efocus.nl>
* @return void
*/
 
function showProductspecifications() {
	
	var w = $('productspecification_table').getStyle('display');
	
	if(w=='none') {
		$('productspecification_table').setStyle('display', 'block');
	} else {
		$('productspecification_table').setStyle('display', 'none');
	}
		
}

/*
* changeOrder
*
* based on the input:
* saves a order and create a new line or
* empties all orders or
* sends the order
* 
* @author Lowen <lowen{AT}efocus.nl>
* @return void
*/

function changeOrder() {
	if(!$(document.body).getElement('div.nav_bestel'));

	var arrOrderButtons = $(document.body).getElements('div.nav_bestel div.button a');
	
	arrOrderButtons.each(function(elOrderButton){
		elOrderButton.addEvent('click', function(event){
			event.stop();
			var strFormAction = $(document.body).getElement('form.contactform').getProperty('action');
			
			if (strFormAction.contains('bestelformulier')) {
				if (elOrderButton.hasClass('saveform')) {
					$('opslaan').value='1';
				}
				else if (elOrderButton.hasClass('emptyform')) {
					$('legen').value='1';
				}
				
				document.orderform.submit();
			}
		});
	});
}


/**
* deleteProduct
*
* deletes a product in the order form
*
* @author Lowen <lowen{AT}efocus.nl>
* @return void
*/
function deleteProduct() {
	var arrButtonDeleteProduct = $(document.body).getElements('a.delete_product');
	
	if (!arrButtonDeleteProduct) return false;
	
	arrButtonDeleteProduct.each(function(elButtonDeleteProduct) {
		elButtonDeleteProduct.addEvent('click', function(event) {
			event.stop();
			$('verwijder').value = this.getProperty('href');
			document.orderform.submit();
		});
	});
}


/**
* initHistoryBack
*
* Go back in browserpage history
*
* @author Lowen <lowen{AT}efocus.nl>
* @return void
*/

function initHistoryBack() {
	var arrBackLinks = $(document.body).getElements('a.back');
	
	if (!$(document.body).getElements('a.back')) return false;
	
	arrBackLinks.each(function(elBackLink){
		elBackLink.addEvent('click', function(event){
			event.stop();
			history.go(-1);
		});
	});
}