(function($){
    $.fn.extend({
        hardStop: function(){
            var stopped = false
            while (!stopped) {
                this.each(function(){
                    var e = $(this);
                    while (e.queue().length){
                        e.stop(false, true);
                    }
                });
                stopped = true;
                this.each(function(){
                    var e = $(this);
                    if (e.queue().length){
                        stopped = false;
                    }
                });
            }
        }
    });
})(jQuery);

/* Check if PNG fix is needed */
function needsAlphaFix() {
	return (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
} // end needsAlphaFix

var cookiecheckval = '1';

/* Fix PNG alpha transparency in IE6 - based on bgsleight, modified for use with jQuery */
$.fn.alphaPNG = function() {
	$(this).each(function() {
		var bg = $(this).css('background-image');
		var br = $(this).css('background-repeat');
	
		if (br.match(/no-repeat/i) != null){
			var mysizemethod = "crop";
		} else {
			var mysizemethod = "scale";
		}
		if (bg.match(/\.png/i) != null){
			var mypng = bg.substring(5,bg.length-2);
			$(this).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='"+mysizemethod+"')");
			$(this).css('background-image','url(/interface/js/x.gif)');
		}
	});
} // end fn.alphaPNG

$.fn.wait = function(time, type) {
	time = time || 1000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
}; // end fn.wait

$.fn.loadSlideshow = function() {
	var container = this;

	function imgLoad(img, completeCallback, errorCallback){
		if(img!=null && completeCallback!=null){
			var loadWatch = setInterval(function() {
				if(img.complete) {
					clearInterval(loadWatch);
					completeCallback(img);
				}}, 100);
		}else{
			if(typeof errorCallback=="function") errorCallback();
		}
	}

	function displaySlideshow() {
		if($.cookie('slideshowPlayed') == cookiecheckval) {
			$('> .loading-message', container).hide();
			$('> #slide1', container).hide();
			$(container).playSlideshow(); // Start gallery slideshow
		} else {
			$('> .loading-message', container).fadeOut('normal', function() {
				$('> #slide1', container).fadeIn('normal', function() {
					$(container).playSlideshow(4000,2000); // Start gallery slideshow
				});
			});
		}
	}

	var imgcount = 1;
	var imgtotal = $('img', container).length;
	$(container).fadeIn();
	$('> li', container).hide();
	$('img', container).each(function(index) {
		imgLoad($(this)[0], function(img){
			imgcount++;
			if(imgcount == imgtotal) {
				displaySlideshow();
			}
		});
	});
}; // end loadSlideshow

$.fn.playSlideshow = function(timeout,speed) {
	var container = this;
	if($.cookie('slideshowPlayed') == cookiecheckval) {
		var headA = $('> #slide1 h2', container).html() + ' ';
		$('> #slide2 h2', container).prepend('<small>'+headA+'</small>');
		$('> #slide2', container).addClass('slide-played');
		$('#contentArea').prepend('<a id="playIntro" class="ui-corner-all intro-button" href="/index.php">Play Intro</a>');
		$('#playIntro').click(function() {
			$.cookie('slideshowPlayed', '0');
		}).hover(
			function() {
				$(this).animate({opacity: 0.8},500);
			},
			function() {
				$(this).animate({opacity: 0.5},500);
		});

	} else {
		$('#skipIntro').wait(500).animate({opacity: 0.2},500).hover(
			function() {
				$(this).animate({opacity: 0.6},500);
			},
			function() {
				$(this).animate({opacity: 0.2},500);
			}
		);
		$('> .slide', container).css('display','block');
		$('> #slide1 img', container).fadeIn(speed);
		$('> #slide1 h2', container).wait(eval(speed/3)).fadeIn(speed, function() {
			$(this).wait(timeout).fadeOut(speed);
			$('> #slide2 h2', container).wait(eval(timeout - 500)).fadeIn(speed);
			$('> #slide2 img', container).wait(eval(timeout + 500)).fadeIn(eval(speed * 1.5));
			$('#imageArea').wait(eval(timeout + 1000)).animate({height: '245px'}, speed, function() {
				$('#contentArea').animate({minHeight: '485px'}, speed);
				$('#skipIntro').fadeOut(speed);
				$('#contentContainer').slideDown(speed, function() {
					$('#pageFooter').fadeIn(speed, function() {
						$.cookie('slideshowPlayed', '1');
					});
				});
		   });
		});
	}
}; // end playSlideshow

$.fn.showSubMenu = function() {
	$(this).addClass('current');
	$('> ul',this).stop(true,true).slideDown('fast');
	$('> .nav-button-lv1', this).click(function() { return false; });
}; // end showSubMenu

$.fn.hideSubMenu = function() {
	$('> ul',this).stop(true,true).wait(500).slideUp('fast', function() {
		$(this).parent('li').removeClass('current');
	});
	$('> .nav-button-lv1', this).click(function() { return false; });
}; // end hideSubMenu


// jQuery-powered scripts
$(document).ready(function() {
	// Fix PNG alpha for old versions of IE
	if(needsAlphaFix()) {
		$('#pageHeader, #logo a, #contentArea, #contentA h2, #serviceList a, .insetTitle').alphaPNG();
	}

	// Create home page main image slideshow
	if($('body').hasClass('home')) {
		if($.cookie('slideshowPlayed') != cookiecheckval) {
			$('#imageArea').css('height','310px');
			$('#contentArea').css('min-height','385px').prepend('<a id="skipIntro" class="ui-corner-all intro-button" href="/index.php">Skip Intro</a>');
			$('#skipIntro').click(function() {
				$.cookie('slideshowPlayed', '1');
				
			});
			$('#contentContainer, body.home #pageFooter').hide();
		}
		$('#main-image-slideshow').loadSlideshow();
	}

	// Add hover effects to Services navigation
	$('#serviceList .image').animate({opacity: .8},1);
	$('#serviceList li').hover(
		function() {
			$.fx.off = false;
			var myid = $(this).attr('id');
			$('#'+myid+' .image').animate({opacity: 1},500);
		},
		function() {
			$.fx.off = false;
			var myid = $(this).attr('id');
			$('#'+myid+' .image').animate({opacity: .8},500);
	}); // end serviceList.li.hover
	
	// Display top navbar sub-navigation on mouseover
	$('.global-nav-has-sub').hover(function() {
			$(this).showSubMenu();
			return false;
		},
		function() {
			$(this).hideSubMenu();
			return false;
	});
	
	// Zebra-stripe specified tables
	$('.mfr-table tbody tr:nth-child(odd)').addClass('odd');
	
	// Apply Lightbox to elements with lightbox class
	$('.lightbox').lightBox();

});

