var stageWidth = 0;
var itemArray = new Array();
var totaleBreedte = 0;
var huidige = 0;
var totaal;
var laden;
var lightbox = false;
var allesGeladen = false;

function init(loadId) {
	$(document)[0].oncontextmenu = function() {return false;}   
	
	stageWidth = $(document).width();
	stageHeight = $(document).height();
	$('#visualBalk').css({top: Math.max(250, stageHeight - (stageHeight/3) - 150) }, 300);
	$('#visualBalk').animate({"marginLeft": stageWidth/2 - totaleBreedte/2 }, { "queue": false, "duration": 800, "easing": "easeOutBack" });
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'facebook', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
		showTitle: true,
		changepicturecallback: function(){ lightbox = true; },
		callback: function(){ lightbox = false; }
	});
	
	laadCata(loadId);
	
	$("#contact").mouseenter(function(){
		openForm();
	});
	
	$("#contact").mouseleave(function(){
		$('#form').stop();
		sluitForm();
	});
}

function openForm() {
	$('#form').animate({"width": 170 }, { "queue": false, "duration": 300, "easing": "easeOutCubic" });
}

function sluitForm() {
	$('#form').animate({"width": 0 }, { "queue": false, "duration": 200, "easing": "easeOutQuad" });
}

$(window).resize(function() {
	stageWidth = $(window).width();
	stageHeight = $(window).height();
	$('#visualBalk').css({top: Math.max(250, stageHeight - (stageHeight/3) - 150) }, 300);
	$('#visualBalk').animate({"marginLeft": stageWidth/2 - totaleBreedte/2 }, { "queue": false, "duration": 800, "easing": "easeOutBack" });
});


$(document).mousemove(function(e){
	//alert(totaleBreedte + "  -  " + stageWidth);
	//var marge = Math.max(Math.round(Math.min(((stageWidth - 100) - e.pageX), 0) / (stageWidth - 100) * stageWidth - 280, 0)), -($('#aanbiedingen_reeks').height() - 280));
	if(totaleBreedte > 0 && stageWidth > 0 && lightbox == false) {
		if(totaleBreedte > stageWidth) {
			var marge = Math.floor(e.pageX / stageWidth * (totaleBreedte - stageWidth));
			$('#visualBalk').animate({"marginLeft": -marge }, { "queue": false, "duration": 800, "easing": "easeOutBack" });
		} else {
			$('#visualBalk').animate({"marginLeft": stageWidth/2 - totaleBreedte/2 }, { "queue": false, "duration": 800, "easing": "easeOutBack" });
		}
	}
});

function laadCata(id) {	
	itemArray = new Array();
	$("#visualBalk").empty();
	
	$.ajax({
		type: "POST",
		url: "mediaLaden.php",
		data: "categorie=" + id,
		success: function(items){
			if(items == "error1")
				alert("Geen categorie meegegeven!");
			else if(items == "error2")
				alert("Geen verbinding met de database!");
			else {
				itemArray = items.split("[]");
				
				var item = new Array();
				huidige = 0;
				laden = 0;
				totaal = itemArray.length;
				totaleBreedte = 0;
				allesGeladen = false;
				for(i = 0; i < totaal; i++) {
					item = itemArray[i].split("::");
					$("#visualBalk").append("<a href='#' rel='Prettyphoto'><div class='foto loading' rel='"+item[1]+"' id='foto" + i + "' style='border: none;'></div></a>");
					totaleBreedte += parseFloat(item[2]);
					//if(totaleBreedte < stageWidth) {
						laadPlaatje(huidige, item[1], item[3], item[2]);
						huidige++;
						laden++;
					//}
				}
				$('#visualBalk').css({width: Math.max(totaleBreedte, stageWidth) }, 300);
				
				$('#visualBalk').animate({"marginLeft": stageWidth/2 - totaleBreedte/2 }, { "queue": false, "duration": 800, "easing": "easeOutBack" });
			}
		}
	});
}

function openFoto(id, video) {
	if(video != "false") {
		$.prettyPhoto.open(video,'','');
	} else {
		$.prettyPhoto.open('media/gr_' + id,'','');
	}
}

function stuurMail() {
	var naam = $("#mailnaam").val();
	var email = $("#mailemail").val();
	var bericht = $("#bericht").val();
	
	if(!naam || !email || !bericht) {
		alert("Niet alle velden zijn ingevuld!");
	} else {
		$("#form").html("<img src='lader.gif' style='margin: 50px 15px 0px 0px' />");
		
		$.ajax({
			type: "POST",
			url: "stuurMail.php",
			data: "naam=" + naam + "&email=" + email + "&bericht=" + bericht,
			success: function(gelukt){
				window.location.href = "";
			}
		});
	}
}

function laadPlaatje(nr, url, video, w) {
	var img = new Image();
	var item;
 	//$('#foto' + nr).css({'width': parseInt(w) });
 	
	$(img).load(function () {
		$(this).hide();
		$('#foto' + nr).removeClass('loading').css({'background': 'url(media/' + url + ')', 'width': parseInt(w) });

		if(video.length > 5)
			$('#foto' + nr).append("<br /><br /><img src='play.png' />");
		
		$(this).css({border: "0px"}, 300);
    
		$(this).fadeIn();
		laden--;
		
		$('#foto' + nr).click(function() {
			openFoto(url, video);
		});
		
//		if(laden <= 0 && huidige < totaal && allesGeladen == false) {
//			allesGeladen = true;
//			for(i = huidige; i < totaal; i++) {
//				if(itemArray[huidige]) {
//					item = itemArray[huidige].split("::");
//					laadPlaatje(huidige, item[1], item[3]);
//					huidige++;
//					laden++;	
//				}
//			}
//		}
		
	}).error(function () {
      
	}).attr('src', 'media/' + url);
}
