var currentO, drop;
var t;

window.onload = function() {
    if(document.getElementById('SDSlider'))
        init_homeSlide();
    if(document.getElementById('slideshowBank'))
	    init_homeBannerFade();
	if(document.getElementById('LDSlider'))
	    init_ldSlide();
	if(document.getElementById('map_canvas'))
	    initMap();
	if(document.getElementById('locationsList'))
	    initLocationTax();
	if(document.getElementById('searchInput'))
	    waterMark();
		
	if(document.getElementById('eCardHolder'))
		ecard_init();
}
window.onunload = function() {
    if(document.getElementById('map_canvas'))
        GUnload();
}

function dropNav(object) {
	if(drop)
	{
		clearTimeout(t);
		hideNavNow();
	}
	drop = true;
	currentO = object;
	object.className = "on";
	object.firstChild.className = "on";
	var element = object.getElementsByTagName('ul')[0];
	if(element)
	{
		element.style.display = "block";
		element.onMouseOver = saveNav(this);
	}
}

function hideNav(object) {
	currentO = object;
	t = setTimeout('hideNavNow()',100);
}

function saveNav(object) {
	clearTimeout(t);
}

function hideNavNow() {
	currentO.className = "";
	currentO.firstChild.className = "";
	var element = currentO.getElementsByTagName('ul')[0];
	if(element)
		element.style.display = "none";
	drop = false;
}

function selectValue(id) {
    //location.href = document.getElementById(id).value;
    toBeBrokenDown = document.getElementById(id).value.split("|");

    targetWindow = toBeBrokenDown[0];
    targetURL    = toBeBrokenDown[1];

	if (targetWindow!=='') {
	// if a new Window name is specified, then it will
	// open in a new Window.
	window.open(targetURL,targetWindow,'toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
	// if we open a new window, then we have to re-set
	// the select box to the first option
	// which should have no value
	id.selectedIndex = 0;
		} else {
	// or else it will open in the current window		
	window.open(targetURL,'_top')
	}
}



var currentDisplay = 1; // The default loaded section on the page
var slideTimeout;
var slideDisplayCount = 0;
var slideOn = true;


function init_ldSlide() {
    var slideDisplay = document.getElementById("LDSlider").getElementsByTagName("dl");
    var tempHeight,tallest = 0;
	slideDisplayCount = slideDisplay.length;
	for(i=1;i<=slideDisplayCount;i++)
	{
		slideDisplay[i-1].id = "LD-Item"+i;
		tempHeight = slideDisplay[i-1].clientHeight;
		if(tempHeight > tallest)
		    tallest = tempHeight;
	}
	document.getElementById("LDSlider").style.width = (130*slideDisplayCount)+"px";
	document.getElementById("LDSlider").style.height = tallest+"px";
	document.getElementById("LD-Frame").style.height = tallest+"px";
}

function ScrollLD(num)
{
    if(document.getElementById('LD-Item1'))
    {
	    var DisplayImage = "LD-Item" + num;
        
    	
	    // Get the element we want to scroll, get the position of the element to scroll to
    	
	    theScroll = document.getElementById('LD-Frame');
	    position = findElementPos(document.getElementById(DisplayImage));

	    // Get the position of the offset div -- the div at the far left.
	    // This is the amount we compensate for when scrolling

	    offsetPos = findElementPos(document.getElementById('LD-Item1'));
	    position[0] = position[0] - offsetPos[0];

	    scrollStart(theScroll, theScroll.scrollLeft, position[0]);
	    // return false;
    	
	    document.getElementById('locationCounter').innerHTML = "Location "+num+" of "+slideDisplayCount;
    }
}


var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 50;
	scrollanim.element = elem;
	
	scrollanim.timer = setInterval("scrollHorizAnim();", 15);

}
function scrollHorizAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

// Utility: Find the Y position of an element on a page. Return Y and X as an array

function findElementPos(elemFind)
{
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	//console.log("Found element "+elemFind+" at "+elemY+"/"+elemX);

	return Array(elemX, elemY);
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}

