
//emulate popup function...
var popWindow = function(u,w,h){
	e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
	if(e == 'png' || e == 'gif' || e == 'jpg')
		popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><img src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" /></div>",{overlay:20});
	else
		popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\"  width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>",{overlay:20});
	popEl.modal({
			onOpen: function (dialog) {
						dialog.data.addClass('alert');
						dialog.container.css('height','auto');
						dialog.overlay.fadeIn('normal', function () {
							dialog.container.show('normal', function () {
								dialog.data.slideDown('normal'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('normal', function () {
						  dialog.container.hide('normal', function () {
							dialog.overlay.fadeOut('normal', function () {
							  $.modal.close(); // must call this to have SimpleModal
											   // re-insert the data correctly and
											   // clean up the dialog elements
							});
						  });
						});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1); 
}


$(document).ready(function(){
	
	//vertical center whole layout
	h = ($(window).height() - $('#container').height()) / 2;
	if(h > 0){
		w = ($(window).width() - $('#container').width()) / 2;
		$('#container').css('position','absolute').css('top',h).css('left',w);
	}
	
	//change googlemaps tot custom popup...
	if($("div.googlemaps_module").length){
		$("div.googlemaps_module").html('<a href="javascript:popWindow(\'/domains/greetverkleedt.nl/modules/route/popup.php\',385,295);">Klik hier voor een route beschrijving met googlemaps</a>');
		//$("div.googlemaps_module").appendTo($("#overlay"));
	}
	
    //change photoalbum gui
	if($("div.photoalbum_module").length){

		$("div.photoalbum_thumb").attr("style","");
		//kill thumbnail table....
		$("table.photoalbum_thumbs").before("<div class=\"photoalbum_thumbs\">" + $("table.photoalbum_thumbs td:first").html() + "</div>").empty();
		//create thumb pages
		amount = 20;
		t = $(".photoalbum_thumb").length;
		c = Math.ceil(t / amount);
		t = amount * c;
		x = 0;
		for(i=0;i<t;i=i+amount){
			j = i + amount;
			$(".photoalbum_thumbs").append("<div class=\"page\" id=\"page_"+ x + "\"></div>");
			$(".photoalbum_thumbs > .photoalbum_thumb:lt("+amount+")").appendTo("#page_" + x);
			x++;
		}
		//add pager
		$("div.photoalbum_thumbs").pager("div.page");
	
		//get right thumb page...
		p = $(document).getUrlParam("photoID");
		if(p){
			//show right thumb page,,,
			$(".page").not($("#photoalbum_thumb_" + p).parent()).hide();
			$("#photoalbum_thumb_" + p).parent().show();
	
			//highlight Thumb
			$("#photoalbum_thumb_" + p + " a").addClass("photoalbum_thumb_highlight");
	
			//set page highlight
			a = $("#photoalbum_thumb_" + p).parent().attr("id").split('_');
			b = parseInt(a[1]) + 1;
			$(".photoalbum_thumbs .nav a").not($(".photoalbum_thumbs .nav a:eq("+b+")")).removeClass("highlight");
			$(".photoalbum_thumbs .nav a:eq("+b+")").addClass("highlight");
		}
		
		$(".photoalbum_thumbs span.photoalbum_thumb a").click(function(e){
			e.preventDefault;
			
			u = $(this).attr('href').replace('index.php?','modules/photoalbums/index.php?json_photo_data=true&');
			$.getJSON(u,function(data){
				$('#container').css('background-image','url(/domains/greetverkleedt.nl/images/fotoboek_back.jpg)');
				$('.album_preview_container').fadeOut('slow');
				$('<div class="album_preview_container"></div>').css('background-image','url(' + data['photo']['path'] + ')').appendTo($("#overlay")).fadeIn('normal');
			});
			
			return false;
		});
	}
});
