$(document).ready(function() {

	$.ajaxSetup({cache: false});

	$.validator.addMethod("endDate", function(value, element) {
            var startDate = $('.startDate').val();
            return Date.parse(startDate) <= Date.parse(value);
        }, "* End date must be after start date");

	$("#user_password").focus(function(){
		$("#passwordInfo").show();
	});

	if ((navigator.userAgent.indexOf("Firefox")!=-1) && (navigator.userAgent.indexOf("3.5")==-1)){
		$(".RLContent li a span.fileSize").attr('style','margin: -17px 15px 0 0;')
		$("#navigation ul ul ul").attr('style','left: 195px;')

	}

	$('#addPerson').click(function(){
		addPerson();
		return false;
	});


	$("#user_password").blur(function(){
		$("#passwordInfo").hide();
	});

	$("#advancedSearch").click(function(){
		$("#AdvancedOptions").slideToggle();
		return false;
	});

	Date.firstDayOfWeek = 0;
	$(".datepicker").datePicker({startDate:'01/01/1996'});

	$("#showRequests").click(function(){
		$(".publishRequests #requestDiv").slideToggle("slow");
		$(this).toggleClass("revealed");
		return false;
	})

	$(".eventList h3 a").click(function(){
		$(this).parent().parent().find('.slideContainer').slideToggle("slow");
		$(this).toggleClass("revealed");
		return false;
	})

	$(".schlocAdminTable h3 a").click(function(){
		$(this).parent().parent().find('.slideContainer').slideToggle("slow");
		$(this).toggleClass("revealed");
		return false;
	})

	$(".RLContent h4 a[show]").click(function(){
		var showWhich = $(this).attr('show');
		$("ul." + showWhich).slideToggle("slow");
		$("p.glossary." + showWhich).slideToggle("slow");
		$(this).toggleClass('revealed');
		return false;
	})

	$(".userMgmt h4 a[show]").click(function(){
		var showWhich = $(this).attr('show');
		$("." + showWhich).slideToggle("slow");
		$(this).toggleClass('revealed');
		return false;
	})

	$(".chooseSchool h5 a[show]").click(function(){
		var showWhich = $(this).attr('show');
		$("ul." + showWhich).slideToggle("slow");
		$(this).find("span").toggleClass('hide');

		return false;
	})

	$(".tool label").hover(
      function () {
		$(this).parent().css('position','relative');
        $(this).parent().find(".toolHint").show();
      },
      function () {
        $(this).parent().css('position','static');
		$(this).parent().find(".toolHint").hide();
      }
    );

	$(".tool img").hover(
      function () {
		$(this).parent().css('position','relative');
		$(this).parent().find(".rolesHint").show();
      },
      function () {
		$(this).parent().css('position','static');
        $(this).parent().find(".rolesHint").hide();
      }
    );


	$("#showArticles").click(function(){
		$("#slideContainer").slideToggle("slow");
		$(this).toggleClass("revealed");
		if($(this).html() == "Hide"){
			$(this).html('Show')
		}else{
			$(this).html('Hide')
		}
		return false;
	})

	$(".category").click(function(){
		$(this).toggleClass('highlight');
		return false;
	})

	$("#upFile").change(function () {
		$("#fakeInput").val($("#upFile").val());
	});

	$("#ajaxSearch select").change(function(){
		$("#results").html('<img src="/images/loading.gif" alt="loading..." />');
		var active = $("#user_active").val();
		var district = $("#district_id").val();
		{$.ajax({
			url : "/index.cfm?event=page.userindexajax&district_id=" + district + "&user_active=" + active,
			success : function (data) {
				$("#results").html(data);
				}
		});}
		return false;
	});

	$(".schoolLocResults h4 a[show]").click(function(){
		var showWhich = $(this).attr('show');
		$("ul." + showWhich).slideToggle("slow");
		$(this).toggleClass('revealed');
		return false;
	})

/**
	$("input#SchoolLocator").click(function(){
		if ($("input#SchoolLocator:checked").val() == '1'){
			$("#schLocWebContainer").attr('class','')
			$("#WebSchoolLocator").removeAttr('readonly')
			$("#WebSchoolLocator").removeAttr('disabled')
		}else{
			$("#schLocWebContainer").attr('class','disabled')
			$("#WebSchoolLocator").attr('readonly',true)
			$("#WebSchoolLocator").attr('disabled',true)
			$("#WebSchoolLocator").removeAttr('checked')
			$("span.webURL").hide();
		}
	})
**/
	$("input#WebSchoolLocator").click(function(){
		if ($("input#WebSchoolLocator:checked").val() == '1'){
			$("span.webURL").show();
			/** $("span.trial").show(); **/
		}else{
			$("span.webURL").hide();
			/** $("span.trial").hide(); **/
		}
	})


	  $(document).bind('reveal.facebox', function(){
		$('#facebox').draggable();
      })

	$(function(){
		$('.multiselect').multiSelect(
	{
		selectAll: false,
		noneSelected: "Please select options"
		});
	});

/*
	{
		$("#processTable")
			.tablesorter({debug: false, widgets: ['zebra']})
			.tablesorterFilter({filterContainer: $("#filter-box"),
			filterClearContainer: $("#filter-clear-button"),
			filterColumns: [0],
			filterCaseSensitive: false});
    }
*/
	{
		$("#tblRequests")
			.tablesorter({debug: false, widgets: ['zebra']})
    }

	{
		$("table.fileMgr")
			.tablesorter({debug: false})
    }

	{
		$(".eventList table")
			.tablesorter({debug: false})
    }

	{
		$(".schlocAdminTable table")
			.tablesorter({debug: false})
    }


	$("#processTable").tablesorter({
		headers: { 2: { sorter:'digit' } }
	});


	$('a[rel*=facebox]').facebox();

/*
	$table = $("#processTable").tablesorter( );
	FilterText = "";
	ColumnArray = ["School District"];
	for (i=0;i<ColumnArray.length;i++) {
        $("#processTable tbody tr").find("td:eq(" + i + ")").click( function() {
            clickedText = $(this).text();
            subText = clickedText.substring(0,2);
			FilterText = ((FilterText == subText) ? "" : subText );
			$.uiTableFilter( $table, FilterText, ColumnArray[i]);
        });
    }*/

	if($(".processStatus").length){
		$(".processStatus").mousemove(function(e){
			$(".cellInfo").css({
				top: (e.pageY + 8)+ "px",
				left: (e.pageX + 8) + "px"
			})
		})
	}
})

