﻿// AddThis
function addAddThis()
{
	var urlToUse = (("https:" == document.location.protocol) ? "https://secure.addthis.com/js/250/addthis_widget.js?pub=averaaddthis" : "http://s7.addthis.com/js/250/addthis_widget.js?pub=averaaddthis");
	$("#PageTools").append("<!-- AddThis Button BEGIN --><div class=\"AddThis\">" +
		"<script type=\"text/javascript\">var addthis_pub = 'averaaddthis';</script>" +
		"<a href=\"http://www.addthis.com/bookmark.php?v=250&pub=averaaddthis\" onclick=\"return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')\" onmouseout=\"addthis_close()\" onclick=\"return addthis_sendto()\">" + 
		"<img src=\"/imgs/sm-plus.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Share\" /> " +
		"Share This Page" +
		"</a>" +
		"<script type=\"text/javascript\" src='" + urlToUse + "'></script>" +
		/*
		"<a href=\"http://www.addthis.com/bookmark.php?v=250&pub=averaaddthis\" onmouseover=\"return addthis_open(this, \'\', \'[URL]\', \'[TITLE]\')\" onmouseout=\"addthis_close()\" onclick=\"return addthis_sendto()\">" +
		"<img src=\"/imgs/sm-plus.gif\" width=\"125\" height=\"16\" alt=\"Bookmark and Share\" style=\"border:0\"/></a>" +
		"<script type=\"text/javascript\" src='" + urlToUse + "'></script>" +*/
		"</div><!-- AddThis Button END -->");
}

try
  {
	$(addAddThis);
  }
catch(err)
  {
	// if error, allow the page to load anyway
  }



function imgAct(imgName) { if (document.images) { document.images[imgName].src = eval(imgName + "_on.src"); } }
function imgInact(imgName) { if (document.images) { document.images[imgName].src = eval(imgName + ".src"); } }

// ** fnOpenWindow **
// Opens a modeless dialog.

function strOpenWindowFeatures(iWindowWidth, iWindowHeight) {
	//var iWindowHeight = 630;
	//var iWindowWidth = 640;
	var iMouseX = 10;
	var iMouseY = 10;
	if ( window.event != null ) {
		//	alert("SRC ELEMENT: " + window.event.srcElement.tagName);
		//	alert("mouse X, Y relative to window: " + window.event.clientX + ", " + window.event.clientY);
		//	alert("mouse X, Y relative to screen: " + window.event.screenX + ", " + window.event.screenY);
		iMouseX = window.event.screenX;
		iMouseY = window.event.screenY; }
		//	alert("iMouseX, iMouseY: " + iMouseX + ", " + iMouseY);
	var iScreenX = window.screen.availWidth;
	var iScreenY = window.screen.availHeight;
		//	alert("iScreenX, iScreenY: " + iScreenX + ", " + iScreenY);
	var iWindowLeft = iMouseX;
	if ( iWindowLeft + iWindowWidth > iScreenX ) { iWindowLeft = iMouseX - iWindowWidth - 10; }
	if ( iWindowLeft < 10 ) { iWindowLeft = 10; }
		//	alert("iWindowLeft: " + iWindowLeft);
	var iWindowTop = iMouseY;
	if ( iWindowTop + iWindowHeight > iScreenY - 50 ) { iWindowTop = iMouseY - iWindowHeight - 60; }
	if ( iWindowTop < 10 ) { iWindowTop = 10; }
		//	alert("iWindowTop: " + iWindowTop);
	return "width=" + iWindowWidth + ", height=" + iWindowHeight + ", left=" + iWindowLeft + ", top=" + iWindowTop + ", channelmode=no, directories=no, fullscreeen=no, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, titlebar=yes, toolbar=no";
}

function fnOpenWindow(strWindowURL, strWindowName, iWindowWidth, iWindowHeight) {
	var objNewWindow = window.open(strWindowURL, strWindowName, strOpenWindowFeatures(iWindowWidth, iWindowHeight), false);
	if ( objNewWindow != null ) {
		objNewWindow.focus();
	}
}


