//Cufon Initialization

var gothamSelectors = [
	'header.master nav a .title',
	'#pageWrap h1',
];
var chaparralSelectors = [
	'header.master nav a .subtitle',
	'h2'
];
gothamSelectors = gothamSelectors.join(', ');
chaparralSelectors = chaparralSelectors.join(', ');

Cufon.replace(gothamSelectors, {fontFamily: 'Gotham', hover: true})
Cufon.replace(chaparralSelectors, {fontFamily: 'Chaparral', hover: true})

$(document).ready(function(){
	var gallerySlider = function(){
		var $active = $('#main_image ul .active');
		var $next = $('#main_image ul .inactive:eq(0)');
		$next.animate({
			height: 'toggle'
		}, 1000, function(){
			$active
				.appendTo('#main_image ul')
				.removeClass('active')
				.addClass('inactive')
		}).removeClass('inactive').addClass('active');
	}
	
	if($('#main_image li').length > 1) {

		$('#main_image li:eq(0)').addClass('active');
		$('#main_image li:gt(0)').addClass('inactive');

		gallerySliderInterval = setInterval(gallerySlider, 5000);
	}	

	//Image Gallery
 $(".main_image").hide().fadeIn("slow");

	//Click and Hover events for thumbnail list
	$(".image_thumb ul li:first").addClass('active'); 
	$(".image_thumb ul li").click(function(){ 
		clearInterval(gallerySliderInterval);
		//Set Variables
		var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
		
		if ($(this).is(".active")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
		    $(".main_image a").attr({ href: imgTitle});
			$(".main_image img").attr({ src: imgTitle});
			$(".main_image img").hide().delay("200").fadeIn("slow");
			
		}
		
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
		$(this).addClass('active');  //add class of 'active' on this list only
		return false;
		
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
	var slide = function(){
		var $active = $('#slider ul .active');
		var $next = $('#slider ul .inactive:eq(0)');
		$next.animate({
			right : $('#slider').width()
		}, 1000, function(){
			$active
				.appendTo('#slider ul')
				.css({ right : '0px' })
				.removeClass('active')
				.addClass('inactive')
		}).removeClass('inactive').addClass('active');
	}
	
	if($('#slider li').length > 1) {

		$('#slider li:eq(0)').addClass('active');
		$('#slider li:gt(0)').addClass('inactive');

		setInterval(slide, 6500);
	}
	
	$('#calendar').fancybox({
		'type' : 'iframe',
		'autoScale' : false,
		'padding' : 0,
		'width' : 973,
		'height' : '75%'
	});
	$('.imageFeed a').fancybox();
	$('#main_image a').fancybox();
    $("a#inline").fancybox({
		'padding' : 0,
		'hideOnContentClick': true
	});
	 $('#nav li').hover(
        function () {
            //show its submenu
            $('ul', this).slideDown(100);
 
        }, 
        function () {
            //hide its submenu
            $('ul', this).slideUp(100);         
        }
    );
	//Removes empty paragraph tags
	$('.leftCol p').each(function(){
		if($(this).html() == '' || $(this).html() == ' ' || $(this).html() == '&nbsp;'){
			$(this).remove();
		}
	})
})


//Flashheed: fixes z-index on flash iframe embeds

var FlashHeed = (function(window) {
    
    var document = window.document;
    
    var gsub = function(string, pattern, replacement) {
        var result = '', source = string, match;
        
        while (source.length > 0) {
            if (match = source.match(pattern)) {
                result += source.slice(0, match.index);
                result += replacement;
                source  = source.slice(match.index + match[0].length);
            } else {
                result += source, source = '';
            }
        }
        return result;
    };
    
    var heed = function(el) {
        if(el === undefined || el === null) var el = document;
        
        
        var objects = el.getElementsByTagName('object');
        var len = objects.length;
        var i;
        
        for(i = 0; i < len; i++) {
            var o = objects[i];
            var params = o.getElementsByTagName('param');
            var params_length = params.length;
            var embeds = o.getElementsByTagName('embed');
            var embed = null;
            if(embeds.length > 0) var embed = embeds[0];

            // Handle embed tag (for non-IE)
            if(embed) {
                // Need to set the embed wmode attribute
                // In this case, we need to remove and re-add the child node
                embed.setAttribute('wmode', 'transparent');
                var nx = embed.nextSibling, pn = embed.parentNode;
                pn.removeChild(embed);
                pn.insertBefore(embed, nx);
            }

            // Handle param tags (for IE)
            var correct_wmode_found = false;
            var incorrect_wmode_found = false;

            for(var j = 0; j < params_length; j++) {
                if(params[j].name === 'wmode') {
                    if(/transparent/i.test(params[j].value) || /opaque/i.test(params[j].value)) {
                        // an existing wmode with "transparent" or "opaque" is found
                        correct_wmode_found = true;
                    } else {
                        incorrect_wmode_found = true;
                    }
                }
            }

            if(!correct_wmode_found || incorrect_wmode_found) {
                var html = o.outerHTML;
                var nx = o.nextSibling, pn = o.parentNode;

                // Do a string replacement for a window param that IE injects be default to the innerhtml
                html = gsub(html, /<param name="wmode".*?>/i, '');

                // Add the correct transparent wmode param
                html = gsub(html, /<\/object>/i, '<PARAM NAME="WMode" VALUE="Transparent"></object>');

                // Totally remove the object tag from the dom
                pn.removeChild(o);

                // Add it to our new div, only to clobber it immediately.
                // This is the only way we've found to force IE to unrender the object.
                // We use a new container for this because you can't mess with the innerhtml
                // of an object tag. (throws a runtime error)
                var div = document.createElement("div");
                div.appendChild(o);
                div.innerHTML = '';

                // Update it with our new HTML that has the correct param tag
                div.innerHTML = html;

                // Finally, insert this new div back in the original spot
                pn.insertBefore(div, nx);
            }
        }
    }
    
    return {
        heed: heed
    }
})(window);

FlashHeed.heed();

