$(window).load(function(){
    preloadImages();
    initRollovers();
    initPrint();
    initClearValue();
    initToggle();
    initHelp();
    initTabbing();
    initEmail();
});

var browser = navigator.appName;
var versionInfo = navigator.appVersion;
var versionNum = parseFloat(versionInfo);
var vendor = navigator.vendor;
var IE = "Microsoft Internet Explorer";
var IE6 = "MSIE 6.0";
var IE7 = "MSIE 7.0";
var safari = "Apple Computer, Inc.";
var classAttribute = classAttr();   //Get the appropriate class attribute to set. IE and Firefox use different attributes for class in the DOM.
var imagePath = "images/";

/* preload images function :
-------------------------------------------------------------------------*/
//This function is mostly because IE 6 isn't loading the background images of the help dynamic layers.
var dynImg = ["bg-form-help.gif","bg-form-help-arrow.gif","bg-form-help-top.gif","bg-form-help-btm.gif","bg-large-help.gif","bg-large-help-arrow.gif","bg-large-help-btm.gif"];

var preloadFlag = false;
function preloadImages() {
	for (i=0; i<dynImg.length; i++) {
		var images = new Array();
		images[i] = new Image();
		images[i].src = imagePath + dynImg[i];
	}
	preloadFlag = true;
}
/* preload images function ^
-------------------------------------------------------------------------*/

/* image rollover funtion :
-------------------------------------------------------------------------*/

/* Function to initalize rollover */
function initRollovers(){	
	var rollovers = getElementsByClass('rollover',null,null);	//Get elements with a class of "rollover"
	
	if (rollovers.length > 0){		//If there is at least one element with a class "rollover"
		for(var i=0;i<rollovers.length;i++){	//Loop through all elements with class "rollover"
			var classValue = rollovers[i].getAttribute(classAttribute);
			
			if((versionInfo.match(IE6) != null) && (classValue.match('tpng') != null) ){ //If the browser is IE6 and a transparent png				
				addEvent(rollovers[i],'mouseover',rolloverPNG);	//Attach mouseover event
				addEvent(rollovers[i],'mouseout',rolloverPNG);	//Attach mouseout event	
			}
			else {
				addEvent(rollovers[i],'mouseover',rollover);	//Attach mouseover event
				addEvent(rollovers[i],'mouseout',rollover);	//Attach mouseout event
			}
		}		
	}
}

// This function grabs the src attribute and replaces "off" with "on" (for mouseover) or "on" with "off (for mouseout) in the attribute string. KM
function rollover(event) {
	var etype = event.type;
	var imgSrc = getObjectAttribute(this,"src");
	var newImgSrc;
	
	if (etype == 'mouseover'){
	    newImgSrc = imgSrc.replace('off.','on.','gi');
		this.setAttribute("src",newImgSrc);
	}
	
	else if (etype == 'mouseout') {
	    newImgSrc = imgSrc.replace('on.','off.','gi');
		this.setAttribute("src",newImgSrc);
	}
}

// IE 6 transparent PNG rollover function. KM
function rolloverPNG(event) {
	var etype = event.type;
	var imgSrc = getObjectAttribute(this,"name");
	var newImgSrc;
	
	if (etype == 'mouseover'){
	    newImgSrc = imgSrc.replace('off.','on.','gi');
		this.setAttribute('src',newImgSrc);
	}
	
	else if (etype == 'mouseout') {
	    newImgSrc = imgSrc.replace('on.','off.','gi');
		this.setAttribute('src',newImgSrc);
	}
}

// When function needs to be added inline (e.g. dynamically added content) KM
function rolloverInline(imgId,type) {
	var imgSrc = $("#" + imgId).attr("src");//jquery
	var newImgSrc;	

	if (type == 'over'){
	    newImgSrc = imgSrc.replace('off.','on.','gi');

	}
	else if (type == 'out') {
	    newImgSrc = imgSrc.replace('on.','off.','gi');
	}	
	$("#" + imgId).attr("src",newImgSrc);//jquery
}
/* image rollover function ^
-------------------------------------------------------------------------*/

/* pop up function :
-------------------------------------------------------------------------*/
function popWindow(href,title,sb,width,height) {
	window.open(href,title,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+ sb +',resizable=0,width='+ width +',height='+ height +',top=150,left=150');
}
/* pop up function ^
-------------------------------------------------------------------------*/

