/**
 * @author arjen
 */


/**
 * addLoadEvent
 * 
 */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() { 
			oldonload();
			func();
		}
	}
}


/**
 * externalLinks
 * 
 */
function externalLinks() {
	if (!document.getElementsByTagName) return;	
	var anchors = document.getElementsByTagName("a");	
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";
	}
}



/**
 * image overlay
 * @author AK
 * @version 0.1 (namaste)
 * 
 */
function _peppered_imageOverlay(elements) {
	//console.log((elements));
	if (!$(elements).get(0)) return false;

	var ie_alpha = false;
	e = $(elements).get(0);
	if (e.runtimeStyle && typeof(e.runtimeStyle.filter) != 'undefined')
		ie_alpha = true;
		
	if (!e.runtimeStyle || ie_alpha == true) {

		w = $('#content img').width() + 2;		// plus default img border
		h = $('#content img').height() - 10;	// minus space for bottom part of overlay
		/* please note: safari&opera need img width&height set for these to work correctly! */
		
		$('#content img').wrap('<div class="imgcontainer"></div>');
		e = $('#content img').parent().append('<div class="overlay"></div>');
		$('#content div.imgcontainer div.overlay').css({ width: w+"px", height: h+"px"});
		$('#content div.imgcontainer div.overlay').append('<div></div>');
		
		if (ie_alpha) {
			e= $('#content div.imgcontainer div.overlay').get(0);
			e.style.background = 'none';
			e.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + 'images/imgoverlay_289.png' + "',sizingMethod='image')";
			e= $('#content div.imgcontainer div.overlay div').get(0);
			e.style.background = 'none';
			e.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + 'images/imgoverlay_289_bottom.png' + "',sizingMethod='image')";
		}

	}
}


// pop-up

/* 16-7-07: added js=true get var, W&H optional (AK) */

function popPage(strLocation, intW, intH)
{
	// to do: checken screenW/H en window op plek zetten...
	
	// adding some pixels for window-borders, title-bars, padding etc.
	var intCorrectedW = intW;	
	//var intCorrectedW = intW;	
	var intCorrectedH = intH+10;	
	//var intCorrectedH = intH;	
	
	//var strParams = 'status=0,resizable=0,width='+intCorrectedW+',height='+intCorrectedH;
	
	var strParams = 'status=1,resizable=1';
	if (intW != undefined)
		strParams += ',width='+intCorrectedW;
	if (intH != undefined)
		strParams += ',height='+intCorrectedH;
		
	strLocation += '&js=true';
	
	window.objPopWindow = window.open(strLocation,'popWindow',strParams);

	if (intW != undefined && intH != undefined) {
		window.objPopWindow.resizeTo(intCorrectedW,intCorrectedH);
	}
	
	window.objPopWindow.focus();

	// determining the available width & height
	if (intW != undefined && intH != undefined) {
		var intAvailW = 0;
		var intAvailH = 0;
		if (self.innerWidth)
		{
			intAvailW = self.innerWidth;
			intAvailH = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			intAvailW = document.documentElement.clientWidth;
			intAvailH = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			intAvailW = document.body.clientWidth;
			intAvailH = document.body.clientHeight;
		}
		
		// calculate the new position, minus some zutt for window borders etc.
		var intTargetX = ((intAvailW-intW)/2);
		var intTargetY = ((intAvailH-intH)/2)-0;
		
		if (intTargetX<0) intTargetX = 0;
		if (intTargetY<0) intTargetY = 0;
		//alert(intAvailW);
		window.objPopWindow.moveTo(intTargetX,intTargetY);
	}
}


/* -- DOM ready js exec -- */