function accordian(districtId){
	var UL = '#' + 'd' + districtId;
	var link = '#link' + districtId;
	if($(link).html() == 'show users'){
		$(UL).slideDown();
		$(link).html('hide users')
	}else{
		$(UL).slideUp();
		$(link).html('show users')
	}

}

function processStatusPopup(taskitemid){
	jQuery.facebox({ ajax: taskitemid })
	//jQuery.facebox(function($) {
		//$.get(taskitemid, function(data) { $.facebox(data) })
	//})
}

function cffmCallback(field_name, url, type, win) {
		//Get URL variable
		var article = 0;
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if(pair[0] == "ArticleID"){
				article = pair[1];
			}
 		 }

		// Do custom browser logic
		url = '/cffm/cffm.cfm?editorType=mce&ArticleID=' + article + '&EDITOR_RESOURCE_TYPE=' + type;
		x = 700; // width of window
		y = 500; // height of window
		win2 = win; // don't ask, it works.  win2 ends up being global to the page, while win is only accessible to the function.
		cffmWindow = window.open(url,"","width="+x+",height="+y+",left=20,top=20,bgcolor=white,resizable,scrollbars,menubar=0");
		if ( cffmWindow != null )
		{
			// bring the window to the front
			cffmWindow.focus();
		}
}

$(function(){
  $("select#district_id").change(function(){
    $.getJSON("/index.cfm",{event: "action.getDistrictSchools", district_id: $(this).val()}, function(j){
      var options = '<option value="">Select...</option>';

      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
      }
      	$("select#school_id").html(options);
    })
  })
})