// This function is also in the admin.js file,
//  and fully documented there...
function fnSimulateRepeaterCommand(strLinkButtonUniqueID, strHiddenFieldUniqueID, strHiddenFieldValue) {
	//alert("fnSimulateRepeaterCommand('" + strLinkButtonUniqueID + "', '" + strHiddenFieldUniqueID + "', '" + strHiddenFieldValue + "');");

	// Split and Join the UniqueID property of the LinkButton to get
	//  the DHTML ID and .NET __doPostBack ID for the server control.
	var aryLinkButtonID = strLinkButtonUniqueID.toString().split(":");
	var strLinkButtonClientID = aryLinkButtonID.join("_");
	var strLinkButtonPostBackID = aryLinkButtonID.join("$");

	// Split and Join the UniqueID property of the HtmlInputHidden
	//  to get the DHTML ID for the server control.
	var aryHiddenFieldID = strHiddenFieldUniqueID.toString().split(":");
	var strHiddenFieldClientID = aryHiddenFieldID.join("_");

	// Look for DHTML objects for the LinkButton and HtmlInputHidden.
	var objLinkButton = document.getElementById(strLinkButtonClientID);
	var objHiddenField = document.getElementById(strHiddenFieldClientID);

	// A variable to verify that everything will work...
	var blnOK = false;
	//alert("typeof __doPostback = '" + typeof __doPostBack + "'");
	if ( typeof __doPostBack == 'function' ) {
		if ( objLinkButton != null ) {
			if ( typeof objLinkButton == 'object' ) {
				if ( objHiddenField != null ) {
					if ( typeof objHiddenField == 'object' ) {
						if ( objHiddenField.value != null ) {
							if ( strHiddenFieldValue.toString().length > 0 ) {
								blnOK = true;
							} else { alert("ERROR! strHiddenFieldValue parameter is an empty string!"); }
						} else { alert("ERROR! DHTML element with id='" + strHiddenFieldClientID + "' has no 'value' attribute!"); }
					} else { alert("ERROR! DHTML element with id='" + strHiddenFieldClientID + "' is not an object!"); }
				} else { alert("ERROR! Could not find a DHTML element with id='" + strHiddenFieldClientID + "'"); }
			} else { alert("ERROR! DHTML element with id='" + strLinkButtonClientID + "' is not an object!"); }
		} else { alert("ERROR! Could not find a DHTML element with id='" + strLinkButtonClientID + "'"); }
	} else { alert("ERROR! Could not find a '__doPostBack' function!"); }

	if ( blnOK == true ) {
		objHiddenField.value = strHiddenFieldValue.toString();
		try {
			__doPostBack(strLinkButtonPostBackID,'');
		} catch (err) {
			alert("JavaScript error:\n" + err.description);
		}
	}
}


// Simplified Popup
// Usage: <a onclick="return popUp(this.href,250,250);" href="popup.aspx">Link</a>
function popUp(theLink, popUpWidth, popUpHeight) {
	var popUpWindow = window.open(theLink, "popUp", "width=" + popUpWidth +
	", height=" + popUpHeight +
	", left=50, top=50, resizable=yes, scrollbars=yes", false);
	if ( popUpWindow != null ) {
		popUpWindow.focus();
	}
	return false;
}
function closePopUp() {
	window.close();
}

// Cross-Browser Block Element Display Toggler
function toggleElement(elId) {
	var visibleStyle = "block";
	if (!document.all) {
		var el = document.getElementById(elId);
		if((el != null) && (typeof(el) == 'object')) {
			var elName = el.tagName.toLowerCase();
			if (elName == "tr") {
				visibleStyle = "table-row";
			}
			else if (elName == "tbody") {
				visibleStyle = "table-row-group";
			}
			else {
				visibleStyle = "block";
			}
		} else {
			alert("ElementId '" + elId + "' is null or not an object.");
		}
	}
	var v = ((document.getElementById(elId).style.display == visibleStyle) || (document.getElementById(elId).style.display == ""));
	document.getElementById(elId).style.display = v ? "none" : visibleStyle;
	return false;
}
function showElement(elId) {
	var visibleStyle = "block";
	if (!document.all) {
		var el = document.getElementById(elId);
		if((el != null) && (typeof(el) == 'object')) {
			var elName = el.tagName.toLowerCase();
			if (elName == "tr") {
				visibleStyle = "table-row";
			}
			else if (elName == "tbody") {
				visibleStyle = "table-row-group";
			}
		} else {
			alert("ElementId '" + elId + "' is null or not an object.");
		}
	}
	document.getElementById(elId).style.display = visibleStyle;
}
function hideElement(elId) {
	document.getElementById(elId).style.display = "none";
}
/*
Script for alternating row colors on "Looking For..." menu
Based on Zebra Tables - dynamically assigns even/odd classes
http://www.alistapart.com/articles/zebratables/
http://koke.amedias.org/articles/2006/02/13/zebra-tables-revisited
*/
function stripeMenu() {
	// The flag we'll use to keep track of whether the current li is odd or even
	var Even = false;
	// Get the menu ul element
	var navCol1 = document.getElementById('NavCol1');
	var ulList = navCol1.getElementsByTagName('ul');
	var theUl = ulList[0];
	var liList = theUl.getElementsByTagName('li');
	for (var l = 0; l < liList.length; l++) {
		liList[l].className =
		Even ? 'Even' : 'Odd';
		// flip from odd to even, or vice-versa
		Even =  ! Even;
	}
}

