// remap jQuery to $
(function($){})(window.jQuery);

// function callback on hiding image
function superbgimage_hide(img) {
	$('.maintitle p').fadeOut("slow");


}

// function callback on showing image
// get title and display it
function superbgimage_show(img) {
	$(".maintitle p").html($("img.activeslide").attr("alt")).text();
	$('.maintitle p span').hide();
	$('.maintitle p').fadeIn("slow");
	$('.maintitle p span').delay(3000).fadeIn("slow");
}

/* trigger when page is ready */
$(document).ready(function (){

	$('.maintitle p').hide();
	
    // Options for SuperBGImage
    $.fn.superbgimage.options = {
        slideshow: 1, // 0-none, 1-autostart slideshow
        slide_interval: 7000, // interval for the slideshow
       speed: 'slow', // animation speed
	onShow: superbgimage_show, // function-callback show image
	onHide: superbgimage_hide // function-callback hide image
    };
 
    // initialize SuperBGImage
    $('#superbgimage').superbgimage().hide();
    
    $(function(){
	    $("#mainnav ul ul").hover(
	      function() {$(this).parent("li").addClass("active");}, 
	      function() {$(this).parent("li").removeClass("active");}
	    );
	});
		
	$('#main-content a#toggle').click(function(e) {
	  $('#content').slideToggle('500', function() {
	    // Animation complete. 
	  }); 

      $(this).text($(this).text() == 'Hide Text' ? 'Show Text' : 'Hide Text');     
      $(this).toggleClass("hid");
      
	  e.preventDefault();
	});
	
	 $('.page_imagery').cycle({
        fx:     'fade',
        speed:  '400',
        timeout: 6000,
		slideExpr: 'span'
		//next:   '#next1', 
	    //prev:   '#prev1'
	                          
    }); 
	
	$('.specials.paged-fade').cycle({ 
	    fx:     'scrollUp', 
	    speed:  'fast', 
	    timeout: 5000, 
	    pager:  '.specials.pager' 
	});
	
	if($("#contactform").length){$("#contactform").validate();}
	
});




///////////////////////


(function($) {
	$.fn.thumbPopup = function(options)
	{
		//Combine the passed in options with the default settings
		settings = jQuery.extend({
			popupId: "thumbPopup",
			popupCSS: {'border': '1px solid #000000', 'background': '#FFFFFF'},
			imgSmallFlag: "_t",
			imgLargeFlag: "_l",
			cursorTopOffset: 15,
			cursorLeftOffset: 15,
			loadingHtml: "<span style='padding: 5px;'>Loading</span>"
		}, options);
		
		//Create our popup element
		popup2 =
		$("<div />")
		.css(settings.popupCSS)
		.attr("id", settings.popupId)
		.css("position", "absolute")
		.appendTo("body").hide();
		
		//Attach hover events that manage the popup
		$(this)
		.hover(setPopup)
		.mousemove(updatePopupPosition)
		.mouseout(hidePopup);
		
		function setPopup(event)
		{
			var fullImgURL = $(this).attr("src").replace(settings.imgSmallFlag, settings.imgLargeFlag);
			
			$(this).data("hovered", true);
			
			//Load full image in popup
			$("<img />")
			.bind("load", {thumbImage: this}, function(event)
			{
				//Only display the larger image if the thumbnail is still being hovered
				if ($(event.data.thumbImage).data("hovered") == true) {
					$(popup2).empty().append(this);
					updatePopupPosition(event);
					$(popup2).show();
				}
				$(event.data.thumbImage).data("cached", true);
			})
			.attr("src", fullImgURL);
			
			//If no image has been loaded yet then place a loading message
			if ($(this).data("cached") != true) {
				$(popup2).append($(settings.loadingHtml));
				$(popup2).show();
			}
			
			updatePopupPosition(event);			
		}
		
		function updatePopupPosition(event)
		{
			var windowSize = getWindowSize();
			var popupSize = getPopupSize();
			if (windowSize.width + windowSize.scrollLeft < event.pageX + popupSize.width + settings.cursorLeftOffset){
				$(popup2).css("left", event.pageX - popupSize.width - settings.cursorLeftOffset);
			} else {
				$(popup2).css("left", event.pageX + settings.cursorLeftOffset);
			}
			//hacked to get it above always - pierce
			//$(popup2).css("top", event.pageY - popupSize.height - settings.cursorTopOffset);

		}
		
		function hidePopup(event)
		{
			$(this).data("hovered", false);
			$(popup2).empty().hide();
		}
		
		function getWindowSize() {
			return {
				scrollLeft: $(window).scrollLeft(),
				scrollTop: $(window).scrollTop(),
				width: $(window).width(),
				height: $(window).height()
			};
		}
		
		function getPopupSize() {
			return {
				width: $(popup2).width(),
				height: $(popup2).height()
			};
		}

		//Return original selection for chaining
		return this;
	};
})(jQuery);


$(function(){
	$("img.logo").thumbPopup({
		imgSmallFlag: "_s",
		imgLargeFlag: "_l",
		popupCSS: {'z-index': '5000', 'bottom':'60px'}
	});
});