function loadGoogleMapMulti() {
	var pYSchool = (json1.Placemark[0].ExtendedData.LatLonBox.north+json1.Placemark[0].ExtendedData.LatLonBox.south)/2;
	var pYStart = (json2.Placemark[0].ExtendedData.LatLonBox.north+json2.Placemark[0].ExtendedData.LatLonBox.south)/2;
	var pYEnd = (json3.Placemark[0].ExtendedData.LatLonBox.north+json3.Placemark[0].ExtendedData.LatLonBox.south)/2;
	var pXSchool = (json1.Placemark[0].ExtendedData.LatLonBox.west+json1.Placemark[0].ExtendedData.LatLonBox.east)/2;
	var pXStart = (json2.Placemark[0].ExtendedData.LatLonBox.west+json2.Placemark[0].ExtendedData.LatLonBox.east)/2;
	var pXEnd = (json3.Placemark[0].ExtendedData.LatLonBox.west+json3.Placemark[0].ExtendedData.LatLonBox.east)/2;
	var pYCenter = (pYSchool+pYStart+pYEnd)/3;
	var pXCenter = (pXSchool+pXStart+pXEnd)/3;

	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		var directionsPanel = document.getElementById("route");
		map.addControl(new GLargeMapControl());

		function onGDirectionsLoad(){
			map.addOverlay(directions.getPolyline());
			var distancee = directions.getDistance();
			$("#mapLegend #distance span").html(distancee.html);
			var newBounds = directions.getBounds()
			var newZoom = map.getBoundsZoomLevel(newBounds);
			var newCenter = newBounds.getCenter();
			map.setCenter(newCenter,newZoom);
			directions.clear();
		}

		//Add the School Icon
			var point = new GLatLng(pYSchool,pXSchool);
            var iconSchool = new GIcon(G_DEFAULT_ICON);
			iconSchool.image = "/images/school.png";
			iconSchool.iconSize = new GSize(35,35);
			iconSchool.iconAnchor = new GPoint(17, 17);
			iconSchool.infoWindowAnchor = new GPoint(18, 20)
			markerOptions = { icon:iconSchool };
			marker = new GMarker(point, markerOptions)
			map.addOverlay(marker);

		//Add the Start Point Icon
        	var point = new GLatLng(pYStart,pXStart);
			var iconHome = new GIcon(G_DEFAULT_ICON);
			iconHome.image = "/images/home.png";
			iconHome.iconSize = new GSize(35,35);
			iconHome.iconAnchor = new GPoint(17, 17);
			iconHome.infoWindowAnchor = new GPoint(18, 20)
			markerOptions = { icon:iconHome };
			marker = new GMarker(point, markerOptions)
			map.addOverlay(marker);

		//Add the End Point Icon
			var point = new GLatLng(pYEnd,pXEnd);
			var iconHome2 = new GIcon(G_DEFAULT_ICON);
			iconHome2.image = "/images/home2.png";
			iconHome2.iconSize = new GSize(35,35);
			iconHome2.iconAnchor = new GPoint(17, 17);
			iconHome2.infoWindowAnchor = new GPoint(18, 20)
			 markerOptions = { icon:iconHome2 };
			marker = new GMarker(point, markerOptions)
			map.addOverlay(marker);

		//Add the Directions
			var directions = new GDirections(map, directionsPanel);
			GEvent.addListener(directions, "load", onGDirectionsLoad);

			var waypoints = new Array(3);
				waypoints[0] = pYStart+","+pXStart;
				waypoints[1] = pYEnd+","+pXEnd;
				waypoints[2] = pYSchool+","+pXSchool;

		directions.loadFromWaypoints(waypoints);
		map.setCenter(new GLatLng(pYCenter,pXCenter),14);
	}
}

function loadGoogleMapSingle(popupText) {
	var pY = (json1.Placemark[0].ExtendedData.LatLonBox.north+json1.Placemark[0].ExtendedData.LatLonBox.south)/2;
	var pX = (json1.Placemark[0].ExtendedData.LatLonBox.east+json1.Placemark[0].ExtendedData.LatLonBox.west)/2;

	 if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		var point = new GLatLng(pY,pX);
		map.setCenter(new GLatLng(pY,pX), 15);

		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = "/images/school.png";
		icon.iconSize = new GSize(63,63);
		icon.iconAnchor = new GPoint(31, 31);
		icon.infoWindowAnchor = new GPoint(32, 32)


		markerOptions = { icon:icon };
		marker = new GMarker(point, markerOptions)

		map.addOverlay(marker);
		marker.openInfoWindowHtml(popupText);
	}
}