/* print functions :
-------------------------------------------------------------------------*/
function initPrint(){	
	var printBtns = $(".print-button");
		
	printBtns.each(function(){         
        $(this).click(function(){
            window.print();
            //If there are hidden tabs, make them display
            $(".tab-section").css("display", "block");
        });
    });
}
/* print functions ^
-------------------------------------------------------------------------*/

/* email functions :
-------------------------------------------------------------------------*/
function initEmail(){	
	var printBtns = $(".email-button");
	var sendPage = "send-to-friend.htm";
	
	printBtns.each(function(){         
        $(this).click(function(){
            popWindow(sendPage,"send_email","yes","300","300");
        });
    });
}
/* email functions ^
-------------------------------------------------------------------------*/

/* toggle pane functions :
-------------------------------------------------------------------------*/
function initToggle(){
    var tObj = $(".toggle-button");
    
    tObj.each(function(){         
        $(this).click(function(){
            togglePane(this);
        });
    });
}

function togglePane(obj){
    var pane;
    
    if($(obj).parent().hasClass("toggle-pane")){
        pane = $(obj).parent();
    }
    else {
        pane = $(obj).parent().find(".toggle-pane");    
    }
    
    toggle(pane);
}
/* toggle pane functions ^
-------------------------------------------------------------------------*/

/* help window functions :
-------------------------------------------------------------------------*/
function initHelp(){
    var tObj = $(".help-button");
    
    tObj.each(function(){         
        $(this).click(function(){
            toggleHelp(this);
        });
    });
}

function toggleHelp(obj){   
    var pane;
    
    if($(obj).parent().hasClass("help-text")){
        pane = $(obj).parent();
    }
    else if($(obj).siblings().hasClass("help-text")){
        pane = $(obj).parent().find(".help-text");
    }
    else {
        pane = $(obj).parent().parent().find(".help-text");    
    }
    
    $(pane).find("div.top").remove();
    $(pane).find("div.bottom").remove();
    $(pane).find("div.arrow").remove();
    $(pane).find("div.help-button").remove(); 
    
    var helpTop = document.createElement("div");
    $(helpTop).addClass("top");
    
    var helpBottom = document.createElement("div");
    $(helpBottom).addClass("bottom");
    
    var helpArrow = document.createElement("div");
    $(helpArrow).addClass("arrow");
    
    var closeBtn = document.createElement("img");
    $(closeBtn).addClass("button help-button");
    $(closeBtn).attr("src","images/btn-close-help.gif");
    $(closeBtn).attr("width", "6");
    $(closeBtn).attr("height", "6");
    $(closeBtn).attr("alt", "Close");
    $(closeBtn).click(function(){
        toggle(pane);
    });
    
    $(pane).prepend(helpTop);
    $(pane).append(helpBottom);
    $(pane).append(helpArrow);
    $(pane).append(closeBtn);
    
    toggle(pane);
    
    var left;
    var top;
    
    if($(pane).hasClass("pricing-details")){
        left = -265;
        top = 4;
    }
    else {
        left = 15;
        top = 7;
    }
    position(obj, pane, left, top);
}
/* help window functions ^
-------------------------------------------------------------------------*/

/* expand item functions :
-------------------------------------------------------------------------*/
function initExpand(){
    var tObj = $(".expand-header");
    
    tObj.each(function(){         
        $(this).click(function(){
            expand(this);
        });
    });
}

function expand(obj){
    var parent = $(obj).parent();
    var pane = $(parent).find(".expand-item");  
    
    if($(parent).hasClass("open")){
        $(parent).removeClass("open");
    }
    else {
        $(parent).addClass("open");    
    }
    
    toggle(pane);
}
/* expand item functions ^
-------------------------------------------------------------------------*/

/* section landing functions :
-------------------------------------------------------------------------*/
function contentSwitch(){
    _gel("html-landing").style.visibility = "visible";
    _gel("flash-landing").style.display = "none";
}
/* section landing functions ^
-------------------------------------------------------------------------*/

