
//make uniqID
var uid = (
	function(){
		var id=0;
		return function(){
			return id++ ;
		};
	}
)();

//emulate popup function...
var popWindow = function(u,w,h,popEl){
	if(!popEl){
		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('fast', function () {
							dialog.container.show('fast', function () {
								dialog.data.slideDown('fast'); // See Other Notes below regarding
														   // data display property and
														   // iframe details
							});
						});
					},
			onClose: function (dialog) {
						dialog.data.slideUp('fast', function () {
						  dialog.container.hide('fast', function () {
							dialog.overlay.fadeOut('fast', 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).css('height',h).css('margin-top',(h / 2) * -1); 
}



//catch all alerts
var foundOnloadAlerts = new Array();
var alert = function(str){
	foundOnloadAlerts[foundOnloadAlerts.length] = str;
}


//load a photo
var index = 0;
var img = new Array();


$(document).ready(function(){

	//my own alert...
	alert = function(str){
		popWindow('',300,100,$('<div id="alert_popup" class="clear alert"><span class="string">' + str + '</span></div>'));
	}
	
	
	//change some layout
	c = $("#content").height();
	$("#left").height(c < 500 ? 500 : c);
	$("#menu ul li:first,#topmenu ul li:first").addClass('first');
	

	//change title
	var t = $("#left .site_title").text().split(' ');
	var nt = '';
	if(t.length > 1){
		for(i in t){
			if(i == Math.round(t.length / 2)){
				nt += '<span class="blue">';
			}
			nt += t[i] + ' ';
		}
		nt += '</span>';
	}else{
		nt = t.join(' ').replace('techniek','<span class="blue">techniek</span>');
	}
	$("#left .site_title").html(nt);
	
	//kill dubble title
	if($('.site_title').text() == $("#left div.box_top:first div.title").text()){
		$("#left .article:first div.title").remove();
	}

	//kill green box when empty
	if(!$.trim($("#left div.box_top:first").text())){
		$("#left div.box_top:first").remove();
	}

	/* ENABLE SUBMENU */
    $("#menu ul li").hover(
		function(){
			if($(this).children('ul:eq(0)').is(':hidden'))
				$(this).children('ul:eq(0)').css('display','block');
		},
		function(){
			if($(this).children('ul:eq(0)').is(':visible'))
				$(this).children('ul:eq(0)').css('display','none');
		}
    );
	
	//change program buttons
	/*
	$("#menu > ul > li > a").each(function(){
		t = $(this).text();
		$(this).html('<img src="/domains/frostklimaattechniek.nl/generate/button.php?text=' + escape(t) + '" alt="' + t + '" border="0" />');
	}).hover(
		function(){
			if(!$(this).parent().hasClass('current_buttonID')){
				s = $(this).children('img').attr('src');
				$(this).children('img').attr('src',s + '&over=1');
			}
		},
		function(){
			if(!$(this).parent().hasClass('current_buttonID')){
				s = $(this).children('img').attr('src');
				$(this).children('img').attr('src',s.replace('&over=1','') + '&over=0');
			}
		}
	);
	*/
	//change home
	//if($("#container.home").length){
	//	$("#middle .article:eq(0)").addClass('first');
	//	$("#middle .article:eq(1)").addClass('last');
	//	$("#middle .article:gt(1)").remove();
	//	
	//	var anim = function(){
	//		$("#middle .article.first").animate({marginLeft:0},2000);
	//		$("#middle .article.last").animate({marginLeft:928 - 349},2000);
	//	}
	//	setTimeout(anim,1000);
	//}
	
	//photoalbums
	if($(".photoalbum_module").length){

/*
		$('.photoalbum_module .previewClick').click(function(){return false;});

		//addpopup container
		if(!$('#projectPhotoAlbumPopup').length){
			$('body').append("<div id=\"projectPhotoAlbumPopup\"><div class=\"photo\"><div class=\"left nav\"></div><div class=\"right nav\"></div></div><div class=\"description\"></div></div>");
		}

		//thumb mouseover and click
		$(".photoalbum_thumbs a").click(function(e){
			h = $(this).attr('href');
			photoID = h.match(/photoID=([0-9]+)/);
			albumID = h.match(/albumID=([0-9]+)/);
			setID = h.match(/setID=([0-9]+)/);

			var obj = $(this);
			$.getJSON('/modules/photoalbums/index.php',{photoID:photoID,albumID:albumID,setID:setID,json_photo_data:1},function(data){
		
				var id = 'popwinlink_' + uid();
				var u = '/' + data['photo']['path'];
				img[id] = new Image();
				img[id].onload = function(){
					img[id].onload = null;
					w = img[id].width;
					h = img[id].height;
		
					index = $(".photoalbum_thumbs a").index(obj);
		
					$('#projectPhotoAlbumPopup div.photo').css('background-image','url(' + u + ')');
					t = $(".photoalbum_thumbs a:eq("+ index +") img").attr('alt');
					if(t){
						$('#projectPhotoAlbumPopup div.description').text(t).show();
					}else{
						$('#projectPhotoAlbumPopup div.description').hide();
					}
					
		
					if(!$('#projectPhotoAlbumPopup').is(':visible')){
		
						$('#projectPhotoAlbumPopup div.nav,#projectPhotoAlbumPopup div.photo').height(h);
						$('#projectPhotoAlbumPopup div.left').click(function(){
							index--;
							if(index < 0)
								index = $(".photoalbum_thumbs a").length - 1;
							$(".photoalbum_thumbs a:eq("+ index +")").trigger('click');
						});
						
						$('#projectPhotoAlbumPopup div.right').click(function(){
							index++;
							if(index >= $(".photoalbum_thumbs a").length)
								index = 0;
							$(".photoalbum_thumbs a:eq("+ index +")").trigger('click');
						});
						
						$('#projectPhotoAlbumPopup div.photo').hover(
							function(){$('#projectPhotoAlbumPopup div.nav').show();},
							function(){$('#projectPhotoAlbumPopup div.nav').hide();}
						);
						
						popWindow('',w,h,$('#projectPhotoAlbumPopup'));
					}else{
						$('#projectPhotoAlbumPopup,#projectPhotoAlbumPopup div.photo').find('div.nav').andSelf().height(h);
						$('#projectPhotoAlbumPopup').width(w);
						$('#modalContainer').animate({height:h,width:w,marginLeft:((w / 2) * -1),marginTop:((h / 2) * -1)},'fast');
					}
				}
				img[id].src = u;
				
				
		
				//load reaction
				var photoID = data['ID'];
				$(".photoalbum_module .reactions").load('/modules/photoalbums/reactions.php?standalone=1&photoID=' + photoID,function(){
					a = self.location.href;
					a = a.indexOf('photoID') > -1 ? a.replace(/photoID=([0-9]+)/[1],photoID) : a + '&photoID=' + photoID;
					$(".photoalbum_module .reactions .loginform form").attr('action',a);
	
					$("input[value=registreer]").attr('onclick','').click(function(){
						self.location.href = '/index.php?module=register';
					});
					
					$(".photoalbum_module .reactions a").each(function(){
						$(this).attr('href',$(this).attr('href').replace(/modules\/photoalbums\/reactions\.php/,'index.php'));
					})
				});
			});
			return false;
		}).hover(
			function(){
				$(this).addClass('thumbHover').fadeTo('slow',0.5);
			},
			function(){
				$(this).removeClass('thumbHover').fadeTo('slow',1.0);
			}
		);
	*/
	}

	//header
	randomHeader = function(i,s){
		//var i = 0;
		//while(i == n){
		//	i = Math.floor(Math.random()*6);
		//}
		if(s == 'B'){
			$('#header .background_B').css('background-image','url(/domains/frostklimaattechniek.nl/images/backgrounds/' + background_header_image[i] + ')').fadeIn('normal',function(){
				headerTmr(i,'A');
			});
		}else{
			$('#header .background_A').css('background-image','url(/domains/frostklimaattechniek.nl/images/backgrounds/' + background_header_image[i] + ')');
			$('#header .background_B').fadeOut('normal',function(){
				headerTmr(i,'B');
			});
		}
	}
	headerTmr = function(i,s){
		if(htmr)
			clearTimeout(htmr);
		i++;
		if(i == background_header_image.length)
			i = 0;
		htmr = setTimeout("randomHeader("+i+",'"+s+"');",6000);
	}
	var htmr;
	var i = 0;//Math.floor(Math.random()*6);
	$('#header .background_A').css('background-image','url(/domains/frostklimaattechniek.nl/images/backgrounds/' + background_header_image[i] + ')');
	headerTmr(i,'B');



	//change the stupid HR behavour of IE...
	if($.browser.msie){
		$("#content hr").wrap("<div class=\"hr\"></div>");
		$("#content hr").css('display','none');
	}

	//kill MS word stuff!
	$(".MsoNormal,.MsoNormal *").removeAttr('style');

	//show all found alerts
	if(foundOnloadAlerts.length > 0){
		$.each(foundOnloadAlerts,function(){
			alert(this);
		});
	}	
});
