(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);

function createCycle() {
	$('#posts article .images').each(function() {
		if(!$(this).hasClass('done')) {
			if($(this).find('img').length > 1) {
				$(this).css('cursor','pointer');
				var theImages = $(this);
				$(this).cycle({
					fx:      'scrollLeft',
					next:   theImages,
					timeout:  0
				});
			}
		}
		$(this).addClass('done');
	});
}

function infinite_scroll_callback() {
	createCycle();	
}

$(function() {

	$('html').addClass('js');
	
	$('.cform input:not(:submit)').each(function() {
		if($(this).attr('value') != '') {
			$(this).parent().addClass('on');
		}
	}).focus(function() {
		$(this).parent().addClass('on');
	}).blur(function() {
		if($(this).attr('value') == '') {
			$(this).parent().removeClass('on');
		}
	});

	createCycle();
	
	$('#home article:first').addClass('first');

	$(window).resize(function () {
		
		var ox = $(window).width();
		var oy = $(window).height() - $('header').outerHeight();
		
		$('#home article img').each(function () {

			var self = this;
			var img = new Image();
			img.onload = function () {
				var cx = this.width;
				var cy = this.height;

				$(self).parent().css({
					'width': ox,
					'height': oy
				});

				$(self).parent().parent().css({
					'width': ox * 5,
					'height': oy
				});

				$(self).parent().parent().parent().css({
					'width': ox,
					'height': oy
				});

				if (ox / oy > cx / cy) {
					$(self).css({
						'width': ox,
						'height': (ox * cy / cx),
						'left': 0,
						'top': 0.5 * (oy - (ox * cy / cx)),
						'position': 'relative'
					});

				} else {
					$(self).css({
						'width': oy * cx / cy,
						'height': oy,
						'top': 0,
						'left': 0.5 * (ox - (oy * cx / cy)),
						'position': 'relative'
					});
				}
			}
			img.src = $(self).attr('src');

		});

		$('#home .nav .next').live('click', function() {
			var ox = $(window).width();
			var oy = $(window).height() - $('header').outerHeight();
			if(!$('body').hasClass('animating')) {
				$('body').addClass('animating');
				$('#home #wrap article:first').animate({'margin-left': -ox}, 'slow', function() {
					var theHTML = $(this).html();
					$('#home #wrap').append('<article style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theHTML + '</article>');
					$(this).remove();
					$('body').removeClass('animating');
					$('#home #wrap article:first').addClass('first').siblings().removeClass('first');
				});
			}
			return false;
		});

		$('#home .nav .prev').live('click', function() {
			var ox = $(window).width();
			var oy = $(window).height() - $('header').outerHeight();
			if(!$('body').hasClass('animating')) {
				$('body').addClass('animating');
				var theHTML = $('#home #wrap article:last').html();
				$('#home #wrap').prepend('<article style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theHTML + '</article>');
				$('#home #wrap article:last').remove();
				$('#home #wrap article:first').css({'margin-left': -ox});
				$('#home #wrap article:first').animate({'margin-left': '0'}, 'slow', function() {
					$('body').removeClass('animating');
					$('#home #wrap article:first').addClass('first').siblings().removeClass('first');
				});

			}
			return false;
		});

	}).trigger('resize');

	if($('#google-map').length > 0) {
		function initialize() {
			var latlng = new google.maps.LatLng(53.400999,-2.973478);
			var myOptions = {
				zoom: 15,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			var map = new google.maps.Map(document.getElementById('google-map'), myOptions);
			var myLatLng = new google.maps.LatLng(53.400999,-2.973478);
			var beachMarker = new google.maps.Marker({
				position: myLatLng,
				map: map
			});
		}
		initialize();
	}
	
	$("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").addClass('external').attr('target','_blank').attr('title','Opens new window');
	
	FB.init({ 
		appId:'227040387347851', cookie:true, 
		status:true, xfbml:true 
	});

	$('#posts article .facebook').click(function() {
		var theURL = $(this).attr('href');
		FB.ui({
			method: 'feed', 
			link: theURL
		});
		return false;
	});

	$('#posts article .twitter').popupWindow({
		width: 550,
		height: 490,
		centerScreen:1 
	});

	$('.post-type-archive-bulletin #posts').infinitescroll({
		debug           : false,
		loading			: {
			img			: "/wp-content/uploads/2011/09/inf-loading-1160022711.gif",
			msgText		: "",
			finishedMsg	: ""
			},
		state			: {
			currPage	: "1"
			},
		nextSelector    : ".wpnav .left a",
		navSelector     : ".wpnav",
		contentSelector : "#posts",
		itemSelector    : "#posts article",
		pathParse		: ["/journal/page/", "/"]
		}, function() { window.setTimeout(infinite_scroll_callback(), 1);
	});

});