function moveLDRight() {
	currentDisplay++;
	if(currentDisplay > slideDisplayCount)
		currentDisplay = 1;
	ScrollLD(currentDisplay);
}

function moveLDLeft() {
	currentDisplay--;
	if(currentDisplay < 1)
		currentDisplay = slideDisplayCount;
	ScrollLD(currentDisplay);
}




// homeSlide
var homeSlide_status = "";
function init_homeSlide() {
	document.getElementById("SDSlider").innerHTML += document.getElementById("SDSlider").innerHTML;
    var slideDisplay = document.getElementById("SDSlider").getElementsByTagName("dl")
	slideDisplayCount = slideDisplay.length;
	for(i=1;i<=slideDisplayCount;i++)
	{
		slideDisplay[i-1].id = "SD-Item"+i;
	}
	document.getElementById("SDSlider").style.width = (350*(slideDisplayCount+1))+"px";
	
	slideTimeout = setInterval('moveSlideRight()', 5000);
	document.getElementById('features').onmouseover = function() { clearInterval(slideTimeout); };
	document.getElementById('features').onmouseout = function() { slideTimeout = setInterval('moveSlideRight()', 5000); };
}

function ScrollDisplay(num)
{
    if(document.getElementById('SD-Item1'))
    {
	    var DisplayImage = "SD-Item" + num;
        
    	
	    // Get the element we want to scroll, get the position of the element to scroll to
    	
	    theScroll = document.getElementById('SD-Frame');
		if(document.getElementById(DisplayImage)) {
			position = findElementPos(document.getElementById(DisplayImage));
		}
		else {
			position = Array(0,0);
		}

	    // Get the position of the offset div -- the div at the far left.
	    // This is the amount we compensate for when scrolling

	    offsetPos = findElementPos(document.getElementById('SD-Item1'));
	    position[0] = position[0] - offsetPos[0];

	    scrollStartHome(theScroll, theScroll.scrollLeft, position[0]);
	    // return false;
    }
}

function scrollStartHome(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 50;
	scrollanim.element = elem;
	
	scrollanim.timer = setInterval("scrollHorizAnimHome();", 15);

}
function scrollHorizAnimHome()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
		if(currentDisplay > slideDisplayCount/2) {
			currentDisplay = currentDisplay - (slideDisplayCount/2);
			scrollanim.element.scrollLeft = (currentDisplay-1)*350;
		}
		homeSlide_status = "";
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

function moveSlideRight() {
	if(homeSlide_status != "pause") {
		currentDisplay++;
		 if(currentDisplay > slideDisplayCount-2) {
			homeSlide_status = "pause";
			// currentDisplay = 1;
		 }
			ScrollDisplay(currentDisplay);
	}
}

function moveSlideLeft() {
	if(homeSlide_status != "pause") {
		currentDisplay--;
		if(currentDisplay < 1) {
			homeSlide_status = "pause";
			//currentDisplay = slideDisplayCount;
			if(document.getElementById('SD-Frame').scrollLeft == 0) {
				document.getElementById('SD-Frame').scrollLeft = (slideDisplayCount/2)*350;
				currentDisplay = slideDisplayCount/2;
			}
		}
		ScrollDisplay(currentDisplay);
	}
}


var newWindow;
// Google Address
function googleAddress() {
    var saddress = document.getElementById('gaddress').value;
    var address = document.getElementById('eaddress').innerHTML.replace(/(<([^>]+)>)/ig,", ");
    
      var myWidth = 0, myHeight = 0;
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }
      
    newWindow = window.open("http://maps.google.com/maps?saddr="+saddress+"&daddr="+address,"Directions","width="+(myWidth - 50)+",height="+(myHeight - 50)+",resizable=yes,status=yes,toolbar=no,scrollbars=yes,location=no");
    if (window.focus) {newWindow.focus()}
	return false;
}

function CallMarker(id) {
    GEvent.trigger(marker[id], "click");
}

