// JavaScript Document
var testform_x = 800;
var testform_y = 600;
var menu_delay = 20000;
var move_offset = 200;
var move_offset_small = 50;
var speed = 800;

/* photo gallery */
var photo_gallery_width = 0;
var photo_gallery_window = 0;
var x = 0;
var x_old = 0;
var max_x = 0;
var fading = 0.5;

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

$(document).ready(function() {
	var noSections = $("#bar_left > ul > li > ul").size();
	// alert(noSections);
	
	/* Languages buttons */
	$("#bar_left ul.languages img").fadeTo("fast", 0.50);
	
	$(".prev").fadeTo( speed, 0 );
	$(".next").fadeTo( speed, 0 );
	
	// change link from language menu to current page
	var parts = location.pathname.split('/');
	// alert(parts[1]);
	if ( (typeof parts[2] != 'undefined') && (parts[1] != 'private') ) {
		var url_en = "/english/" + parts[2] + location.search;
		var url_es = "/spanish/" + parts[2] + location.search;
		
		$("#select_language_en").attr({ href: url_en });
		$("#select_language_es").attr({ href: url_es });
	}
	
	// check if menu with subs is selected
	
	// check if sub-menu is selected
	var esel = $("li.selected").parent().parent().attr("id");
	if ($("li.selected li").size() > 0) esel = $("li.selected").attr("id");
	
	if (esel != "bar_left") {
		// sub-menu selected
		$("#bar_left #" + esel + " > ul").show();
		$("#bar_left #" + esel + " > a").css('backgroundImage','url(/images/arrow_down.gif)'); 
	}
	
	// prevent click on selected item
	$("#bar_left li.selected").click(function(event) {
		event.preventDefault();
	});
	
	for (teller = 1; teller <= noSections; teller++) {
		addHoverHide("header_sub", "section" + teller);
	}
	
	// functions
	
	$('#bar_left ul.languages a').hover(function() {
		$('#bar_left ul.languages a img').fadeTo("fast", 1);
	}, function() {
		$('#bar_left ul.languages a img').fadeTo("fast", 0.50);
	});
	
	$('#bar_right .news_item p').click(function() {
		var hoogte = $(this).height();
		if (hoogte == "58") {
			// $(this).css({height: "auto"});
			$(this).animate({height: "100%"});
			$(this).parent().find('.read_more').hide();
		} else {
			$(this).animate({height: "58px"});
			$(this).parent().find('.read_more').show();
		}
	});
	
	try {
		var pageTracker = _gat._getTracker("UA-6033135-1");
		pageTracker._trackPageview();
	} catch(err) {}
});

/* after all images have loaded */
$(window).load(function() {
	
	$("#photo_bar li").each(function(){ 
		photo_gallery_width += $(this).width() + 2;
	});
	
	photo_gallery_window = $("#photo_bar .slides").width() + 2;
	move_offset = Math.round(photo_gallery_window / 2);
	move_offset_small = Math.round(photo_gallery_window / 8);
	max_x = photo_gallery_width - photo_gallery_window;
	
	// functions
	$(".prev").click(function() {
		if (x > 0) {
			x -= move_offset;
		} else {
			x -= move_offset_small;
			$("#photo_bar ul").animate({
				left: -x + "px"
			}, "fast", "swing" );
			x_old = x;
		}
		animate_photo_gallery();
	});
	
	$(".prev").hover(function() {
		$(this).fadeTo( "normal", 1 );
	}, function() {
		$(this).fadeTo( "normal", fading );
	});
	
	$(".next").click(function() {
		if (x < max_x) {
			x += move_offset;
		} else {
			x +=  move_offset_small;
			$("#photo_bar ul").animate({
				left: -x + "px"
			}, "fast", "swing" );
			x_old = x;
		}
		animate_photo_gallery();
	});
	
	$(".next").hover(function() {
		$(this).fadeTo( "normal", 1 );
	}, function() {
		$(this).fadeTo( "normal", fading );
	});
	
	$(".prev").fadeTo( speed, fading );
	$(".next").fadeTo( speed, fading );
	
	// dragable
	$('#photo_bar ul').draggable({
		axis: 'x',
		start: function(event, ui) {
			$('#photo_bar li a').unbind();
		},
		stop: function(event, ui) {
			x = -(ui.position.left);
			x_old = x;
			animate_photo_gallery();
			setTimeout ( 'activate_slimbox()', 100 );
		}
	});
	
	$(window).resize(function() {
		// photo gallery
		photo_gallery_window = $("#photo_bar .slides").width() + 2;
		move_offset = Math.round(photo_gallery_window / 2);
		move_offset_small = Math.round(photo_gallery_window / 8);
		max_x = photo_gallery_width - photo_gallery_window;
		animate_photo_gallery();
	});
});

function addHoverHide(linkClass, sectionId) {
	if ( ($("#" + sectionId).is(".selected")) || ($("#" + sectionId).find("ul > li").is(".selected")) ) {
		// do not attach
	} else {
		var t;
		$("#" + sectionId + " a." + linkClass).parent().hover(function() {
			clearTimeout(t);
			$("#" + sectionId + " ul").slideDown("normal").parent().find("a.header_sub").css('backgroundImage','url(/images/arrow_down.gif)');
		}, function() {
			t = setTimeout(function() {$("#" + sectionId + " ul").slideUp("normal", function() { $("#" + sectionId + " a.header_sub").css('backgroundImage','url(/images/arrow_right.gif)') })}, menu_delay);
		});
	}
}

function TestForm(formulier)
{
	var ImageWindow = null;
	var xPositie = 0;
	var yPositie = 0;
	var tempBreedte = testform_x;
	var tempHoogte = testform_y;
	var Verschuiven = "yes";
	var naam = '/testforms/testform_' + formulier + '.html';

	if (tempBreedte > screen.availWidth) {
		tempBreedte = screen.availWidth - 10;
		tempHoogte = Math.abs(tempHoogte) + 16;
	}
	if (tempHoogte > screen.availHeight) {
		tempHoogte = screen.availHeight - 40;
		tempBreedte = Math.abs(tempBreedte) + 16;
	}
	
	xPositie = (screen.availWidth - tempBreedte) / 2;
	yPositie = (screen.availHeight - tempHoogte) / 2;
	
	ImageWindow = window.open(naam,"","height="+tempHoogte+",width="+tempBreedte+",screenX="+xPositie+",screenY="+yPositie+",scrollbars="+Verschuiven+",resizable=yes,noresize=no,toolbar=0,location=0,directories=0,status=0,menubar=0")
}

function animate_photo_gallery() {
	if (x < 0) {
		x = 0;
	} else if (x > (photo_gallery_width - photo_gallery_window)) {
		x = photo_gallery_width - photo_gallery_window;
	}
	
	if (x_old != x) {
		$("#photo_bar ul").animate({
			left: -x + "px"
		}, speed, "swing" );
		x_old = x;
	}
}

function activate_slimbox() {
	$("a[rel^='lightbox']").slimbox({/* Put custom options here */}, null, function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
}

function blockError()
{
	return true;
}

// window.onerror = blockError;
