﻿Site =
{
	start: function() {
		$(window).load(Site.fixBackground);
		Site.fixNavigationSize();
		Site.initHeaders();
		Site.initLinks();
		Site.initDistrictSchoolList();
		Site.initReportSelector();
		Site.initOverviewReport();
		Site.initOverlayLinks();
		$('#page').css('visibility', 'visible');
		$('.widecolumn ul, .widecolumn ol').each(function() {
			if ($(this).parent().get(0).tagName == 'DIV')
				$(this).css('padding-right', '230px');
		});
	},

	initOverlayLinks: function() {
		var overlayLinks = $('a.overlay');
		if (overlayLinks.overlay) {
			if (!Browser.ie6())
				overlayLinks.overlay({
					onBeforeLoad: function(content) {
						$('.ayp_overlay').hide();
					}
				});
			else
				overlayLinks.overlay({
					onBeforeLoad: function(content) {
						$('.ayp_overlay').hide();
						$('#_overlayImage[src$=.png]').ifixpng().get(0).filters(0).sizingMethod = 'scale';
						content.find('img[src$=.png]').ifixpng();
					}
				});
		}
	},

	fixBackground: function() {
		var h = $('#main').innerHeight() - 20;
		if (Browser.ie() && $('#reportSelector')[0])
			h = h - 30;
		$('#content_bg_middle').css({
			'width': $('#content_bg_middle').width() + 'px',
			'height': h + 'px'
		});
	},

	fixNavigationSize: function() {
		if (Browser.ie7() || $.browser.safari && !Browser.chrome())
			$('#content').css('margin-top', '-49px');
	},

	initHeaders: function() {
		$('h2 + h3').css('margin-top', '-1em');
	},

	initLinks: function() {
		Site.initLinkHoverDivs($('#links a'));
		var activeLinkIndex = Site.getActiveLinkIndex();
		if (activeLinkIndex >= 0) {
			var activeLink = $($('#links a')[activeLinkIndex]);
			activeLink.unbind('mouseout');
			activeLink.triggerHandler('mouseover');
			activeLink.unbind('mouseover');
		}
	},

	initDistrictSchoolList: function() {
		if ($('.ds_list')[0] && !$('#search_results')[0])
			$.freezeHeader($('.ds_list:last'));
	},

	initReportSelector: function() {
		var reportSelector = $('#reportSelector');
		if (reportSelector[0]) {
			reportSelector.insertBefore($('#breadcrumbs')).after($('<div/>'));

			var tabs = reportSelector.find('li');
			var active = this.getActiveTabIndex(tabs);
			$('span', tabs).remove();
			var activeTab = tabs.eq(active);
			activeTab.append($('<span />').text(activeTab.text())).addClass('activeTab');

			Site.initLinkHoverDivs(tabs.find('a'));
			$('div, a', activeTab).hide();
		}
	},

	getActiveTabIndex: function(tabs) {
		return $('.overview_wrapper')[0] ? 0 :
				 $('.performance_report')[0] ? 1 :
				 $('.data_table')[0] ? 2 :
				 $('#iframe')[0] ? tabs.length - 4 :
				 $('#parents')[0] ? tabs.length - 3 :
				 $('#educators')[0] ? tabs.length - 2 : tabs.length - 1;
	},

	getActiveLinkIndex: function() {
		return $('#home_content')[0] ? 0 :
			$('#about_content')[0] ? 1 :
			$('#media_content')[0] ? 2 :
			$('#state_report_wrapper')[0] ? 3 : -1;
	},

	initOverviewReport: function() {
		$('.overview_current .baseheader td').css({
			'color': '#fff',
			'background-color': '#355575'
		});
		$('.overview_report .year:first').css({
			'color': '#355575',
			'background-color': '#eee'
		});
	},

	initLinkHoverDivs: function(eventTrigger) {
		var height = parseInt(eventTrigger.eq(0).css('margin-top'));
		if (isNaN(height))
			height = 0;

		eventTrigger.show().prev().css('display', 'inline').each(function() {
			var offset = parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
			if (isNaN(offset))
				offset = 0;
			$(this).css({
				'display': 'block',
				'width': $(this).parent().innerWidth() - offset + 'px',
				'height': height + 'px',
				'top': height + 'px'
			});
		});
		eventTrigger.mouseover(function() {
			$(this).prev().css('top', '0px');
		}).mouseout(function() {
			$(this).prev().css('top', height + 'px');
		});
	}
};

$(Site.start);
