jQuery(document).ready(function () {
    prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm != null) {
        prm.add_pageLoaded(setupMask);
        prm.add_pageLoaded(setupErrorModal);
    };
	
	setupMask();
    setupErrorModal();
	
	//stick the footer at the bottom of the page if we're on an iPad/iPhone due to viewport/page bugs in mobile webkit
    if (navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod') {
        jQuery("#toolbar").css("position", "static");
    };

    //drop-down navigation
    jQuery("nav ul li").mouseover(function () {
        jQuery(this).children("ul").stop(true, true);
        jQuery(this).children("ul").slideDown(400);
    }).mouseleave(function () {
        jQuery(this).children("ul").slideUp(200);
    });

    //Search Box Text
    var search_box = jQuery("input[type='text'].search_box ");
    jQuery(search_box).val("Search our site...");
    jQuery(search_box).focus(function () {
        if (jQuery(search_box).val() == 'Search our site...') {
            jQuery(search_box).val("");
        };
    });
    jQuery(search_box).blur(function () {
        if (jQuery(search_box).val().trim() == '') {
            jQuery(search_box).val("Search our site...");
        };
    });

    //main sliders on home page
    jQuery("#scrolling_container").nivoSlider({
        effect: 'sliceDown',
        slices: 10,
        animSpeed: 1000,
        pauseTime: 8000,
        startSlide: 0,
        directionNav: true,
        directionNavHide: false,
        controlNav: false
    });

    //our partners rotator on home page
    jQuery("#partner_logos").nivoSlider({
        effect: 'sliceDown',
        slices: 10,
        animSpeed: 2000,
        pauseTime: 5000,
        startSlide: 0,
        directionNav: false,
        controlNav: false
    });

    //get latest tweet
    jQuery('#latest_tweet').liveTwitter('FGNW', {
        mode: 'user_timeline',
		showAuthor: false,
        limit: 1,
        refresh: false
    });

    //show/hide county/msa data
    jQuery(".nav_counties a").click(function (e) {
        e.preventDefault();
        var datatype = jQuery(this).attr("href");
        datatype = datatype.replace('#', '');
        ShowSpecificData(datatype);
        jQuery(".nav_counties a.selected").removeClass("selected");
        jQuery("a." + datatype).addClass("selected");
    });

    jQuery(".add_to_report, .add_to_report_icon").live('click',function () {
        var id = jQuery(this).attr("rel");
        var html = jQuery("#" + id).html();
        jQuery("input.item_HTML_" + id).val(html);
    });

    //colorbox modals
    jQuery("a.colorbox_form").colorbox({ opacity: "0.75", width: "700px", height: "400px", iframe: true });
	jQuery(".colorbox_website").colorbox({ opacity:"0.75", iframe:true, height:"80%", width:"80%" });
	jQuery("a[rel='photo']").colorbox({ photo: true, maxHeight:"80%", maxWidth:"80%" });

    //row striping
    jQuery(".boardMembers, .investors, #page_content table tbody").each(function () {
        jQuery(this).find("li:odd").addClass("alt");
        jQuery(this).find("tr:even").addClass("alt");
    });

    //Print Button
    jQuery('#page_tools a.print').click(function () {
        window.print();
        return false;
    });
	
	//Hide (Collapse) the toggle containers on load
	jQuery(".toggle_container").hide();
	
	//Hide (Collapse) the toggle containers on load
	jQuery("h2.open").toggleClass("active").next().slideToggle("slow");
	
	//set content height
    ResizeColumns();
    PlacePageTools();
	//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
	jQuery("h2.trigger").click(function(){
		jQuery(this).toggleClass("active").next().slideToggle("slow");
		return false; //Prevent the browser jump to the link anchor
	});

	//appends li at end of list to clear floated
	jQuery("#targetsector .toggle_container ul, #main_content ul.four_column").each(function(){
		jQuery(this).append("<li class='clear'></li>");
	});
	
	jQuery("#main_content ul.four_column li:nth-child(4n)").each(function(){
		jQuery(this).addClass("noPadding");
	});
	
    //back to top link
      jQuery("a.backToTop").click(function(){       
        //Animates scroll to the top of the page...and take 1 second to do it.
          jQuery('html, body').animate({ scrollTop:0 }, '1000');   
      });
	  
	  jQuery("#page_header + br").remove(); 
});

//set content height
function ResizeColumns() {
	var cHeight = jQuery("#main_content").height();
	var sHeight = jQuery("#sidebar").height() + 136;
	if (sHeight > cHeight) {
		jQuery("#main_content").css("min-height", sHeight + "px");
	};
};

function PlacePageTools() {
	if (jQuery("#page_header h1").height() > 51) {
		//var ptTop = Math.ceil(15 + ((jQuery("#page_header h1").height() - 50) / 2));
		var ptTop = 30;
		jQuery("#page_tools").css("top", ptTop);
	}
	var stTop = 88 + (jQuery("#page_header h1").height() - 50);
	jQuery("#social_buttons").css("top", stTop);
};

function ShowSpecificData(datatype) {
    jQuery("#county_msa .datatype:visible").fadeOut();
    jQuery("#county_msa .datatype." + datatype).fadeIn();
};

function setupMask() {
    jQuery(".mask").height(jQuery(window).height()).width(jQuery(window).width()).fadeTo("fast", 0.8);
    jQuery(".mask_text").css("top", jQuery(".mask").height() / 2 - 20 + "px");
}

function setupErrorModal() {
    jQuery(".error_panel").css("top", jQuery(window).height() / 2 - 55 + "px").css("left", jQuery(window).width() / 2 - 150 + "px");
}

