// Sitewide scripts using jquery
// place this at the bottom of other script includes to activate jquery plugins

// these scripts will fire after html has loaded
$(document).ready(function() { 
	
// initialise Header Rotating Images using InnerFade
	$("#banner").innerfade({
		containerheight: "295px", //uncomment to fix a height
		speed: 2000,
		timeout: 5000,
		type: "random"
	});
	// initialise Header Rotating Images using InnerFade
	$(".gallery").innerfade({
		containerheight: "225px", //uncomment to fix a height
		speed: 2000,
		timeout: 5000,
		type: "random"
	});
	
// Function to hide email addresses
	$('span.mailme').each(function(){
		var $this = $(this);
		var at = / at /;
		var dot = / dot /g;
		var addr = $($this).text().replace(at,"@").replace(dot,".");
		$($this).after('<a href="mailto:'+addr+'">'+ addr +'</a>');
		$($this).remove();
	});

// Add external link icon to text hyperlinks
	$("a").filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).addClass("external_link").attr("target", "_blank");
	
//validate form
	$("form").validate();
	
/*// Add an rss link icon to text hyperlinks
	$("a[href^='http://feeds']:not(:has(img))").addClass("feed_link").attr("target", "_blank");
	
// colorbox photo gallery
	$('.gallery a').colorbox({
    onOpen:function(){ $('embed, object, select').css({'visibility' : 'hidden'}); },
    onCleanup:function(){ $('embed, object, select').css({'visibility' : 'visible'}); }
  });
	
// Confirm comment abuse
	$(".abuse a").click(function() {
		var type = $(this).attr("data-review");
		if (type == "N") {
			alert("This comment has already been reviewed and approved by a moderator");
		} else {
			var answer = confirm('Are you sure you want to flag this comment as abuse?');
			if (answer) {
				var url = $(this).attr("href");
				$.post(url, function() {
					log(url);
					alert("Thank You, this comment will be reviewed by a moderator");
				});
			}
		}
		return false; // answer is a boolean
	});
	
// monthly calendar event rollover
  $(".calendar-event a").mouseover(function() {
    $event = $(this);

    //calculate div in relation to edge of document
    var edge = $(document).width();
    var offset = $event.offset();
    var distance = edge - (offset.left + 30);

    var $long = $event.next(".eventname-long");

    //reposition the div if it is too long and falls off the page
    if ((distance) <= $long.width()) {
      var position = "-" + ($long.width() - (distance - 30)) + "px";
      $long.css({"left": position});
    }

    //change the z index of the event above all others
    $event.css({"z-index": "999"});

    //show and hide div
    $(".eventname-long").hide();
    $long.show();
    $long.mouseout(function() {
      $(this).hide();
      $event.css({"z-index": "0"});
    });
  });

//monthly calendar dialog box
  $(".eventname-long").click(function() {
    //create a div for the dialog box if it is not there
    var $popup = $(".modal-window");
    if (!$popup.length) {
      $popup = $('<div class="modal-window"></div>').appendTo('body');
    }

    //store the date variables
    var current = $(this).attr("data-current");
    var date = $(this).attr("data-date");

    //ajax the page data into the div
    $popup.load("/calendar-detail.php", {"current": current}, function() {
      //show the dialog box
      $popup.dialog({
        title: date,
        width: 800,
        modal: true
      });
    });

    return false;
  });

//monthly calendar prev and next rollover
	$("#calendar-nav a").hover(
		function() {
			$(this).addClass("ui-state-hover ui-corner-all");
		},
		function() {
			$(this).removeClass("ui-state-hover ui-corner-all");
		}
	);

// iSelect Popup
	$('#iselect_design').hover(function() {
		$(this).find('#iselect_promo:hidden').fadeIn(500);
	}, function() {
		$(this).find('#iselect_promo:visible').fadeOut(500);
	});
	*/
//end $(document).ready();
}); 