// Used for multiple window.onload events
// http://www.sitepoint.com/blogs/2004/05/26/closures-and-executing-javascript-on-page-load/
/*
addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
	// more code to run on page load 
});
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
			window.onload = function() {
			oldonload();
			func();
		}
	}
}

// PHOTO SLIDER
$(document).ready(function() {
    $('.PhotoSlider').wrapInner('<div class="Border"></div>');
	$('.PhotoSlider ul').wrap('<div class="Overflow"></div>');
	$('<p class="PhotoCaption" />').insertAfter('.Overflow');
	$('.Overflow ul').addClass('Container');
	$('.Container li').addClass('Slide');
	$('<div class="Buttons"></div>').insertAfter('p.PhotoCaption');
	$('<a href="#" class="Prev"></a>').appendTo('.Buttons');
	$('<a href="#" class="Next"></a>').appendTo('.Buttons');
	$('<em class="Count"></em>').insertBefore('.Next');
});

$.fn.accessSlider = function() {
    return this.each(function(i) {
        aSlider.init(this);
    });
};

var slideWidth = 260; // equals width of image plus padding on its container <li>
var slideSpeed = "slow";
var scrollNum = 1; // number of items scrolled left
var aSlider = {
    init: function(p) {
        slideLength = $(".Slide",p).length;
        $(".Count",p).prepend("<span></span>&nbsp;" + " of " + slideLength );
        newsContainerWidth = slideLength * slideWidth;
        $(".Container",p).css("width",newsContainerWidth + "px");
        $(".Prev",p).addClass("Inactive");
        animating = false;
        $(".Next",p).bind("click",{p:p,cssClass:".Next",alternateClass:".Prev"}, Slider_ButtonClick);
        $(".Prev",p).bind("click",{p:p,cssClass:".Prev",alternateClass:".Next"}, Slider_ButtonClick);
		$(".Count span").html(1);
		
		var currentSlide = $(".Slide", p).eq(0);
		$(".PhotoCaption", p).html($("img", currentSlide).attr("alt"));
    }
};

/// This function handles the click event for all buttons (Previous and Next inside the PhotoSlider)
function Slider_ButtonClick(event)
{
	//Assign Parameters from data oblject passed by jQuery, to local variables.
	var p = event.data.p;
	var cssClass = event.data.cssClass;
	var alternateClass = event.data.alternateClass;
	var isNext = cssClass == ".Next";

	// Clears the Click event so that 'Next' Click does not shoot up to the top of the screen.
	$(cssClass,p).click(function(){return false;});

            if (animating == false) {
                animating = true;
                animateLeft = parseInt($(".Container",p).css("left")) 
                if(isNext)
					animateLeft -= (slideWidth * scrollNum);
                else
					animateLeft += (slideWidth * scrollNum);
					
				var shouldContinue = isNext
										? animateLeft + parseInt($(".Container",p).css("width")) > 0
										: (animateLeft + parseInt($(".Container",p).css("width"))) <= parseInt($(".Container",p).css("width"));
				
                if (shouldContinue)
                {
                        $(alternateClass,p).removeClass("Inactive");
						$(".Container",p).animate({left: animateLeft}, slideSpeed, function()
						{
							$(this).css("left",animateLeft);
                        
			                
							//****** SETS THE Text Of the Caption on the box to reflect current Image Index ******//
							
							var current = parseInt($(".Count span", p).html());
							
							//The next two lines Copies the text from the alt of the new Img being scrolled to and then puts it in the title of the correct Image Container.
							
							//if its next you do not subtract anything because the div ids are 1 based and you will get a node in the correct slot.
							// however if the user clicked the back button, then you subtract 2, one for the one based index offset and 1 more for the back click.
							var currentSlide = $(".Slide", p).eq((isNext ? current : (current - 2)));
							if($("img", currentSlide).attr("alt") != null)
								$(".PhotoCaption", p).html($("img", currentSlide).attr("alt"));
							else
								$(".PhotoCaption", p).html("&nbsp;");
							
							var newCurrent = isNext ? current + 1: current -1;
							
							$(".Count span",p).html(newCurrent);
							//****** END SETS THE Text ... ******//
							
							var hitLimit = isNext
										? parseInt($(".Container",p).css("left")) + parseInt($(".Container",p).css("width")) <= slideWidth * scrollNum
										: parseInt($(".Container",p).css("left")) == 0;
							if (hitLimit)
							{
								$(cssClass,p).addClass("Inactive");
								$(cssClass,p).click(function(){return false;});
							}
							else
							{
								$(cssClass,p).bind("click",{p:p,alternateClass:alternateClass,cssClass:cssClass}, Slider_ButtonClick);
							}
							$(alternateClass,p).bind("click",{p:p,cssClass:alternateClass,alternateClass:cssClass}, Slider_ButtonClick);

							animating = false; 
                    });
                } else {
                    animating = false;
                }
				return false;
            }
        }
$(document).ready(function() {
    $(".PhotoSlider").accessSlider();
});
