var showButtonTitle = function(title){
	if(title !== false){
		$("#buttonTitle").html(title).show();
	}
}

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

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).css('height',h).css('margin-top',(h / 2) * -1); 
}

$(document).ready(function(){

	//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');

	//kill title from submenu
	$(".submenu_module").parents('.article').find('.title:eq(0)').remove();

	//kill empty titles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text())){
			$(this).remove();
		}
	});
	
	//hide blank br
	$(".article .content .module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').css('display','none');
		}
	});

	//change menus
	$("#menu > ul > li:first > a:first,#topmenu > ul > li:first > a:first").addClass('first');
	$("#menu ul > li > ul").not('#menu ul > li > ul >li > ul').each(function(){
		$(this).children("li:last").children('a:first').addClass('last');
	});
	var last_menu_zIndex = 10;
	var menuTitles = new Array();
	$("#header").prepend('<div id="buttonTitle"></div>');
	$("#menu ul li").hover(
		function(){
			$(this).addClass('hover');
			if($(this).children('ul:eq(0)').is(':hidden') && $(this).parents('ul').length < 2){
				last_menu_zIndex++;
				$(this).children('ul:eq(0)').css('z-index',last_menu_zIndex).show();

				buttonID = $(this).attr('class').match(/buttonID_([0-9]+)/)[1];
				if(!menuTitles[buttonID] && menuTitles[buttonID] !== false){
					$.getJSON("/domains/heliomareonderwijs.nl/js/json_agent.php",{action:'button',id:buttonID},function(data){
						menuTitles[buttonID] = data['title'] ? data['title'] : false;
						showButtonTitle(menuTitles[buttonID]);
					});
				}else{
					showButtonTitle(menuTitles[buttonID]);
				}
			}
		},
		function(){
			if(!$(this).hasClass('current_buttonID')){
				$(this).removeClass('hover');
			}
			if($(this).children('ul:eq(0)').is(':visible')){
				$("#buttonTitle").hide();
				$(this).children('ul:eq(0)').hide();
			}
		}
	);
	$("#menu > ul > li > a").wrapInner('<span class="left"><span class="right"><span class="middle"></span></span></span>');
	//highlight current
	$("#menu ul li.current_buttonID").parents('li').andSelf().addClass('hover').addClass('current_buttonID');
	
	//change left stuff
	$("div.leftmenu").addClass('page_texts_submenu');
	$("#left .article.boxed div.title,.page_texts_submenu > ul:first > li").prepend('<span class="left_icon"><img src="/domains/heliomareonderwijs.nl/images/left_icon.png" class="left_icon" border="0" /></span>');

	//change articles
	$("#content > #left > div.article:last,#content > #middle > div.article:last").addClass('last');
	
	//change all submit buttons
	$("input.submit").each(function(){
		$(this).before('<span class="left replaced_submit"><span class="right"><span class="middle">' + $(this).val() + '</span></span></span>');
		$(this).hide();
	});
	$("span.replaced_submit").click(function(){
		$(this).parents('form').trigger('submit');
	});
	
	//add text size menu
	$("#topmenu .sizemenu span").html('A');
	$("#topmenu .sizemenu span").hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover');}
	).click(function(){
		o = $("#topmenu .sizemenu").attr('class').split(' ')[1];

		n = $(this).attr('class').split(' ')[0];
		$(this).parents('.sizemenu').removeClass(o).addClass(n);
		$("#content").removeClass(o).addClass(n);
		
		$.get('/index.php',{current_text_size:n,ajax:true});
	});

	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		i = 0;

		//change all onclick popups
		if($(this).attr('onclick')){
			i = $(this).attr('onclick').toString().indexOf("dow.open(this.href,");

			if(i){
				w = 640;
				h = 480;
				
				u = $(this).attr('href');
				e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
				
				if(e == 'png' || e == 'gif' || e == 'jpg'){
					var id = 'popwinlink_' + uid();
					$(this).attr('id',id);

					img[id] = new Image();
					img[id].onload = function(){
						img[id].onload = null;
						w = img[id].width;
						h = img[id].height;
	
						$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
					}
					img[id].src = u;
				}else{
					c = $(this).attr('onclick');
	
					h = c.substring(c.indexOf('height=') + 7);
					h = h.substring(0,h.indexOf(','));
	
					w = c.substring(c.indexOf('width=') + 6);
					w = w.substring(0,w.indexOf(','));
	
					$(this).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");				
				}
			}

		//change all movie files
		}else if($(this).attr('href') && $(this).attr('href').match(/.(flv|FLV)$/)){
			//get filename...
			var u = $(this).attr('href').match(/(\/db\/.*.(flv|FLV)$)/)[1];
			var o = $(this);
			$.getJSON('/domains/heliomareonderwijs.nl/video/get_flv_info_agent.php',{file:u,json:true},function(data){
				o.addClass('popWindowLink').attr('href',"javascript:popWindow('/domains/heliomareonderwijs.nl/video/index.php?file=" + u + "',"+ data['video']['resolution_x'] +","+ data['video']['resolution_y'] +");");		
			});
		}
	});
	
/*
	//make the left menu stick...
	var original_menu_position = $('.page_texts_submenu').offset().top;
	$(window).scroll(function(){
		sp = $(document).scrollTop() + 20;
		mp = $('.page_texts_submenu').offset().top;
		if(sp > original_menu_position + 20){
			$('.page_texts_submenu').animate({marginTop:sp - original_menu_position},{queue:false,duration:100,easing:'linear'});
		}else if(original_menu_position != mp){
			$('.page_texts_submenu').animate({marginTop:0},{queue:false,duration:100,easing:'linear'});
		}
	});
*/	
});
