$(document).ready(function(){
	//change all popups...
	var img = new Array();
	$(".article a").each(function(){
		s = "dow.open(this.href,";
		i = 0;
		if($(this).attr('onclick'))
			i = $(this).attr('onclick').toString().indexOf(s);
	   
		if(i){
			w = 640;
			h = 480;
		   
			u = $(this).attr('href');
			var id = 'popwinlink_' + uid();
			e = u.substring(u.lastIndexOf('.')+1).toLowerCase();
		   
			$(this).attr('id',id);
			if(e == 'png' || e == 'gif' || e == 'jpg'){
				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 = $("a#" + id).attr('onclick');
				c = $("a#popwinlink_0").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(','));
			   
				$("a#" + id).attr('onclick',"").addClass('popWindowLink').attr('href',"javascript:popWindow('" + u + "'," + w + "," + h + ");");
			}
		}
	});

	//change images
	$("#page img").each(function(){
		if($(this).attr('align') == 'left')
			$(this).css('margin-right','10px');
		else if($(this).attr('align') == 'right')
			$(this).css('margin-left','10px');
	});

	//scroll
	$("#image_scroll div.scroll_pan").width($("#image_scroll img").length * ($("#image_scroll img:eq(0)").width()));
	start_image_scroll();
});

//scroll
var start_image_scroll = function(){
	x = $("#image_scroll div.scroll_pan img:eq(0)").width();// + 40;
	$("#image_scroll div.scroll_pan").animate({'left':(x * -1) + 'px'}, Math.round(x * 20),"linear",function(){
		$("#image_scroll div.scroll_pan img:eq(0)").appendTo("#image_scroll div.scroll_pan");
		$("#image_scroll div.scroll_pan").css('left','0px');
		start_image_scroll();
	});
}

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

//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>");
	else
		popEl = $("<div class=\"popWindowDiv\" style=\"display:block;\"><iframe src=\""+ u +"\" width=\"" + w + "\" height=\"" + h + "\" border=\"0\" frameborder=\"0\"></iframe></div>");
	popEl.modal({
		onOpen: function (dialog) {
					dialog.overlay.fadeIn('slow', function () {
						dialog.container.slideDown('slow', function () {
							dialog.data.fadeIn('slow'); // See Other Notes below regarding
													   // data display property and
													   // iframe details
						});
					});
				},
		onClose: function (dialog) {
					dialog.data.fadeOut('slow', function () {
					  dialog.container.slideUp('slow', function () {
						dialog.overlay.fadeOut('slow', function () {
						  $.modal.close(); // must call this to have SimpleModal
										   // re-insert the data correctly and
										   // clean up the dialog elements
						});
					  });
					});
				}
				
	});
	$("#modalContainer").css('height',h).css('width',w).css('margin-left',(w / 2) * -1);
}