
/**
 * Check if object is defined 
 * 
 * @param object obj Object to check if existing
 * @return boolean Is defined yes/no
 */
function isDefined(obj) {
	return (obj != undefined);
}

Object.extend(document, {
    isDocReady: false,
    isDocLoaded: false,
    ready: function(fn) { Event.observe(document, "doc:ready", fn); },
    load: function(fn) { Event.observe(document, "doc:loaded", fn); }
});
Event.observe(document, "dom:loaded", function() {
    Event.fire(document, "doc:ready");
    document.isDocReady = true;
    if (document.isDocLoaded)
        Event.fire(document, "doc:loaded");
});
Event.observe(window, "load", function() {
    document.isDocLoaded = true;
    if (!document.isDocReady) return;
    Event.fire(document, "doc:loaded");
});

/**
 * Check if current input is a number 
 * 
 * @param event
 * @return
 */
function checkNumber(event) {
	if(document.all)
		keycode = event.keyCode;
	else
		keycode = event.which;
	
	if ( keycode == 188 )
		return false;
	
	if((keycode >=48 && keycode <= 58) || (keycode >=96 && keycode <= 105) || keycode==8 || keycode==188 || keycode==9 || keycode==107 || keycode==109 || keycode == 189 || keycode == 37 || keycode == 39) {
		return true;
	}
	else {
		return false;
	}
}


/**
 * Toggle div or hide if necessary
 * 
 * @param string el Element name which needs to be toggled 
 * @param boolean hide Do we need to hide the field, if set hide is enforced
 */
function toggleDiv(el,hide) {
	// Check if element is there, if not return 
	if ( !isDefined($(el)) ) {
		return;
	}
	
	// If hide is not set, check current state
	if ( hide == undefined ) { 
		if ( $(el).getStyle('display') == 'block' ) 
			hide = true;
		else 
			hide = false;
	}

	// Use Scriptaculous effect
	if ( ($(el).getStyle('display') == 'block' ) && !hide ) {
		// Open but hide prohibited
	}
	else if ( ($(el).getStyle('display') == 'none' || $(el).getStyle('display') == '' ) && hide ) { 
		// Already closed so dont toggle
	}
	else {
		Effect.toggle(el, 'blind', {duration: 0.7});
	}
}

/**
 * Toggle menu item
 * 
 * @param string el Menu item name which needs to be toggled 
 */
function toggleMenu(el) {
	setPhpMenu(el);
	toggleMenuImage(el);
	toggleDiv('sub-'+el);
}

/**
 * Toggle menu image 
 * 
 * @param string el Menu item name which needs to be toggled
 */
function toggleMenuImage(el) {
	var block = 'sub-'+el;
	if ( $(block).getStyle('display') == 'none' )
		$(el).addClassName('togglemenu_open');
	else 
		$(el).removeClassName('togglemenu_open');
}

/**
 * Update php status
 * 
 */
function setPhpMenu(el) {
	if ( $('sub-'+el).getStyle('display') == 'block' ) 
		var status = 0;
	else 
		var status = 1;
	
	var url = '/cookie.php?menu='+el+'&status='+status; 
	
	new Ajax.Request( url, {
		method: 'post'
		,asynchronous:true
		,evalScripts:true
	});	
}

/**
 * In place edit function
 * 
 * @param string el Element name which needs to be edited
 * @param string url Url which will be used to update the field
 */
function inplaceEdit(el,url,additionalControl) {
	if ( additionalControl ) {
		new Ajax.InPlaceEditor($(el), url, {
			highlightcolor: '#E9F3FF'
			, cancelControl: 'button'
			, externalControl: additionalControl
			, savingText: 'Saving…' 
			, highlightendcolor: '#FFFFFF'
			, ajaxOptions: {method: 'get'}
		});
	}
	else {
		new Ajax.InPlaceEditor($(el), url, {
			highlightcolor: '#E9F3FF'
			, cancelControl: 'button'
			, savingText: 'Saving…' 
			, highlightendcolor: '#FFFFFF'
			, ajaxOptions: {method: 'get'}
		});
	}
}

/**
 * Fade in div or hide if necessary
 * 
 * @param string el Element name which needs to be toggled 
 * @param boolean hide Do we need to hide the field, if set hide is enforced
 */
function fadeinDiv(el,hide) {
	// Check if element is there, if not return 
	if ( !isDefined($(el)) ) {
		return;
	}
	
	// If hide is not set, check current state
	if ( hide == undefined ) { 
		if ( $(el).getStyle('display') == 'block' ) 
			hide = true;
		else 
			hide = false;
	}

	// Use Scriptaculous effect
	if ( ($(el).getStyle('display') == 'block' ) && !hide ) {
		// Open but hide prohibited
	}
	else if ( ($(el).getStyle('display') == 'none' || $(el).getStyle('display') == '' ) && hide ) { 
		// Already closed so dont toggle
	}
	else {
		Effect.Appear(el, 'blind', {duration: 0.7});
	}
}