/* Begin tab change functions :
-------------------------------------------------------------------------*/
function initTabbing(){	
	var tabs = getElementsByClass("tab",null,null);	//Get elements with a class of "tab"
	
	if (tabs.length > 0){		//If there is at least one element with a class "tab"
		for(var i=0;i<tabs.length;i++){	//Loop through all elements with class "tab"
			addEvent(tabs[i],"click",tabClick);	//Attach click event
			addEvent(tabs[i],"mouseover",tabHover);	//Attach over event
			addEvent(tabs[i],"mouseout",tabHover);	//Attach out event
		}		
	}
}

function tabClick(event){    
    var tabs = getElementsByClass("tab",null,null);	//Get tabs
    var tabSections = getElementsByClass("tab-section",null,null);   //Get tab body sections
    	
	for(var t=0;t<tabs.length;t++){ //Loop through all tabs
	    var tabObj = tabs[t];
	    var tabSectionObj = tabSections[t];
	    	    
	    if(tabObj == this){ //Change clicked tab on
            tabObj.setAttribute(classAttribute, "tab selected");
            tabSectionObj.style.display = "block";
	    }
	    else {	//Change other tabs off	        
            tabObj.setAttribute(classAttribute, "tab");
            tabSectionObj.style.display = "none";		    
	    }
	}
}

function tabHover(event){ 
    var etype = event.type;
	
	if (this.getAttribute(classAttribute) != "tab selected"){
	    if (etype == "mouseover"){
		    this.setAttribute(classAttribute,"tab hover");
	    }
    	
	    else if (etype == "mouseout") {
		    this.setAttribute(classAttribute,"tab");
	    }
	}
}
/* Begin tab change functions ^
-------------------------------------------------------------------------*/

/* thumbnail swapping functions :
-------------------------------------------------------------------------*/
var mainImageEl = ".large-image > .primary-image";
var speed = 400;
var opacity = 0.50;

function initProjectPhotos(){
	var thumbImages = $(".thumb").length;
	
	if (thumbImages > 0){//If there is at least one element with a class "thumbnail"		
		$(".thumb:gt(0)").fadeTo(10, opacity).mouseover(function(event){
		  $(this).fadeTo(speed, 1);
		}).mouseout(function(event){
		  $(this).fadeTo(speed, opacity);
		});
		$(".thumb").click(changeProjects);
	}
}

function changeProjects() {
    var clickedImage = $(this).attr("src").split(moduleImgPath)[1];
    var mainImage = $(mainImageEl).attr("src").split(moduleImgPath)[1];
	
    for(var i=0;i<moduleArr.length;i++){ 
        var imageThumb = moduleArr[i].split("::")[0];
        var imageLarge = moduleArr[i].split("::")[1]; 
        var imageCaption = moduleArr[i].split("::")[2];   
          
        if(clickedImage == imageThumb){
            $(this).fadeTo(1000, 1).unbind("mouseout");
			$(mainImageEl).attr("src",moduleImgPath + imageLarge); 
			$(".image-caption:eq(0)").html(imageCaption);
        }
        else{
            $(".thumb:eq("+i+")").unbind("mouseover").unbind("mouseout").fadeTo(10, opacity).mouseover(function(event){
		      $(this).fadeTo(speed, 1);
		    }).mouseout(function(event){
		      $(this).fadeTo(speed, opacity);
		    });
        }
    }	
}
/* thumbnail swapping functions ^
-------------------------------------------------------------------------*/

/* helper functions :
-------------------------------------------------------------------------*/
function toggle(pane){
    if($(pane).css("display") == "block"){
		$(pane).css("display", "none");
	}
	else {
		$(pane).css("display", "block");
	}
}

function position(obj, pane, left, top){
    var xPos = $(obj).position().left + left;
    var yPos = $(obj).position().top - top; 
    
    $(pane).css("left", xPos + "px");
	$(pane).css("top", yPos + "px");
}

/* Extracted from Prototype JS Library, http://prototype.conio.net/
 Written by Sam Stephenson, http://conio.net/ */
function _gel() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string') element = (document.getElementById) ? document.getElementById(element) : eval("document.all."+element) ;
    if (arguments.length == 1) return element;
    elements.push(element);
  }
  return elements;
}

/* Function to grab all elements with a specific class name.
 Original written by Jonathan Snook, http://www.snook.ca/jonathan
 Add-ons by Robert Nyman, http://www.robertnyman.com 	
 Re-written by Zack Gilbert of Seen Creative, http://www.weareseencreative.com */
