/**
 * @author arjen
 */

if (!window.console) { window.console = { log: function(){}, debug: function(){} }; }


/**
 * 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);
	}
}


/**
 * PopupWindow object
 * spawns/handles ye classic popup windows
 *
 * @author AK
 *
 * requires: jQuery 1.2.x, 1.3.x
 */
window.PopupWindow = function(){
	this.width = 500;
	this.height = 500;
	this.$container = $(window);
	this.offsetLeft = 0;
	this.offsetTop = 0;
	this.menubar = 'no';
	this.location = 'yes';
	this.resizable = 'yes';
	this.scrollbars = 'yes';
	this.status = 'yes';
	this.name = 'popupWindow';
	this.url = '';
};
window.PopupWindow.prototype.prepare = function(){
	var oAnchor = this.anchor;
	if (typeof oAnchor.data('popWinObject') == 'object') {
		var oPopWin = oAnchor.data('popWinObject');
		if (!oPopWin.closed) {
			oPopWin.close();
		}
	}
	var sHref = oAnchor.attr('href');
	if (sHref.indexOf('?') > -1) {
		sHref += '&';
	} else {
		sHref += '?';
	}
	sHref += 'popup=true';
	this.url = sHref;
};
window.PopupWindow.prototype.spawn = function(){
	this.prepare();
	var x = this.$container.width() / 2 - (this.width / 2);
	var y = this.$container.height() / 2 - (this.height / 2);
	
	if (x < 0) { x = 0; }
	if (y < 0) { y = 0; }
	
	if (typeof window.screenX !== 'undefined') {
		x += window.screenX;
		y += window.screenY;
	}
	else if (typeof window.screenLeft !== 'undefined') {
		x += window.screenLeft;
		y += window.screenTop / 2;
	}
	
	this.offsetLeft = x;
	this.offsetTop = y;
	
	var oPopWin = window.open(this.url, this.name, 'width=' + this.width + ',height=' + this.height + ',left=' + this.offsetLeft + ',top=' + this.offsetTop + ',menubar=' + this.menubar + ',location=' + this.location + ',resizable=' + this.resizable + ',scrollbars=' + this.scrollbars  + ',status=' + this.status);
	
	if (typeof oPopWin != 'undefined') {
		this.anchor.data('popWinObject', oPopWin);
		return true;
	}
	return false;
};



P.voorbeeldreizen = {
	
	enhanceOverview: function(){
		
		var $list = $('#voorbeeldreizen'),
			$listItems = $list.children(),
			$rows1 = $('#voorbeeldreizen > li > div.wrap > div.row1'),
			$rows2 = $('#voorbeeldreizen > li > div.wrap > div.row2'),
			$classElms = $list.find('dd.klasse'),
			$openRow = {}, $closingRow = {},
			hoverTimeout = null,
			
			toggleRow2 = function($row1){
				var $row2 = $row1.next(),
					$menu = $row2.find('ul.menu'),
					$listItem = $row1.parents('li:first'),
					rowHeight
				;
				
				// close the open row
				if ($openRow.length) {
					$openRow.find('ul.menu').fadeTo(200, 0);
					$closingRow = $openRow;
					$openRow.slideUp(function(){
						//console.log($closingRow.parents('li:first'))
						$closingRow.prev().data('expanded', false);	
						$closingRow.parents('li:first').removeClass('active').prev().removeClass('prevActive');
					});
				};
				
				if ($row1.data('expanded') == true) {
					$row1.data('expanded', false);	
					$openRow = {};
					return;
				}
				
				$listItem.addClass('active').prev().addClass('prevActive');
				
				rowHeight = $row2.height();
				
				$menu.fadeTo(0, 0);
						
				if (rowHeight < 102) {
					$row2.height(0).show().animate({height: 102}, function(){
						$openRow = $row2;
						$row1.data('expanded', true);				
						$row1.removeClass('waitASec');
						$menu.fadeTo(200, 1);
					})
				}
				else {
					$row2.slideDown(function(){
						$openRow = $row2;
						$row1.data('expanded', true);				
						$row1.removeClass('waitASec');
						$menu.fadeTo(200, 1);
					});
				}
				
				
			}
		;
		
		$rows2.hide();
		$rows2.hide().css('minHeight', 0);
				
		$rows1.click(function(e){
			toggleRow2($(this));
			e.preventDefault();
		});
		
		// $rows1.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');
		// });
		
		// Klasse stars IR
		$classElms.wrapInner('<span class="ir IR-hover"></span>');
	 
		//- tooltips
	 	$list.find('dd.klasse').Tooltip();
		
		// hover
		$listItems.hover(function(){
				$(this).addClass('hover');
				$(this).prev().addClass('prevHover');
				$(this).find('dd.klasse .IR-hover').addClass('IR-hovered');
			},
			function () {
				$(this).removeClass('hover');
				$(this).prev().removeClass('prevHover');
				$(this).find('dd.klasse .IR-hover').removeClass('IR-hovered');
			}
		);
		
		$list.addClass('enh'); 
		
		// open first item
		$(window).load(function(){
			$rows1.first().click();
		});
		
		// $rows1.hoverIntent({
			// over: function() {
				// var $this = $(this);
				// if ($this.data('expanded') == true) { return; }
				// $this.addClass('waitASec');
				// hoverTimeout = window.setTimeout(function(){
					// toggleRow2($this);
				// }, 250);				
			// },
			// out: function(){ window.clearTimeout(hoverTimeout); },
			// timeout: 0
		// });
		

	}
};


// share
P.share = {
	init: function() {
		var popWin = null;
		$('.share .facebook a, .share .twitter a, .share .hyves a').click(function(e) {
			var $this = $(this);
			
			popWin = new PopupWindow();
			popWin.anchor = $this;
			popWin.width = 550;
			popWin.height = 436;
			if (popWin.spawn()) {
				e.preventDefault();
			}			
		
		});
	}
};


/* -- 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();
	 
	 //- content menu
	 $('ul#contentMenu li.active').prev().addClass('activePrev');
	 $('ul#contentMenu li.active').next().addClass('activeNext');
 	 
 	 
 	 $('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');
	 
	
	
	$('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's, route and hotels
	$('#subMenu #hotel-term, #subMenu #hotel-def, #subMenu #fotos-term, #subMenu #fotos-def, #delen-term, #subMenu #delen-def, #subMenu #kaart-term, #subMenu #kaart-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;
	});
	
});