/**
 * Fadout div or hide if necessary
 * 
 * @param string el Element name which needs to be toggled 
 * @param boolean hide Do we need to hide the field, if set hide is enforced
 */
function fadeoutDiv(el,hide) {
	// Check if element is there, if not return 
	if ( !isDefined($(el)) ) {
		return;
	}
	
	// If hide is not set, check current state
	if ( hide == undefined ) { 
		if ( $(el).getStyle('display') == 'block' ) 
			hide = true;
		else 
			hide = false;
	}

	// Use Scriptaculous effect
	if ( ($(el).getStyle('display') == 'block' ) && !hide ) {
		// Open but hide prohibited
	}
	else if ( ($(el).getStyle('display') == 'none' || $(el).getStyle('display') == '' ) && hide ) { 
		// Already closed so dont toggle
	}
	else {
		Effect.Fade(el, 'blind', {duration: 0.7});
	}
}

function toggle(id,srcElement)
{
	var element = document.getElementById(id);
		
	if(element.style.display=='none')
	{
		element.style.display = '';
		srcElement.style.background = 'url("/img/bg_sub_min.gif")';
		srcElement.style.backgroundRepeat = 'no-repeat';
		srcElement.style.backgroundColor = '#E2EBF7';
	}
	else
	{
		element.style.display = 'none';
		srcElement.style.background = 'url("/img/bg_sub.gif")';
		srcElement.style.backgroundRepeat = 'no-repeat';
		srcElement.style.backgroundColor = '#E2EBF7';
	}
}

function countTextarea(el,numChar) {
	if ( !$('counter'+el) ) {
		$(el).insert({after: '<div id="counter'+el+'"></div>'});
	}
	if ( $F(el).length >= numChar ) {
		$(el).value = $F(el).substring(0,numChar);
		$('counter'+el).addClassName('count-limit');
		$('counter'+el).removeClassName('count-safe');
	} else {
		$('counter'+el).addClassName('count-safe');
		$('counter'+el).removeClassName('count-limit');
	}
	$('counter'+el).update( $F(el).length + '/' + numChar);
}

function makeCounter(el,numChar) {
	if ( $(el) ) {
		Event.observe($(el), 'keyup', function(){countTextarea(el, numChar);}, false);
		Event.observe($(el), 'keydown', function(){countTextarea(el, numChar);}, false);
		countTextarea(el,numChar);
	}
}

function travelDetail(el) {
	
	if ( el != 'singlevakantieintkort' ) {
		$('about1').addClassName('over-deze-vakantie');
		$('about1').removeClassName('over-deze-vakantie_actief');
		toggleDiv('singlevakantieintkort',true);
	} else {
		$('about1').addClassName('over-deze-vakantie_actief');
		$('about1').removeClassName('over-deze-vakantie');		
	}
	
	if ( el != 'activiteiten' ) {
		$('about2').addClassName('over-deze-vakantie');
		$('about2').removeClassName('over-deze-vakantie_actief');
		toggleDiv('activiteiten',true);		
	} else {
		$('about2').addClassName('over-deze-vakantie_actief');
		$('about2').removeClassName('over-deze-vakantie');		
	}
	
	if ( el != 'accomodatie' ) {
		$('about3').addClassName('over-deze-vakantie');
		$('about3').removeClassName('over-deze-vakantie_actief');
		toggleDiv('accomodatie',true);
		//toggleDiv('accomodationimages',true);
		//toggleDiv('travelimages',false);
		
		if($('accomodationimages')){
			$('accomodationimages').hide();
			$('travelimages').show();
		}
	} else {
		$('about3').addClassName('over-deze-vakantie_actief');
		$('about3').removeClassName('over-deze-vakantie');	
		//toggleDiv('accomodationimages',false);
		//toggleDiv('travelimages',true);
		if($('accomodationimages')){
			$('travelimages').hide();
			$('accomodationimages').show();
		}
	}
	
	if ( el != 'belangrijk' ) {
		$('about4').addClassName('over-deze-vakantie');
		$('about4').removeClassName('over-deze-vakantie_actief');
		toggleDiv('belangrijk',true);		
	} else {
		$('about4').addClassName('over-deze-vakantie_actief');
		$('about4').removeClassName('over-deze-vakantie');		
	}
	
	if ( el != 'dataenprijzen' ) {
		$('about5').addClassName('over-deze-vakantie_l');
		$('about5').removeClassName('over-deze-vakantie_l_actief');
		toggleDiv('dataenprijzen',true);		
	} else {
		$('about5').addClassName('over-deze-vakantie_l_actief');
		$('about5').removeClassName('over-deze-vakantie_l');		
	}
	
	toggleDiv(el,false);
	if ( $('head-bread') )
		Effect.ScrollTo($('head-breed'));
	

}