function onEnter(event) {
    if ((event.which == 13) || (event.keyCode == 13)) 
    {
        var saddress = document.getElementById('gaddress').value;
        var address = document.getElementById('eaddress').innerHTML.replace(/(<([^>]+)>)/ig,", ");
        
          var myWidth = 0, myHeight = 0;
          if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
          } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
          } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
          }
          
        newWindow = window.open("http://maps.google.com/maps?saddr="+saddress+"&daddr="+address,"Directions","width="+(myWidth - 50)+",height="+(myHeight - 50)+",resizable=yes,status=yes,toolbar=no,scrollbars=yes,location=no");
        if (window.focus) {newWindow.focus()}
	    return false;
    }
    else
    {
        return true;
    }
}


function toggleTax(id) {
    object = document.getElementById(id);
    
    if(object.className == "hide")
        object.className = "show";
    else
        object.className = "hide";
}


function getQueryVariable(variable) { 
	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] == variable) { 
			return pair[1]; 
		} 
	} 

}

function initLocationTax() {
    if(taxId = getQueryVariable('taxId'))
    {
        if(document.getElementById('taxId'+taxId))
            document.getElementById('taxId'+taxId).className = "show";
    }
    
}

function showTab2() {
    document.getElementById('ctl00_bodyHolder_divTab2').className = "show";
    document.getElementById('ctl00_bodyHolder_divTab1').className = "hide";
    document.getElementById('ctl00_bodyHolder_tab1').className = "";
    document.getElementById('ctl00_bodyHolder_tab2').className = "selected";
}

function waterMark() {
    if (document.getElementById('searchInput').value == "")
        document.getElementById('searchInput').value = "Search";
    
}

function waterMarkOff() {
    if (document.getElementById('searchInput').value == "Search")
        document.getElementById('searchInput').value = "";
}

function waterMark2() {
    if (document.getElementById('ctl00_searchInput').value == "")
        document.getElementById('ctl00_searchInput').value = "Search";
    
}

function waterMarkOff2() {
    if (document.getElementById('ctl00_searchInput').value == "Search")
        document.getElementById('ctl00_searchInput').value = "";
}

var eCardWindow;
function eCardPopup(id) {
    newWindow = window.open("/eCardCreate.aspx?id="+id,"eCard","width=640,height=480,resizable=yes,status=yes,toolbar=no,scrollbars=yes,location=no");
}

// Ecard js addition below

function ecard_popup(location) {
	newWindow = window.open(location,"eCard","width=640,height=700,resizable=yes,status=yes,toolbar=no,scrollbars=yes,location=no");
	newWindow.focus();
	return false;
}
function ecard_init() {
	var messageCount = 750;
	document.getElementById('eCardNamePH').innerHTML = ecard_clean(document.getElementById('eCardName').value);
	document.getElementById('eCardPatientPH').innerHTML = ecard_clean(document.getElementById('eCardPatient').value);
	document.getElementById('eCardRoomPH').innerHTML = ecard_clean(document.getElementById('eCardRoom').value);
	$('#eCardCopyPH').text(ecard_clean(document.getElementById('eCardCopy').value));
	
	$('#eCardName').bind("keyup blur",function() { document.getElementById('eCardNamePH').innerHTML = ecard_clean(this.value); });
	$('#eCardPatient').bind("keyup blur",function() { document.getElementById('eCardPatientPH').innerHTML = ecard_clean(this.value); });
	$('#eCardRoom').bind("keyup blur",function() { document.getElementById('eCardRoomPH').innerHTML = ecard_clean(this.value); });
	$('#eCardCopy').bind("keyup blur",function() {
    if (this.value.length > messageCount){
	    document.getElementById('msgCount').innerHTML = 0;
        this.value = this.value.substr(0,750);
        
    }else{
        document.getElementById('msgCount').innerHTML = messageCount - this.value.length;
    }
    $('#eCardCopyPH').text(ecard_clean(this.value));
	});
}
function ecard_clean(html) {
	return html.replace(/<\/?[^ >]+>/gi, '');
}

function popitup(url) {
newwindow = window.open(url,"name","width=640,height=700,resizable=yes,status=yes,toolbar=no,scrollbars=yes,location=no");
 if (window.focus) {newwindow.focus()}
return false;
}