var background_header_image = new Array();
var background_header_click = new Array();
var background_header_text = new Array();

//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.show();
						//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
							//$('#alert').remove();
							});
						  });
						//});
					}
	});
	$("#modalContainer").css('width',w).css('margin-left',(w / 2) * -1).css('height',h).css('margin-top',(h / 2) * -1); 
}


function makeFormAjax(o){
	o.find('form').submit(function(e){
		e.preventDefault();
		
		$.post($(this).attr('action'),$(this).serialize(),function(data){
			d = $(data).find('#middle');
			d.find('.module_contents > a:eq(0)').remove();
			makeFormAjax(d);
			$('#popup').html(d.html());
			_typeface_js.renderDocument();
			
			$('#middle .article:last').load(self.location.href + ' #middle .article:last > *',function(){
				_typeface_js.renderDocument();
			});
		});
		
		return false;
	});
}

$(document).ready(function(){

	$(".reaction_stats span.comments a").click(function(){
		$.post($(this).attr('href'),{ajax:true},function(data){
			d = $(data);
			d.find('.module_contents > a:eq(0)').remove();
			var o = $('<div id="popup" /></div>').html(d.html());
			o.appendTo('body');
			_typeface_js.renderDocument();
			o.css('display','none');
			
			makeFormAjax(o);
			
			popWindow('',450,o.outerHeight(),o);
		});
		return false;
	});

	//footer icons
	$('#footer > a').hover(
		function(){$(this).css('margin-top','-66px');},
		function(){$(this).css('margin-top','0');}
	);

	//hide blank br before modules
	$(".module_contents").each(function(){
		if($(this).prev('br').length == 1){
			$(this).prev('br').remove();
		}
	});
	
	//kill empty titles and articles
	$(".article,.title").each(function(){
		if(!$.trim($(this).text()) && !$(this).find('img,object,embed').length){
			$(this).remove();
		}
	});

	//change same layouts
	$('table.reaction').attr('cellpadding','0');

	//flicker popup UI
	$(".flickralbum_module ul.thumbs li a").click(function(e){
		var currImgIndx = $(this).parents('ul.thumbs').find('a').index(this);
		
		o = $('<div id="flickr_popup_ui"><div class="image"><div class="back"></div><div class="forward"></div></div></div>');
		o.find('.image').css({backgroundImage:'url(' + $(this).attr('href') + ')'}).hover(
			function(){$(this).find('div').fadeIn('normal');},
			function(){$(this).find('div').fadeOut('normal');}
		);

		o.find('.forward').click(function(){
			currImgIndx++;
			u = $(".flickralbum_module ul.thumbs li a:eq(" + currImgIndx + ")").attr('href');
			$(this).parents('.image').css({backgroundImage:'url(' + u + ')'});
		});
		o.find('.back').click(function(){
			currImgIndx--;
			u = $(".flickralbum_module ul.thumbs li a:eq(" + currImgIndx + ")").attr('href');
			$(this).parents('.image').css({backgroundImage:'url(' + u + ')'});
		});
		
		popWindow('',504,350,o);
		
		return false;
	});


	//header
	var current_head_click_key = 0;
	randomHeader = function(i,s){
		if(s == 'B'){
			$('#header .background_B span').html(background_header_text[i]);
			$('#header .background_B').css('background-image','url(' + background_header_image[i] + ')').fadeIn('normal',function(){
				current_head_click_key = i;
				headerTmr(i,'A');
			});
		}else{
			$('#header .background_A span').html(background_header_text[i]);
			$('#header .background_A').css('background-image','url(' + background_header_image[i] + ')');
			$('#header .background_B').fadeOut('normal',function(){
				current_head_click_key = i;
				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;
	$('#header .background_B').css('background-image','url(' + background_header_image[i] + ')');
	$('#header .background_B span').html(background_header_text[i]);
	headerTmr(i,'A');
	$('#header .background_B,#header .background_B').click(function(){
		if(background_header_click[current_head_click_key]){
			self.location.href = '/index.php?module=program&eventID=' + background_header_click[current_head_click_key];
		}
	});
	
	if($('.side_slider_module').length){
		var skipNextPSframe = function(){
			i = $('.side_slider_module .menu a').index($('.side_slider_module .menu a.active'));
			if(i == $('.side_slider_module .menu a').length - 1){
				i = 0;
			}else{
				i++;
			}
			$('.side_slider_module').fadeTo(5000,1,function(){
				$('.side_slider_module .menu a:eq(' + i + ')').trigger('click');
			});
		}

		$('.side_slider_module .menu a:eq(0)').addClass('active');
		$('.side_slider_module .menu a').click(function(){
			i = $('.side_slider_module .menu a').index(this);
			x = $('.side_slider_module .canvas a:eq(' + i + ')').position().left * -1;
			$('.side_slider_module .canvas').animate({marginLeft:x},1100);
			$('.side_slider_module .menu a.active').removeClass('active');
			$(this).addClass('active');
			skipNextPSframe();
		});
	
		$('.side_slider_module .menu').fadeTo(1000,1,function(){
			$('.side_slider_module .menu a:eq(' + i + ')').trigger('click');
		});
	}
	
	
	
	//admin pebkac patches...
	//$('.article *[style]').not('input,form,object,a').removeAttr('style');
	$('.article a').each(function(){
		u = $(this).parents('u');
		if(u.length){
			u.after($(this));
			u.remove();
		}
	});
	
	//image margins
	$('.content img[align=left]').css('margin-right','10px');
	$('.content img[align=right]').css('margin-left','10px');

	$('#middle .article .content').each(function(){
		$(this).html($(this).html().replace(/[\-]{6,}/g,'<hr />'));
	});

	//typeface
	_typeface_js.renderDocument();

});
