

/*Carousel Javascript 
Written by Sean Madden (http://leftbracket.com)*/

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}

// our dollar function
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}



var theCarousel,theDivs,divWidth,origClassName,leftAnchor,rightAnchor;
var divsLocale = new Array;


var move = {

	right : function() {
			nextID = getCurrID()+1;
			document.getElementById(nextID).className += " current"
			toggleDisplay(nextID);
			animate('carousel',divsLocale[nextID],10);
		
		
	},
	left : function() {
			nextID = getCurrID()-1;
			document.getElementById(nextID).className += " current"
			toggleDisplay(nextID);
			animate('carousel',divsLocale[nextID],10);
	}
};

function toggleDisplay(id){
       if(id == (divsLocale.length-1)){
               rightAnchor.src = '/assets/images/arrow_right_dim.gif';
               rightAnchor.onclick = null;
       }
       else {
               rightAnchor.src = '/assets/images/arrow_right.gif';
               rightAnchor.onclick = function(){move.right();return false;};
               }
       if(id == 0){
               leftAnchor.src = '/assets/images/arrow_left_dim.gif';
               leftAnchor.onclick = null;
       }
       else {
               leftAnchor.src = '/assets/images/arrow_left.gif';
               leftAnchor.onclick = function(){move.left();return false;};
               }
}


function getCurrID(){
	current = getElementsByClass("current",theCarousel);
	activeDiv = current[0];
	currID = parseInt(activeDiv.getAttribute('id'))
	activeDiv.className = origClassName;
	return currID;
}

function prepareCarousel(){
       theCarousel = $('carousel');
       origClassName = theCarousel.getElementsByTagName('div')[0].className;
       theDivs = getElementsByClass(origClassName, theCarousel);
       if(!theCarousel.style.left) theCarousel.style.left = "0px"
       divWidth = theDivs[0].offsetWidth;
       for(var i=0;i<theDivs.length;i++){
               theDivs[i].setAttribute("id",i);
               divsLocale[i] = -(i * divWidth);
       }
       theDivs[0].className += " current";
}
function prepareLinks(){
	leftAnchor = $('moveLeft');
	rightAnchor = $('moveRight');
	leftAnchor.source = "/assets/images/arrow_left_dim.gif";
	rightAnchor.onclick = function(){move.right();return false;};
}


function animate(theElement, final_x, interval){
		
            var elem = $(theElement);
            var distance;
            var new_x;
            var current_x = parseInt(elem.style.left);
            if(elem.movement){
                        clearTimeout(elem.movement);              
            }

            if (current_x == final_x) return true;
            if (current_x < final_x){
                        distance = Math.ceil((final_x - current_x)/10);
                        new_x = current_x + distance;
            }

            if (current_x > final_x) {
                        distance = Math.ceil((current_x - final_x)/10);
                        new_x = current_x - distance;

            }
            elem.style.left = new_x + "px";
            recursion = "animate('"+theElement+"',"+final_x+","+interval+")";
            elem.movement = setTimeout(recursion,interval);

}


window.onload = function(){prepareCarousel();prepareLinks();}
//addLoadEvent(prepareGallery());

/* DOMScripting Highlight Page (You are Here) Script */
function highlightPage() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("nav")) return false;
  var nav = document.getElementById("nav");
  var links = nav.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) {
    var linkurl = links[i].getAttribute("href");
    var currenturl = window.location.href;
    if (currenturl.indexOf(linkurl) != -1) {
      links[i].className = "on";
      // var linktext = links[i].lastChild.nodeValue.toLowerCase();
     // document.body.setAttribute("id",linktext);
    }
  }
}
addLoadEvent(highlightPage);
/* Google Map */
var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;


function googleMap() {
	
    // Check to see if this browser can run the Google API
    if (GBrowserIsCompatible()) {

      // A function to create the marker and set up the event window
      function createMarker(point,name,html) {
        var marker = new GMarker(point);

        // The info window version with the "to here" form open
        to_htmls[i] = html + '<div class="to-from"><strong>To here</strong> - <a href="javascript:fromhere(' + i + ')">From here</a></div>' +
           'Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input class="map-info-input" type="text" maxlength=40 name="saddr" id="saddr" value="" /><br />' +
           '<INPUT class="map-info-submit" value="Get Directions" type="submit">' +
           '<input type="hidden" name="daddr" value="' +
           point.y + ',' + point.x + "(" + name + ")" + '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<div class="to-from"><a href="javascript:tohere(' + i + ')">To here</a> - <strong>From here</strong></div>' +
           'End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input class="map-info-input" type="text" maxlength=40 name="daddr" id="daddr" value="" /><br />' +
           '<INPUT class="map-info-submit" value="Get Directions" type="submit">' +
           '<input type="hidden" name="saddr" value="' +
           point.y + ',' + point.x + "(" + name + ")" + '"/>';
        // The inactive version of the direction info
        html = html + '<div class="to-from"><a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a></div>';

        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml('<div class="info-window">'+html+'</div>');
        });
        gmarkers[i] = marker;
        htmls[i] = html;
        i++;
        return marker;
      }

      // Display the map, with some controls, initial location , marker
      var map = new GMap(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.centerAndZoom(new GPoint(-122.325379, 47.614189), 4);
						map.openInfoWindowHtml(map.getCenterLatLng(), '<div class="info-window"><div class="dir"><strong>Driving Directions</strong></div><div class="to-from"><a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a></div></div>'); 

		    // Set up marker with info window
						var point = new GPoint(-122.325379, 47.614189);
      var marker = createMarker(point,'500 E Pike St Seattle WA 98122','<div class="dir"><strong>Driving Directions</strong></div>');
      map.addOverlay(marker);

    }

    // display a warning if the browser was not compatible
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}
// functions that open the directions forms
function tohere(i) {
  gmarkers[i].openInfoWindowHtml('<div class="info-window">'+ to_htmls[i]+'</div>');
}
function fromhere(i) {
  gmarkers[i].openInfoWindowHtml('<div class="info-window">'+ from_htmls[i]+'</div>');
}

addLoadEvent(googleMap);



