$.fn.cycle.updateActivePagerLink = function(pager, currSlide, clsName) {
	    $(pager).each(function() {
	        $(this).children('li').children('a').removeClass('selected').fadeTo(0, 1.0).filter(':eq('+currSlide+')').fadeTo(150, 0.6).addClass('selected');
	    });
	};

$(document).ready(function() {

	$("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'facebook', showTitle: false, animationSpeed: 'slow', opacity: 0.5 });

	$('#twitter').show();	
	$('#nav').show();
	$('#btnSearch').hover(function() {
    	$(this).css({'cursor' : 'pointer'});
    });
		
	$('#home-slideshow').cycle({ 
	    fx:		'fade', 
	    speed:	 800, 
	    timeout: 6000, 
	    pager:  '#nav',
	    pagerAnchorBuilder: pagerFactory,
	    before:	onBefore
	});
	
	function pagerFactory(idx, slide) {
		//alert(idx);
        return '#nav li:eq(' + idx + ') a';
    };
    
    function onBefore(incomingSlide, outgoingSlide, options, isForwardNav) {
    	    	
    	var text = outgoingSlide.getElementsByTagName('img')[0].getAttribute('alt');
		var textlength = text.length;
		
		$("#caption").html(text);
		
		if(text=="") {
			$("#caption").hide();
		} else {
			$("#caption").css({ 'width' : textlength*6.25});
		}
    };
    
	var activeOpacity   = 1.0,
        inactiveOpacity = 1.0,
        fadeTime = 150,
        clickedClass = "selected",
        thumbs = "#nav li a";

    $(thumbs).fadeTo(0, activeOpacity).filter(':first').addClass(clickedClass).fadeTo(0, inactiveOpacity);
    
    // set width of slideshow nav
    var total = $("#nav li").size();
    $("#nav").css({ 'width' : 18*total});

    $(thumbs).hover(
        function(){
            $(this).fadeTo(fadeTime, activeOpacity);
        },
        function(){
            // Only fade out if the user hasn't clicked the thumb
            if(!$(this).hasClass(clickedClass)) {
                $(this).fadeTo(fadeTime, activeOpacity);
            } else {
            	$(this).fadeTo(fadeTime, inactiveOpacity);
            }
        });
     $(thumbs).click(function() {
		
		$('#home-slideshow').cycle('pause');

		$(thumbs).fadeTo(0, activeOpacity);
		$(thumbs).removeClass(clickedClass);
		
        /*// Remove selected class from any elements other than this*/
        var previous = $(thumbs + '.' + clickedClass).eq();
        var clicked = $(this);
        if(clicked !== previous) {
        	previous.removeClass(clickedClass);
        }
        clicked.addClass(clickedClass).fadeTo(0, inactiveOpacity);
     });
	
	$("a.single-img").fancybox({
		'overlayOpacity'	: '0.8',
		'overlayColor'		: '#000',
		'titlePosition' 	: 'inside',
		'titleFormat'		: formatTitle

	});
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
	    return '<div id="gallery-title"><span class="gtitle">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '</span>Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</div>';
	}

	
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
   		return this.animate({opacity: 'toggle'}, speed, easing, callback);
	};
	
	if(document.getElementById("commentform")) {
		$("#commentform").validate();
	}
	
	if(document.getElementById("mc-embedded-subscribe-form")) {
		$("#mc-embedded-subscribe-form").validate();
	}
	
	function megaHoverOver(){
    
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
					
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {
					// added 5 for column structure	
					rowWidth += $(this).width()+5; 
				});
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
		
			alert('row found');
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' : biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
		}
	}
	
	function megaHoverOut(){
		$(this).find(".sub").stop().fadeTo('fast', 0, function() {
			$(this).hide(); 
		});
	}

	var config = {    
		 sensitivity: 1.5, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 20, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("#sitenav li .sub").css({'opacity':'0'});
	$("#sitenav li").hoverIntent(config);
	
	
	if(typeof document.body.style.maxHeight === "undefined") {
				
		// Fix annoying IE6 background from moving down 
		
		$("#sitenav .sub").hover(function(){		
			var sh = $(this).height(); // get div height
			var bp = 10 - (300 - sh); // calculate orig pos (-) image height (-) div height		
	        $(this).css({"background-position" : "0 " + bp}); // reset height
		});
		
		// Hide form select on menu hover
			
	    $("#sitenav").hover(function(){
			$("#reserveform select.ie").css({ visibility: "hidden" });
		},function(){
			$("#reserveform select.ie").css({ visibility: "visible" });
		});
	};
	
});

// original pop-up

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

