
jQuery(document).ready(function($){
//*****Replace align attribute with class************************************************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class***********************************************************************************//
function replaceTarget() {
  if (!document.getElementsByTagName('a')) return false;
  $('a[target]').addClass('newwindow').removeAttr('target');
}
//*****The following function make it possible to have web standard popups***************************************************//
function strictNewWindow() {
  if (!document.getElementsByTagName('a')) return false;
  $('a.newwindow, a[href$=.pdf], a[href$=.doc]').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
}
//*****Add swfobject flash call**********************************************************************************************//
function initializeFlash() {
	if (!document.getElementById('flash-content')) return false;
	var flashvars = {};
	var params = {wmode: 'transparent'};
	var attributes = {};
	attributes.id = "flash";
	swfobject.embedSWF("/swf/flash-content.swf", "flash-content", "1024", "249", "8.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
}
//*****Partners Cyle**********************************************************************************************//
function initializeCycle() {
  if (!$('.partnerRotation').length > 0) return false;
  $('.partnerRotation').cycle('fade');
}
//*****View Calendar Switcher**********************************************************************************************//
function viewCalendarSwitcher() {
  if (!$('.viewCalendarAs').length > 0) return false;
  $('.viewCalendarAs ul').hide();
  $('.viewCalendarAs p').click(function() {
    $('.viewCalendarAs ul').slideToggle();
  });
}
//********************************product tab switcher*********************************//
function exploreTabSwitch() {
  if ($('ul.exploreTab').length != 1) return false;
  //hide all tabWraps on page load
  $('div.tabWrap').each(function (i) {
    $(this).hide();
  });
  //add an id to all tabs beginning with callTab and ending in a related number
  $('ul.exploreTab li a').each(function (i) {
    $(this).attr('id','callTab' + i);
  });
  //add a class of selected to the first tab
  $('ul.exploreTab li:first a').addClass('selected');
  //show the first tabWrap div
  $('ul.exploreTab + div.tabWrap').show();
  //when you click a tab...
	$('ul.exploreTab li a').click(function(){
	  //remove class selected from all tabs
    $(this).parent().parent().children('li').children('a').each(function (i) {
      $(this).removeClass('selected');
    });
    //hide all tabWrap divs
    $(this).parent().parent().parent().children('div').each(function (i) {
      $(this).hide();
    });
    //add a class of selected to the clicked a
    $(this).addClass('selected');
    //get the number postion of the tab clicked
    var position = $(this).attr('id').substr(7,2) - 0;
    //show corresponding tabWrap div
	  $('div.tabWrap:eq('+position+')').show();
	  //do not execute the link
	  return false;
	});
	
	
	var tab_value = getQuerystring('tab');
	if(tab_value == "zip"){
		$('#zipCodeTab').show();
		$('#callTab1').addClass('selected');
		$('#callTab0').removeClass('selected');
		$('#schoolDistrictsTab').hide();
		$('#keyIndicatorsTab').hide();
	}
	else if(tab_value == "school"){
		$('#zipCodeTab').hide();
		$('#schoolDistrictsTab').show();
		$('#callTab2').addClass('selected');
		$('#callTab0').removeClass('selected');
		$('#keyIndicatorsTab').hide();
	}
}
//***************************************strips query string values based on what key you use*****//
//***************************************ex: var tab_value = getQuerystring('tab');***************//
function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
	return default_;
  else
	return qs[1];
}
//********************************customize lightbox tab switcher*********************************//
function customizeTabSwitch() {
  if ($('ul.customizeTab').length != 1) return false;
  //hide all tabWraps on page load
  $('ul.tabWrap').each(function (i) {
    $(this).hide();
  });
  //add an id to all tabs beginning with callTab and ending in a related number
  $('ul.customizeTab li a').each(function (i) {
    $(this).attr('id','callTab' + i);
  });
  //add a class of selected to the first tab
  $('ul.customizeTab li:first a').addClass('selected');
  //show the first tabWrap ul
  $('ul.tabWrap:eq(0)').show();
  //when you click a tab...
	$('ul.customizeTab li a').click(function(){
	  //remove class selected from all tabs
    $(this).parent().parent().children('li').children('a').each(function (i) {
      $(this).removeClass('selected');
    });
    //hide all tabWrap divs
    $(this).parent().parent().parent().children('ul.tabWrap').each(function (i) {
      $(this).hide();
    });
    //add a class of selected to the clicked a
    $(this).addClass('selected');
    //get the number postion of the tab clicked
    var position = $(this).attr('id').substr(7,2) - 0;
    //show corresponding tabWrap div
	  $('ul.tabWrap:eq('+position+')').show();
	  $.nyroModalSettings({
		  height: $('#customize').height() + 10
		});
	  //do not execute the link
	  return false;
	});
}
//********************************product tab switcher*********************************//
function customizeCheckboxes() {
  //when a checkbox is clicked
  $(':checkbox').click(function () {
    //get what tabWrap we are viewing
    var position = $('ul.customizeTab li a.selected').attr('id').substr(7,2) - 0;
    countChecked(position);
  });
  //when the select all link is clicked
  $('#selectAll').click(function () {
    //get what tabWrap we are viewing
    var position = $('ul.customizeTab li a.selected').attr('id').substr(7,2) - 0;
    //check all of the boxes in that tabWrap
		$(this).parent().parent().children('ul.tabWrap:eq('+position+')').find(':checkbox').attr('checked', 'checked');
		countChecked(position);
	});
	//when the deselect all link is clicked
  $('#deselectAll').click(function () {
    //get what tabWrap we are viewing
    var position = $('ul.customizeTab li a.selected').attr('id').substr(7,2) - 0;
    //uncheck all of the boxes in that tabWrap
		$(this).parent().parent().children('ul.tabWrap:eq('+position+')').find(':checkbox').attr('checked', '');
		countChecked(position);
	});
	
	//when the select all link is clicked
  $('#selectAllKeys').click(function () {
    //check all of the boxes in that tabWrap
		$(this).parent('p').siblings('ul').children('li').children('input:checkbox').attr('checked', 'checked');
	});
	//when the deselect all link is clicked
  $('#deselectAllKeys').click(function () {
    //uncheck all of the boxes in that tabWrap
		$(this).parent('p').siblings('ul').children('li').children('input:checkbox').attr('checked', '');
	});
	
	//when the select all link is clicked
  $('#selectAllCount').click(function () {
    //check all of the boxes in that tabWrap
		$(this).parent('p').siblings('ul').children('li').children('input:checkbox').attr('checked', 'checked');
	});
	//when the deselect all link is clicked
  $('#deselectAllCount').click(function () {
    //uncheck all of the boxes in that tabWrap
		$(this).parent('p').siblings('ul').children('li').children('input:checkbox').attr('checked', '');
	});
	
	//when the select all link is clicked
  $('#selectAllDist').click(function () {
    //check all of the boxes in that tabWrap
		$(this).parent('p').siblings('ul').children('li').children('input:checkbox').attr('checked', 'checked');
	});
	//when the deselect all link is clicked
  $('#deselectAllDist').click(function () {
    //uncheck all of the boxes in that tabWrap
		$(this).parent('p').siblings('ul').children('li').children('input:checkbox').attr('checked', '');
	});
	//
	//
	function countChecked(position) {
	  //count the number of checkboxes chcked in a given tabWrap
    var n = $('ul.tabWrap:eq('+position+') input:checked').length;
    //update the text next to the name of the tabWrap with the count of checkboxes checked
    $('ul.customizeTab li a.selected span').text(n);
	}
}
//********************************initialize the nyroModal jquery plugin http://nyromodal.nyrodev.com/*********************************//
function initializeColorbox() {
  if ($('.nyroModal').length != 1) return false;
  $('.nyroModal').nyroModal();
}
/**********************************Show Hide details on repository pages*****************************************************/
function toggleDetails() {
	$('.detailsToggle').parent('li').children('div').hide();
	$('.detailsToggle').toggle(
		function () {
		$(this).parent('li').children('div').show();
		$(this).html('Details &#9650;');
		},
		function () {
		$(this).parent('li').children('div').hide();
		$(this).html('Details &#9660;');
		}
	);
}
/****************************Show/Hide FAQs*******************************/
function toggleFAQ() {
	$('.jv_button_accordion .element').hide();
	$('.jv_button_accordion .toggler').click(function () {
		if($(this).parent().siblings('.element:visible').length){
			$(this).parent().siblings('.element').slideToggle();
		}else{
			$('.jv_button_accordion .element:visible').slideToggle();
			$(this).parent().siblings('.element').slideToggle();
		}
  });
}
function scrollCustomReport(){
	var $tableWidth = $('#tableWrap table').width();
	if ($tableWidth > 949){
		$('#tableWrap table').wrap('<span class="scrollPane" />');
		$('#tableWrap .scrollPane').css({'overflow-x':'scroll', 'clear' : 'both', 'display' : 'block'});
	}
}
//*****Load all functions****************************************************************************************************//
	//toggleFAQ();
	scrollCustomReport();
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  initializeFlash();
  clearDefaultValue();
  initializeCycle();
  //viewCalendarSwitcher();
  exploreTabSwitch();
  initializeColorbox();
  toggleDetails();
});
