$(document).ready(function() {
	/*load top menu*/
	$.getScript("js/sub_build_menu.js");

	/*load top menu*/
	$.getScript("js/basic_hovers.js");
	
	//width height
	
	if(jQuery.browser.msie){
		sWidth = document.documentElement.offsetWidth;
		sHeight = document.documentElement.offsetHeight;
		
	}
	else {
		sWidth = window.innerWidth;
		sHeight = window.innerHeight;
	}
	
	var innerHeight = document.body.scrollHeight;
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	
	/*
		Bram's horrorbox
	  Eigenbox thickbox soort van applicatie
	  
	  class="eigenbox"
	  
	  @author b.mittendorff
	  @version 1.0 23 maart 2011
	 */
	var percentageCorners = 0.8;
	var currentIndex = 0;
	var theImageId = 0;
	var theBouwId = 0;
	var $itemLength = 0;
	
	//eigenbox bouwen
	$("#content_bouw #mid a").click(function() {
		theImageId = $(this).attr("rel");
		theBouwId = $(this).attr("rev");
		
		if($(this).hasClass("eigenbox")) {
			$("body").prepend(
				'<div class="eigenbox-wrapper">'+
					'<div class="eigenbox-inner">'+
						'<div class="top">'+
							'<a href="#" class="eigenbox-sluiten">Sluiten</a>'+
						'</div>'+
						'<div class="bottom">'+
							'<div class="left">'+
								'<div class="watermerk">'+
									//'<img src="images/logo-eigenbox.png" />'+
								'</div>'+
								'<div class="image">'+
									//'<img src="empty" class="eigenbox-image" width="'+Math.round(((percentageCorners*sWidth)*0.7)-20)+'" />'+
									'<img src="empty" class="eigenbox-image" />'+
								'</div>'+
								'<div class="navigation">'+
									'<a href="#" class="left">&nbsp;</a>'+
									'<a href="#" class="right">&nbsp;</a>'+
								'</div>'+
							'</div>'+
							'<div class="right">'+
								'<div class="righttop">'+
								
								'</div>'+
								'<div class="rightbottom">'+
									'<a href="#" class="toevoegenprofiel"><img src="images/toevoegen-profiel.gif" /></a>'+
									//'<a href="print.asp?PrintId='+theImageId+'" target="_blank" class="printnu"><img src="images/lightbox-print.gif" /></a>'+
									'<a href="javascript:window.print()" class="printnu"><img src="images/lightbox-print.gif" /></a>'+
								'</div>'+
							'</div>'+
						'</div>'+
					'</div>'+
				'</div>'
			);
			
			$(".toevoegenprofiel").click(function() {
				if($("#profile_top").hasClass("youarenowloggedin")) {
					$.ajax({
						type: "GET",
						url: "_ajax_bouw_profiel.asp",
						data: "BouwId=" + $(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rev") + "&TypeId=" + $(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rel") + "&AfbeeldingId=" + $(".eigenbox-inner .righttop a.eigenboxbyinner").attr("name"),
						success: function(resultaat){
							$("#profile #text #afbeeldingen_site").html(resultaat);
							$(".eigenbox-wrapper").fadeOut("slow", function() {
								$(".eigenbox-wrapper").remove();
							});
						},
						error:function (xhr, ajaxOptions, thrownError){
							//console.log(xhr+ajaxOptions+thrownError);
						}
					});
				}
				else {
					//alert("Om afbeeldingen aan uw profiel toe te voegen dient u eerst in te loggen.");
					var c = confirm("Om afbeeldingen aan uw profiel toe te voegen dient u eerst in te loggen.\nWilt u nu inloggen?");
					if (c)
					{
						var q = window.location.toString().split("?")[1];
						window.location = "login.asp?" + q;
					}
				}
			});
			
			$(".eigenbox-image").attr("src",$(this).attr("href"));
			
			//alert($(".eigenbox-inner").height());
			$(".eigenbox-inner").css({
				//'width' : (percentageCorners*sWidth),
				//'height' : (percentageCorners*sHeight),
				//'marginTop' : (sHeight-($(".eigenbox-inner").height()))/2
				marginTop : (sHeight/2) - (530/2)
			});
			
			/*$(".eigenbox-inner .bottom .left .navigation").css({
				'width' : Math.round(((percentageCorners*sWidth)*0.7)-20),
				'height' : Math.round((percentageCorners*sHeight)-((percentageCorners*sHeight)*0.05))
			});*/
			/*
			$(".eigenbox-inner .bottom .right").css({
				'height' : Math.round((percentageCorners*sHeight)-((percentageCorners*sHeight)*0.05))
			});
			*/
			
			//$("body").css("overflow","hidden");
			
			$(".eigenbox-wrapper").css("height",yScroll+"px");
			
			$(".eigenbox-image").load(function(){
				$(".eigenbox-wrapper").fadeIn();
				
			});
			
			function left(str, n){
				if (n <= 0)
				    return "";
				else if (n > String(str).length)
				    return str;
				else
				    return String(str).substring(0,n);
			}
			
			$.ajax({
				type: "GET",
				url: "_ajax_lightbox.asp",
				data: "imageCode="+left($(".eigenbox-image").attr("src").replace("db_images/",""),6)+"&imageId="+theImageId+"&BouwId="+theBouwId,
				success: function(resultaat){
					$(".eigenbox-wrapper .right .righttop").html(resultaat);
					
					$itemLength = $(".eigenbox-inner .bottom .right .righttop a").length;
					
					$(".eigenbox-inner .righttop a.eigenboxbyinner").addClass("eigenboxbyinner");
	
					$(".eigenbox-inner .righttop a").click(function() {
						theImageId = $(this).attr("rel");
						$(".printnu").attr("href","print.asp?PrintId="+theImageId+"");
						
						$(".eigenbox-image").attr("src",$(this).attr("href"));
						currentIndex = $(".eigenbox-inner .righttop a").index($(this));
						$(".eigenbox-inner .righttop a.eigenboxbyinner").removeClass("eigenboxbyinner");
						$(this).addClass("eigenboxbyinner");
						return false;
					});
					
					
					//eerste
					if(currentIndex == 0) {
						$(".eigenbox-inner .bottom .left .navigation .left").css("display","none");
					}
					
					//laatste
					if(currentIndex == $itemLength-1) {
						$(".eigenbox-inner .bottom .left .navigation .right").css("display","none");
					}
					
					//navigation
					$(".eigenbox-inner .bottom .left .navigation .left").click(function() {
						//eerst allebij laten zien
						$(".eigenbox-inner .bottom .left .navigation .right").css("display","block");
						$(".eigenbox-inner .bottom .left .navigation .left").css("display","block");
						
						$(".eigenbox-image").attr("src",$(".eigenbox-inner .righttop a:eq("+(currentIndex-1)+")").attr("href"));
						
						if(currentIndex+1 <= 0) {
							
						}
						else {
							currentIndex--;
						}
						checkLeft(currentIndex);
						
						$(".eigenbox-inner .righttop a").removeClass("eigenboxbyinner");
						$(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").addClass("eigenboxbyinner");
						
						theImageId = $(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").attr("rel");
						$(".printnu").attr("href","print.asp?PrintId="+theImageId+"");
					});
					$(".eigenbox-inner .bottom .left .navigation .right").click(function() {
						//eerst allebij laten zien
						$(".eigenbox-inner .bottom .left .navigation .right").css("display","block");
						$(".eigenbox-inner .bottom .left .navigation .left").css("display","block");
						
						$(".eigenbox-image").attr("src",$(".eigenbox-inner .righttop a:eq("+(currentIndex+1)+")").attr("href"));
						
						if($itemLength <= currentIndex+1) {
						
						}
						else {
							currentIndex++;
						}
						checkRight(currentIndex);
						
						$(".eigenbox-inner .righttop a").removeClass("eigenboxbyinner");
						$(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").addClass("eigenboxbyinner");
						
						theImageId = $(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").attr("rel");
						$(".printnu").attr("href","print.asp?PrintId="+theImageId+"");
					});
					
			
					function checkLeft(i) {
						if(i <= 0) {
							$(".eigenbox-inner .bottom .left .navigation .left").css("display","none");
						}
						else {
							$(".eigenbox-inner .bottom .left .navigation .left").css("display","block");
						}
					}
					
					function checkRight(i) {
						//if($itemLength <= i) {
						if (i == $itemLength-1) {
							$(".eigenbox-inner .bottom .left .navigation .right").css("display","none");
						}
						else {
							$(".eigenbox-inner .bottom .left .navigation .right").css("display","block");
						}
					}
					
									
					//sluiten van eigenbox en andere controls
					if($.browser.msie) {
						$(document).keydown(function(e) {
							if(e.keyCode == 27) {
								$(".eigenbox-wrapper").fadeOut("slow", function() {
									$(location).attr("href", "bouw.asp?BouwId="+$(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rev")+"&TypeId="+$(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rel")+"&ProjectId="+parseInt($(".eigenbox-inner .righttop a.eigenboxbyinner").attr("coords"))+"");
									$(".eigenbox-wrapper").remove();
								});
							}
							
							//left
							if(e.keyCode == 37) {
								if(currentIndex+1 <= 0) {
									$(".eigenbox-inner .bottom .left .navigation .left").css("display","none");
								}
								else {
									//eerst allebij laten zien
									$(".eigenbox-inner .bottom .left .navigation .right").css("display","block");
									$(".eigenbox-inner .bottom .left .navigation .left").css("display","block");
									
									$(".eigenbox-image").attr("src",$(".eigenbox-inner .righttop a:eq("+(currentIndex-1)+")").attr("href"));
									currentIndex--;
									
										$(".eigenbox-inner .righttop a").removeClass("eigenboxbyinner");
										$(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").addClass("eigenboxbyinner");
										
										theImageId = $(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").attr("rel");
										$(".printnu").attr("href","print.asp?PrintId="+theImageId+"");
									
									checkLeft(currentIndex);
								}
							}
							//right
							if(e.keyCode == 39) {
								if($itemLength <= currentIndex+1) {
									$(".eigenbox-inner .bottom .left .navigation .right").css("display","none");
								}
								else {
									//eerst allebij laten zien
									$(".eigenbox-inner .bottom .left .navigation .right").css("display","block");
									$(".eigenbox-inner .bottom .left .navigation .left").css("display","block");
									
									$(".eigenbox-image").attr("src",$(".eigenbox-inner .righttop a:eq("+(currentIndex+1)+")").attr("href"));
									currentIndex++;
									
										$(".eigenbox-inner .righttop a").removeClass("eigenboxbyinner");
										$(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").addClass("eigenboxbyinner");
										
										theImageId = $(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").attr("rel");
										$(".printnu").attr("href","print.asp?PrintId="+theImageId+"");
									
									checkRight(currentIndex);
								}
							}
						});
					}
					else {
						$(document).keypress(function(e) {
							if(e.keyCode == 27) {
								$(".eigenbox-wrapper").fadeOut("slow", function() {
									$(location).attr("href", "bouw.asp?BouwId="+$(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rev")+"&TypeId="+$(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rel")+"&ProjectId="+parseInt($(".eigenbox-inner .righttop a.eigenboxbyinner").attr("coords"))+"");
									$(".eigenbox-wrapper").remove();
								});
							}
							
							//left
							if(e.keyCode == 37) {
								if(currentIndex+1 <= 0) {
									$(".eigenbox-inner .bottom .left .navigation .left").css("display","none");
								}
								else {
									//eerst allebij laten zien
									$(".eigenbox-inner .bottom .left .navigation .right").css("display","block");
									$(".eigenbox-inner .bottom .left .navigation .left").css("display","block");
									
									$(".eigenbox-image").attr("src",$(".eigenbox-inner .righttop a:eq("+(currentIndex-1)+")").attr("href"));
									currentIndex--;
									
										$(".eigenbox-inner .righttop a").removeClass("eigenboxbyinner");
										$(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").addClass("eigenboxbyinner");
										
										theImageId = $(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").attr("rel");
										$(".printnu").attr("href","print.asp?PrintId="+theImageId+"");
									
									checkLeft(currentIndex);
								}
							}
							//right
							if(e.keyCode == 39) {
								if($itemLength <= currentIndex+1) {
									$(".eigenbox-inner .bottom .left .navigation .right").css("display","none");
								}
								else {
									//eerst allebij laten zien
									$(".eigenbox-inner .bottom .left .navigation .right").css("display","block");
									$(".eigenbox-inner .bottom .left .navigation .left").css("display","block");
									
									$(".eigenbox-image").attr("src",$(".eigenbox-inner .righttop a:eq("+(currentIndex+1)+")").attr("href"));
									currentIndex++;
									
										$(".eigenbox-inner .righttop a").removeClass("eigenboxbyinner");
										$(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").addClass("eigenboxbyinner");
										
										theImageId = $(".eigenbox-inner .bottom .right .righttop a:eq("+(currentIndex)+")").attr("rel");
										$(".printnu").attr("href","print.asp?PrintId="+theImageId+"");
									
									checkRight(currentIndex);
								}
							}
						});
					}
					
					$(document).click(function(e) {
						//moet voor internet explorer 7
						var $target = $(e.target);
						
						if ($target.hasClass("eigenbox-wrapper") || $target.hasClass("eigenbox-sluiten")) {
							$(".eigenbox-wrapper").fadeOut("slow", function() {
								//$(location).attr("href", "bouw.asp?BouwId="+$(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rev")+"&TypeId="+$(".eigenbox-inner .righttop a.eigenboxbyinner").attr("rel")+"&ProjectId="+$(".eigenbox-inner .righttop a.eigenboxbyinner").attr("coords")+"");
								$(".eigenbox-wrapper").remove();
								$(document).unbind("click");
							});
							
						//$("body").css("overflow","auto");
							return false;
						}
					});
					
					/*
					$("#jquery-lightbox #lightbox-container-image-data-box #lightbox-print-save #ajax_content a").bind("click", function(){
						$("#lightbox-image").attr("src",$(this).attr("href"));

						setTimeout(function() {
								_resize_container_image_box(800,$("#lightbox-image").height());
						}, 100);
						
						return false;
					});
					*/
				},
				error:function (xhr, ajaxOptions, thrownError){
					
				}      
			});
			
			//geef geen return waarde voor de a href
			return false;
		}
	});
	
});
