var carr_options = {  
	visible: '100%',  
	auto: false,
	circular: false,
	mouseWheel: false,
	speed: 4000,
	eltByElt: true,
	start: 0
};
var content_cache = {
	cache: new Array(),
	has: function(section, id, sort) {
			for(var i = 0; i < this.cache.length; i++) {
				if((this.cache[i].section == section) &&
				   (this.cache[i].id == id) &&
				   (this.cache[i].sort == sort)) {
					return true;	
				}
			}
			return false;
		},
	get: function(section, id, sort) {
			if(this.has(section, id, sort)) {
				var index = this.getIndex(section, id, sort);
				return this.cache[index].data;
			}
			return null;
		},
	getIndex: function(section, id, sort) {
			for(var i = 0; i < this.cache.length; i++) {
				if((this.cache[i].section == section) &&
				   (this.cache[i].id == id) &&
				   (this.cache[i].sort == sort)) {
					return i;	
				}
			}
			return -1;
		},
	set: function(section, id, sort, data) {
		this.cache.push({"section":section.toString(), "id":id, "data":data.toString(), "sort":sort.toString()});
	}
};
$(function() {
	$.preload(['arrow-white-down','arrow-white-left'], { ext: '.png', base: 'scripts/' });
	$.preload(['1x1_80w'], { ext: '.png', base: 'styles/' });
	
	var getcontent = function(handler, section, id, sort, force_no_cache) {
		var json = null;
		var no_cache = (force_no_cache==true)?true:false;
		
		var id_arr = (id.constructor.toString().indexOf("Array")>-1);
		var id_str = "";
		if(id_arr) {
			for(var idi = 0; idi < id.length; idi++) {
				id_str += "&id[]=" + id[idi];
			}
		} else {
			id_str += "&id=" + id;
		}
		
		if(window.content_cache && !no_cache) {
			if(window.content_cache.has(section, id_str, sort)) {
				json = window.content_cache.get(section, id_str, sort);
			} else {
				var uri = 'json.php?s=' + section + id_str + '&sort=' + sort;
				//alert('URI : ' + uri + ' Type : ' + typeof(uri));
				
				json = $.ajax({
					url: uri,
					async: false
					}).responseText;
				window.content_cache.set(section, id_str, sort, json);
			}
		} else {
			json = $.ajax({
				url:'json.php?s=' + section + id_str + '&sort=' + sort,
				async: false
				}).responseText;
		}
		eval(handler + '(' + json + ');');
	}
	window.getcontent = getcontent;
	var checkdata = function(data) {
		if(data.error) {
			alert("Error: " + data.error + "\nQuery: " + data.query);
			return false;
		} else {
			return true;
		}
	};
	window.checkdata = checkdata;
	
	var closeBox = function() {
		$('#boxframe').hide("scale", { direction: "both", percent: 0 }, 1500);
		$('#sidebar').hide("drop", { direction: "left" }, 1000);
	};
	var handleClick = function() {
		if($('#boxframe').is(':hidden')) {
			$('#boxframe').show("scale", { direction: "both", percent: 100 }, 1500);
		}
		if($('#sidebar').is(':hidden')) {
			$('#sidebar').show("drop", { direction: "left" }, 1000);
		}
		
		if($(this).attr('href').replace(__loc(),'')=='#schotland') {
			var newWin = window.open('http://schotland.motoerit.nl/','newWin','left=100,top=50,width=850,height=600,toolbar=no,menubar=no,status=no,location=no');
			if(newWin.focus) {
				newWin.focus();
			}
			return false;
		}
		
		if($(this).attr('href').replace(__loc(),'').indexOf('#')==0) {
			return false;
		} else if($(this).attr('href').replace(__loc(),'').indexOf('http://')>-1) {
			var newWin = window.open($(this).attr('href'),'newWin');
			if(newWin.focus) {
				newWin.focus();
			}
			return false;
		}
		
		return ($(this).attr('href').indexOf('javascript:')>-1 || $(this).attr('href').indexOf('mailto:')>-1  || $(this).attr('href').indexOf('uploads')>-1);
	};
	window.handleClick = handleClick;
	
	$('a[href]:not(.submenuheader)').bind('click', handleClick);
	
	$('#content ul li a[href]').unbind('click').click(function() {
		$('#content .content-tab').hide();
		$($(this).attr('href')).show();
		$('#content #imagetab').remove();
		$('#mapli').hide();$('#gps_icon').empty();
		return false;
	});
	
	$('#closeicon').bind('click', closeBox);
	
	$(document).ready(function() {
		window.getcontent('handleCat','categorie',-1,'volgorde,naam');
		window.getcontent('handleContent','content',1,'id');
		window.getcontent('handleTopMenu','content',-1,'menutitel');
		
		if($('#fotoslider ul li').length > 0) {
			if(!ie_running) { $("#fotoslider").jMyCarousel(window.carr_options); }
		}
		
		var search = window.location.search.substr(1);
		if(search.indexOf('reis')>-1) {
			getcontent('handleReizen','reizen', search.substr(search.indexOf('=',search.indexOf('reis'))+1),'id');
		}
	});
});
function handleCat(data) {
	if(checkdata(data)) {
		$('#sidebar .glossymenu').empty();
		for(var i = 0; i < data.result.length; i++) {
			var str = '';
			if(data.result[i].id == '2') {
				str = '<a href="#schotland" class="menuitem' + ((data.result[i].sub)?' submenuheader':'') + '">' + data.result[i].naam + '</a>';
			} else {
				str = '<a href="#sub' + data.result[i].id + '" class="menuitem' + ((data.result[i].sub)?' submenuheader':'') + '">' + data.result[i].naam + '</a>';
			}
			if(data.result[i].sub) {
				var d = '<div id="sub' + data.result[i].id + '" class="submenu" style="display:none;"><ul>';
				for(var j = 0; j < data.result[i].sub.length; j++) {
					d += '<li><a href="javascript:getcontent(\'handleReizen\',\'reizen\',' + data.result[i].sub[j].id + ',\'id\')">' + data.result[i].sub[j].menutitel + '</a></li>';
				}
				d += '</ul></div>';
				str += d;
			}
			$('#sidebar .glossymenu').append(str);
		}
		acc_refresh();
	}
}
function handleContent(data) {
	if(checkdata(data)) {
		$('#boxframe h2:first').text('Motoerit - ' + data.result[0].menutitel);
		$('#content-sidebar').hide();
		$('#content-content').css('margin-left','10px');
		
		$('#tab1').empty().append('<h3>' + data.result[0].kop + '</h3>').append(data.result[0].content);
		$('#mapli').hide();$('#gps_icon').empty();

		$('#content .content-tab').hide();
		$('#tab1').show();
		
		$('#content-content a').bind('click', handleClick);
		
		$('#content ul li a[href]').unbind('click').click(function() {
			$('#content .content-tab').hide();
			$('#gps_icon').empty();$('#mapli').hide();
			$($(this).attr('href')).show();
			return false;
		});
		
		try {
			window.getcontent('handleFirstScene','firstscene',-1,'id');
		} catch (e) { alert(e.message.toString()); }
	}
};
function handleFirstScene(data) {
	if(checkdata(data)) {
		// Cleaning up the carousel
		for(var t = 0; t < $.timers.length; t++) {
			if($.timers[t].elem.tagName=="UL" && $.timers[t].elem.parentNode.id == "fotoslider") {
				$.timers.splice(t);
			}
		}
		if(!ie_running) { 
			$('#fotoslider ul').jMyCarousel = null;
		}
		$('#fotoslider').removeAttr('style');
		$('#fotoslider').empty();
		$('#fotoslider').append('<ul></ul>');
		
		// Adding Images
		for(var i = 0; i < data.result.length; i++) {
			if(data.result[i].foto_1!='') {
				$('#fotoslider ul').append('<li><a href="javascript:getcontent(\'handleReizen\',\'reizen\',' +
											data.result[i].id + 
											',\'id\');"><img src="/images/nthumbs/' +
											data.result[i].foto_1 + 
											'" border="0" /></a></li>');
			}
			if(data.result[i].foto_1!='') {
				$('#fotoslider ul').append('<li><a href="javascript:getcontent(\'handleReizen\',\'reizen\',' +
											data.result[i].id + 
											',\'id\');"><img src="/images/nthumbs/' +
											data.result[i].foto_2 + 
											'" border="0" /></a></li>');
			}
			if(data.result[i].foto_1!='') {
				$('#fotoslider ul').append('<li><a href="javascript:getcontent(\'handleReizen\',\'reizen\',' +
											data.result[i].id + 
											',\'id\');"><img src="/images/nthumbs/' +
											data.result[i].foto_3 + 
											'" border="0" /></a></li>');
			}
		}
		
		// Running the carousel again
		if($('#fotoslider ul li').length > 0) {
			if(!ie_running) { $("#fotoslider").jMyCarousel(window.carr_options); }
		} else {
			$('#fotoslider').css('height','75px');
		}
		if(window.updateIEFB) { window.updateIEFB(); }
		$('#fotoslider, #fotoslider ul').css('height','75px').css('width','100%');
	}
}
function handleReizen(data) {
	if(checkdata(data)) {
		$('#boxframe h2:first').text(data.result[0].menutitel);
		
		$('.content-tab').hide();
		
		$('#tab1').show().empty().append('<h3>' + data.result[0].menutitel + '</h3>');
		
		$('#tab1').append(data.result[0].omschrijving).show();
		$('#tab2').empty().append('<h3>Reisschema</h3>').append(data.result[0].inbegrepen);
		$('#tab3').empty().append('<h3>Kosten</h3>').append(data.result[0].prijs);
		//$('#tab4').empty().append('<h3>Boeken</h3><p><a href="boeken.php?reis=' + data.result[0].id + '" class="force">Klik hier om te boeken</a></p>');
		$('#tab5').empty();
		
		if(parseInt(data.result[0].positie_lat)!=0) {
			$('#tab5').html('<iframe width="580" height="380" border="0" frameborder="0" scrolling="no" src="/google.maps.php?lat=' + 
							data.result[0].positie_lat + 
							'&amp;lng=' + 
							data.result[0].positie_lon + 
							'&amp;t=' +
							escape(data.result[0].menutitel) +
							'" />');
			$('#mapli').show();
		} else {
			$('#mapli').hide();
		}
		if(data.result[0].gps!="0") {
			$('#gps_icon').empty();
			var precontent = "<p style=\"font-size: .8em;\">";
				precontent += '<img src="styles/gpsicon.png" width="16" height="16" alt="Garmin GPS Route beschikbaar" title="Garmin GPS Route beschikbaar" /> ';
			var d = data.result[0].dagen;
				d = parseInt(d,10);
				dd = "dagen";
				if(d == 1) { dd = "dag"; }
				precontent += "" + d + " " + dd + "</p>";
				$('#gps_icon').append(precontent);
		} else {
			$('#gps_icon').empty();	
		}

		$('#content-sidebar').show();
		$('#content-content').css('margin-left','110px');
		
		$('#content-content a:not(.force)').bind('click', handleClick);
		
		$('#content ul li a[href]').unbind('click').click(function() {
			$('#content .content-tab').hide();
			$('#content #imagetab').remove();
			$($(this).attr('href').replace(window.location.toString(),'')).show();
			return false;
		});
		
		if($('#boxframe').is(':hidden')) {
			$('#boxframe').show("scale", { direction: "both", percent: 100 }, 1500);
		}
		if($('#sidebar').is(':hidden')) {
			$('#sidebar').show("drop", { direction: "left" }, 1000);
		}
		
		if(data.result[0].fotoboek) {
			try {
				window.getcontent('handleFotoBoek','fotoboek', data.result[0].fotoboek ,'id');
			} catch (e) { alert(e.message.toString()); }
		} else {
			if(!ie_running) { 
				$('#fotoslider ul').jMyCarousel = null;
			}
			$('#fotoslider').removeAttr('style');
			$('#fotoslider').empty();
			$('#fotoslider').css('height','75');
		}
		
		window.curr_id = data.result[0].id;
		$('a#boeklink').attr('href','boeken.php?reis=' + window.curr_id).unbind('click');
	}
};
function handleTopMenu(data) {
	if(checkdata(data)) {
		$('#menubar ul').empty().append('<li><a href="javascript:getcontent(\'handleContent\',\'content\',1,\'id\')">Reisprogramma</a></li>');
		for(var i = 0; i < data.result.length; i++) {
			if(data.result[i].id == 1) { continue; }
			$('#menubar ul').append('<li><a href="javascript:getcontent(\'handleContent\',\'content\',' +
																		data.result[i].id +
																		',\'id\')">' + data.result[i].menutitel + '</a></li>');
		}
	}
	$('a[href]:not(.submenuheader)').bind('click', handleClick);
};
function handleFotoBoek(data) {
	if(checkdata(data)) {
		// Cleaning up the carousel
		for(var t = 0; t < $.timers.length; t++) {
			if($.timers[t].elem.tagName=="UL" && $.timers[t].elem.parentNode.id == "fotoslider") {
				$.timers.splice(t);
			}
		}
		if(!ie_running) { 
			$('#fotoslider ul').jMyCarousel = null;
		}
		$('#fotoslider').removeAttr('style');
		$('#fotoslider').empty();
		$('#fotoslider').append('<ul></ul>');
		
		// Adding Images
		for(var i = 0; i < data.result.length; i++) {
			if(data.result[i].sub) {
				for(var j = 0; j < data.result[i].sub.length; j++) {
					$('#fotoslider ul').append('<li><a class="lightbox" href="/images/nfotos/' +
												data.result[i].sub[j].filename + 
											    '"><img src="/images/nthumbs/' +
											    data.result[i].sub[j].filename + 
											    '" alt="' + 
											    data.result[i].sub[j].titel +
											    '" width="' +
											    data.result[i].sub[j].breedte + 
											    '" height="' +
											    data.result[i].sub[j].hoogte + 
											    '" border="0" /></a></li>');
				}
			}
		}
		
		// Running the carousel again
		if($('#fotoslider ul li').length > 0) {
			if(!ie_running) { $("#fotoslider").jMyCarousel(window.carr_options); }
			acc_refresh();
		} else {
			$('#fotoslider').css('height','75px');
		}
		if(window.updateIEFB) { window.updateIEFB(); }
	}
};
function acc_refresh() {
	$('#sidebar a').unbind('click');
	$('#sidebar a.submenuheader').unbind('click').click(function() {
		
		$($(this).attr('href').replace(__loc(),'')).toggle();
		
		if($($(this).attr('href').replace(__loc(),'')).is(":hidden")) {
			$(this).find('img').attr('src','scripts/arrow-white-left.gif');
		} else {
			$('div.submenu:visible:not(' + $(this).attr('href').replace(__loc(),'') + ')').hide();
			$('a.submenuheader img').attr('src','scripts/arrow-white-left.gif');
			$(this).find('img').attr('src','scripts/arrow-white-down.gif');
		}
		return false;
	}).each(function() {
		if($($(this).attr('href').replace(__loc(),'')).is(':hidden')) {
			$(this).append('<span class="statusicon"><img src="scripts/arrow-white-left.gif" border="0" alt="expand"/></span>');
		} else {
			$(this).append('<span class="statusicon"><img src="scripts/arrow-white-down.gif" border="0" alt="collapse"/></span>');
		}
	});

	$('#sidebar a:not(.submenuheader)').bind('click', handleClick);
	$('a.lightbox').click(function() {
		$('#mapli').hide();
		//$('#gps_icon').empty();
		$('#content .content-tab').hide();
		$('#content-content').append('<div class="content-tab" id="imagetab"><img src="' + $(this).attr('href') + '" /></div>');
		$('#content-sidebar').show();
		return false;
	});
};
function __loc() {
	return window.location.protocol + '//' + window.location.host + ((window.location.path)?window.location.path:'/') + ((window.location.search)?window.location.search:'');	
}
function changeBG() {
	if($('body').is('.other-bg')) {
		$('body').removeClass('other-bg');	
	} else {
		$('body').addClass('other-bg');	
		setTimeout(changeBG, 10000);
	}
}