function getElementsByClass(className, elm, tag){
	if(elm == null) elm = document;
	if(tag == null) tag = '*';
	var elems = elm.getElementsByTagName(tag);
	var returnElems = new Array();
	className = className.replace(/\-/g, "\\-");
	var pattern = new RegExp("(^|\\s)" + className + "(\\s|$)");
	for(var i=0; i<elems.length; i++){
		if(pattern.test(elems[i].className)) returnElems.push(elems[i]);
	}
	return returnElems
}

/* Function to return the class attribute. IE and all other standards compliant browsers have different names for the class attribute object. */
function classAttr(){
	var class_att = null;
	if (browser == IE ){
		class_att = 'className';
	}
	else {
		class_att = 'class';
	}
	
	return class_att;
	
}
/* Functions to add/remove event listeners to objects
	Written by John Resig, http://ejohn.org */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) obj.addEventListener( type, fn, false );
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener) obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
/* Functions to handle cross-browser events.
 Written by Zack Gilbert of Seen Creative, http://www.weareseencreative.com. */
/* get the target of an event object */
function getTarget(e) {
	return (e.target) ? e.target : e.srcElement;
}

/* Function to get object attribute text */
function getObjectAttribute(obj,attr){
	var attrValue = obj.getAttribute(attr);	
	return attrValue;
}

/* Function to get img extension */
function getImgExt(obj){
	var src = obj.getAttribute('src');

	var imgExtPosition = src.lastIndexOf('.') + 1;	//Get the extension. Add 1 to get only the extension, not .extension

	var extension;

	if(src.match('clearpixel.gif')){	//If IE and the image is a PNG, return 'png', because the PNG transparency function causes the img src to be clearpixel.gif.
		extension = 'png';
	}

	else {
		var extension = src.slice(imgExtPosition);
	}

	return extension;
}

/***** Begin clear value functions *****/
// This function can be used to add a click and focus event to any input with a class of "clear-value".
// The function clears the value of the input and removes the click and focus event, so the value doesn't get cleared everytime a user clicks in the input.
function initClearValue(){	
	var input = getElementsByClass("clear-value",null,null);
	
	if (input.length > 0){
		for(var i=0;i<input.length;i++){
			addEvent(input[i],"click",clearValue);
			addEvent(input[i],"focus",clearValue);
		}		
	}
}

function clearValue(){    
    this.value = "";
    removeEvent(this,"click",clearValue);
    removeEvent(this,"focus",clearValue);
 
    //Remove the class that also sets the style for the input before the value is cleared.
    var classValue = this.getAttribute(classAttribute);
    var newClassValue = classValue.replace('clear-value','','gi');
    this.setAttribute(classAttribute, newClassValue);

}
/***** End clear value functions *****/
/* helper functions ^
-------------------------------------------------------------------------*/

/* IE 6 dropdown scripts :
-------------------------------------------------------------------------*/
if(versionInfo.match(IE6) != null){
    mhover = function() {
	    var navUL = _gel('site-menu');
    	
	    if(navUL){		
		    var sfEls = navUL.getElementsByTagName("li");
    		
		    for (var i=0; i<sfEls.length; i++) {
			    if(sfEls[i].getAttribute('className').match("first")) {
				    sfEls[i].onmouseover=function() {
					    this.className+=" mhover-first";
				    }
				    sfEls[i].onmouseout=function() {
					    this.className=this.className.replace(new RegExp(" mhover-first\\b"), "");
				    }
			     }
			    else if(sfEls[i].getAttribute('className').match("last")) {
				    sfEls[i].onmouseover=function() {
					    this.className+=" mhover-last";
				    }
				    sfEls[i].onmouseout=function() {
					    this.className=this.className.replace(new RegExp(" mhover-last\\b"), "");
				    }
			     }
			     else {
				    sfEls[i].onmouseover=function() {
					    this.className+=" mhover";
				    }
				    sfEls[i].onmouseout=function() {
					    this.className=this.className.replace(new RegExp(" mhover\\b"), "");
				    }
			     }
		    }
	    }
    }


    if (window.attachEvent) {
	    window.attachEvent("onload", mhover);
    }
}
/* IE 6 dropdown scripts ^
-------------------------------------------------------------------------*/