function myErrorHandler() { return true; }
window.onerror = myErrorHandler;
function getPageSize() {
	var doc = document.documentElement;
	var width = window.innerWidth || self.innerWidth || (doc && doc.clientWidth) || document.body.clientWidth;
	var height = window.innerHeight || self.innerHeight || (doc && doc.clientHeight) || document.body.clientHeight;
	size = new Array(width, height);
	return size;
}
var PopupWindow = {
	openImage: function(src) {
		if ( src.length > 0 ) {
			if ( $('#popup-window').is('DIV') ) {
				$('#popup-overlay').click();
			}
			var popup = document.createElement('DIV');
			$(popup).appendTo(document.body).hide().attr({
				id: 'popup-window'
			});
			var img = new Image();
			$(document.createElement('DIV')).appendTo($('#popup-window')).attr({
				id: 'popup-close'
			})
			.click(function() {
				$('#popup-overlay').click();
			});
			if ( src.indexOf("?") !== -1 ){
				var src = src.substr(0, src.indexOf("?"));
			}
			var url = src + '?' + Math.random();
			$(img).appendTo($('#popup-window')).load(function() {
				var pageSize = getPageSize();
				$('#popup-window').css({
					'display': 'block'
				});
				var x = pageSize[0] - 150;
				var y = pageSize[1] - 150;
				var width = img.width;
				var height = img.height;
				if ( width > x ) {
					height = height * (x / width);
					width = x;
					if ( height > y ) {
						width = width * (y / height);
						height = y;
					}
				} else if ( height > y ) {
					width = width * (y / height);
					height = y;
					if (width > x) {
						height = height * (x / width);
						width = x;
					}
				}
				$(img).css('width', width + 'px');
				$(img).css('height', height + 'px');
				$("#popup-window").css({
					width: width + "px",
					left: ((pageSize[0] - width)/2) + "px",
					top: ((pageSize[1] - height - 50)/2) + "px"
				});
			});
			img.src = url;
			var current = false;
			var next = false;
			var imgs = $('ul.portfolio li a').get();
			for ( var i = 0; (i < imgs.length && !next); i++ ) {
				if ( imgs[i].href.indexOf(src) >= 0 || imgs[i].href == src ) {
					current = true;
					total = "Image " + (i + 1) +" of "+ (imgs.length);
				} else {
					if ( current ) {
						var next = $(document.createElement('a')).attr({
							'href': imgs[i].href + '?' + Math.random(),
							'target': 'popup-image',
							'class': 'next'
						})
						.html('>>');
					} else {
						var prev = $(document.createElement('a')).attr({
							'href': imgs[i].href + '?' + Math.random(),
							'target': 'popup-image',
							'class': 'prev'
						})
						.html('<<');
					}
				}
			}
			if ( prev ) {
				$(prev).appendTo($("#popup-window"));
			}
			if ( next ) {
				$(next).appendTo($("#popup-window"));
			}
			$('#popup-overlay').show();
			$('#popup-overlay').height($(window).height() + 'px');
			$("#popup-window").stylize();
		}
	}
}
$.extend($.fn, {
	stylize: function() {
		$(this).find('#calendar table tbody tr:last').addClass('last');
		$(this).find('#calendar a.prev, #calendar a.next').each(function() {
			$(this).click(function() {
				var url = $(this).attr('href');
				$.ajax({
					type: "GET",
					url: url,
					success: function(str) {
						$('#calendar').empty().html(str).parent().stylize();
					},
					dataType: ''
				});
				return false;
			});
		});
		
		$(this).find('a.ex2trigger').each(function() {
			$(this).click(function() {
				var url = $(this).attr('href');
				$('#ex2').fadeIn().jqm({ajax: url, trigger: 'a.ex2trigger'});
				return false;
			});
				
		});
		
		$(this).find('ul.portfolio a[target="popup"], a[target="popup-image"]').click(function() {
			PopupWindow.openImage($(this).attr('href'));
			return false;
		});
	}
});

$(document).ready(function() {
	var screen = document.createElement('DIV');
	$(screen).appendTo(document.body).hide().attr({id: 'popup-overlay'}).click(function() {
		$(this).hide();
		$('#' + 'popup-window').remove();
	});
	$(document).stylize();
	$('#ex2').jqm({ajax: $('a.ex2trigger').attr('href'), trigger: 'a.ex2trigger'});
});