$(function(){
	/** 
	 * jquery 1.2.1
	 * @author AK
	 * 
	 */ 
	
	
	/**
	 * fotoslide
	 */
	
	if ($('.fotoslide').get(0)) {
		var sId = $('.fotoslide').attr('id');
		var aIdParts = sId.split('-');
		var iReisId = parseInt(aIdParts[1]);
		var so = new SWFObject("/flash/slideshow.swf?reis_id=" + iReisId, "slideshow2", "280", "240", "6", "#ffffff");
		so.addParam("quality", "high");
		so.addParam("wmode", "transparent");
		so.addParam("salign", "t");
		so.addVariable("reis_id", iReisId);
		if (so.write(sId))
			$('#'+sId).addClass('fotoslideActive');
	}
	
	// link to js only styles
	var css = document.createElement("link");
	css.setAttribute("href",'/css/javascript.css');
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	document.getElementsByTagName("head")[0].appendChild(css);
	 
	 $('body').addClass('js');
	 
	 //- external links
	 externalLinks();
	 
	 //- tooltips
	 $('td.klasse').Tooltip();
	 
	  //- content menu
	 $('ul#contentMenu li.active').prev().addClass('activePrev');
	 $('ul#contentMenu li.active').next().addClass('activeNext');
 	 
	 $('#voorbeeldreizenTabel td.klasse').wrapInner('<span class="ir IR-hover"></span>');
	 $('dd#hotel-def dd.klasse').addClass('ir IR-hover klasseIRActive');
	 
	/**
	  *  Image replacement
	  * adds extra spans after text
	  * styled applied from javascript.css
	  * 
	  */
	 
	 /* servicemenu enh. */
	
	 // preload curl over 
	 jQuery("<img>").attr("src", '/images/servicemenu_curl_over.gif');
	 $('#servicemenu-home a').hover (
	 	function(){$('#servicemenuContainer').addClass('homeHover')},
		function(){$('#servicemenuContainer').removeClass('homeHover')}
	);
	 
	
	// Image replace
	
	$('.ir').append('<span class="ir-aid"></span>');				// single element IR
	$('.anchorIR a').append('<span class="ir-aid"></span>');		// descendant anchors of an element
	$('.ir.IR-hover span.ir-aid, .anchorIR.IR-hover a span.ir-aid').append('<span></span>');	// append another span when it has a hover state	 
	
	$('.ir').addClass('irActive');
	 
	//- voorbeeldreizenTabel row click & hover
	
	$('#voorbeeldreizenTabel tr').click(function(){
		sHref = $(this).children('td.titel').children('a').attr('href');
		document.location.href = sHref;
	});
	
	$('#voorbeeldreizenTabel tr').hover(function(){
			$(this).addClass('hover');
			$(this).children('td.klasse').children('span.IR-hover').addClass('IR-hovered');
		},
		function () {
			$(this).removeClass('hover');
			$(this).children('td.klasse').children('span.IR-hover').removeClass('IR-hovered');
		}
	);
	
	
	$('dd#hotel-def dl').click(function(){
		//sHref = $(this).children('dd.naam').children('a').attr('href');
		//document.location.href = sHref;
		$(this).children('dd.naam').children('a').click();
	});
	
	$('dd#hotel-def dl').hover(function(){
			$(this).addClass('hover');
			$(this).children('.klasse').addClass('IR-hovered');
		},
		function () {
			$(this).removeClass('hover');
			$(this).children('.klasse').removeClass('IR-hovered');
		}
	);
	
	
	// enable thickbox for hotel anchors

	$('dd#hotel-def dd.naam a').each(function() {
		sHref = $(this).attr('href');
		$(this).addClass('thickbox');
		sHref = sHref + '&KeepThis=true&TB_iframe=true&height=480&width=520';
		$(this).attr('href', sHref);
	});
	

	// enable thickbox for nb archief

	$('#bodyNieuwsbrief #nieuwsbriefArchiefLijst td a').each(function() {
		sHref = $(this).attr('href');
		$(this).addClass('thickbox');
		iSH = screen.height;
		iH = iSH - 350;
		sHref = sHref + '?KeepThis=true&TB_iframe=true&height='+ iH +'&width=620';
		$(this).attr('href', sHref);
		
	});
		
	
	// peppered_imageOverlay('#cosntent .subColumn img');
	
		
	/**
	 * Offerte formulier
	 */
	$('#formOfferte #formOfferte-referer input[type=radio]').click( function(){
		$('#formOfferte #formOfferte-referer-anders').parent().children('.focus-receiver').addClass('inactive');
	});
	
	$('#formOfferte #formOfferte-referer-anders').click( function() {
		$(this).parent().children('.focus-receiver').removeClass('inactive');
		$(this).parent().children('.focus-receiver').focus();
	});
	
	$('#formOfferte #formOfferte-referer-anders-tekst').click( function() {
		$(this).parent().children('.focus-trigger').attr('checked', 'checked');
		$(this).removeClass('inactive');
	});		
		
	$('form input.disabled').attr('disabled', 'disabled');
	

	/**
	 * Reisverslag/reisprogramma submemnu
	 */	

	$('#subMenu').addClass('js');
	$('#subMenu dt').wrapInner('<a href="#"></a>'); // our friend ie6 needs it
	
	$('#subMenu dt a').click( function(){
		var oParent = $(this).parent();
		var sId = oParent.attr('id');
		
		var aId = sId.split('-');
		var sDefId = aId[0] + '-def';

		if($('#'+sDefId).css('display') == 'block') {			
			oParent.removeClass('active');
			$('#'+sDefId).removeClass('active');
		}
		else {
			//oParent.siblings('dt,dd').removeClass('active');
			oParent.addClass('active');
			$('#'+sDefId).addClass('active');
		}
		return false;
	} );
	
	//- show opened on certain hashes
	var sHash = window.location.hash;
	if (sHash && $('#subMenu ' + sHash).get(0)) {
		var aId = sHash.split('-');
		var sDefId = aId[0] + '-def';
		$('#subMenu ' + sHash).addClass('active');
		$(sDefId).addClass('active');
	}

	// always open "foto" and "contact"
	$('#subMenu #hotel-term, #subMenu #hotel-def, #subMenu #fotos-term, #subMenu #fotos-def').addClass('active');
	
	//- print anchor
	$('#menuReisverslag #menuReisverslag-home').after('<li class="left" id="menuReisverslag-print"><a href="javascript:window.print()">Print deze pagina</a></li>');
	
	// hotel pop-up
	// superceded by thickbox
	
	/*
	$('#hotel-def a').click( function() {			
		popPage(this.href, 535, 500);
		return false;
	});
	*/
	
	/* reisinformatie */
	
	var sTermId;
	
	$('#reisInformatieMenu dd').addClass('jsfx');
	$('#reisInformatieMenu dt').wrapInner('<span class="jsfx"></span>')
	$('#reisInformatieMenu dd').wrapInner('<span class="jsfx"></span>')

	
	$('#reisInformatieMenu dt, #reisInformatieMenu dd').hover(function(){
			$(this).addClass('hover');
			sE = $(this).get(0).nodeName.toLowerCase();
			if (sE == 'dt') {
				$(this).next().addClass('hover');
			}
			else if (sE == 'dd') {
				$(this).prev().addClass('hover');
			}			
		},
		function () {
			$(this).removeClass('hover');
			sE = $(this).get(0).nodeName.toLowerCase();
			if (sE == 'dt') {
				$(this).next().removeClass('hover');
			}
			else if (sE == 'dd') {
				$(this).prev().removeClass('hover');
			}
		}
	);
	
	$('#reisInformatieMenu dt').click(function() {
		sHref = $(this).children().children('a').eq(0).attr('href');
		document.location.href = sHref;
	});
	$('#reisInformatieMenu dd').click(function() {
		sHref = $(this).prev().children().children('a').eq(0).attr('href');
		document.location.href = sHref;
	});
	
});


