var loaded = 0;
var portfolioWidth = 0;

function firstTime() {
        $("#overlay1").fadeIn(100, function(){
          $("#overlay2").fadeIn(800, function(){
            $("#overlay3").fadeIn(500, function(){
              $("#overlay4").fadeIn(300, function(){
                $("#overlay6").fadeIn(200, function(){
                  $("#overlay7").fadeIn(300, function(){
                    $("#overlay8").fadeIn(200, function(){
                      $("#overlay5").fadeIn(800, function(){
                        $("#leftnav").slideToggle(300);
                        $("#rightnav").slideToggle(300);
                        $("#centernav").slideToggle(300);
					  });
                    });
                  });
                });
              });
            });
          });
        }); 
}
function onReady(page) {
  if (page == 'index.php') {
	$("#main").fadeIn(400, function(){
	  rotateInnerHTML('#keywords', 'keywords', 1500);
	});				
  }
  else if (page == 'profile.php'){
    $("#main").fadeIn(500, function(){ 
      $("#thumb2").fadeTo(100, .4, function(){}); 
      $("#thumb3").fadeTo(100, .4, function(){}); 
      $("#thumb4").fadeTo(100, .4, function(){}); 
      $("#thumb5").fadeTo(100, .4, function(){}); 
    });
  }
  else if (page == 'services.php'){
    $("#main").fadeIn(500, function(){ 
      $("#thumb2").fadeTo(100, .4, function(){}); 
      $("#thumb3").fadeTo(100, .4, function(){}); 
      $("#thumb4").fadeTo(100, .4, function(){});
      $("#thumb5").fadeTo(100, .4, function(){});
    });
  }
  else if (page == 'clients.php'){
    
		    $("#main").fadeIn(500, function(){
            });
  }
  else if (page == 'portfolio.php'){
    $("#main").fadeIn(500, function() {});
  }
  else if (page == 'contact.php'){
    $("#main").fadeIn(500, function() {});
  }
}
function changePage(page) {
  
    $("#main").fadeOut(500, function(){
      window.location = page;
	});
  
}

function cycleImage(id) {
    $(id).cycle({});
}
function buttonHoverOn(element) {
  $(element).animate({
  color: "#333"
  }, 300 );
}

function buttonHoverOff(element) {
  $(element).animate({
  color: "#fff"
  }, 300 );
}

function findAndReplace(searchText, replacement, searchNode) {
    if (!searchText || typeof replacement === 'undefined') {
        // Throw error here if you want...
        return;
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'g') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html,head,style,title,link,meta,script,object,iframe';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function(){
                var html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML = html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}

function rotateInnerHTML(parentID, elementID, speed) {
  element = document.getElementById(elementID);
  $(parentID).fadeOut(speed, function(){
    element.innerHTML = "<span class='firstletter'>S</span><span class='restofword'>URPASSES</span>";
    $(parentID).fadeIn(speed, function(){
      $(parentID).fadeOut(speed, function(){
        element.innerHTML = "<span class='firstletter'>C</span><span class='restofword'>REATES</span>";
        $(parentID).fadeIn(speed, function(){
          $(parentID).fadeOut(speed, function(){
            element.innerHTML = "<span class='firstletter'>I</span><span class='restofword'>NITIATES</span>";
            $(parentID).fadeIn(speed, function(){
              $(parentID).fadeOut(speed, function(){
                element.innerHTML = "<span class='firstletter'>A</span><span class='restofword'>MAZES</span>";
                $(parentID).fadeIn(speed, function(){
                  $(parentID).fadeOut(speed, function(){
                    element.innerHTML = "<span class='firstletter'>D</span><span class='restofword'>ESIGNS</span>";
                    $(parentID).fadeIn(speed, function(){
                      $(parentID).fadeOut(speed, function(){
                        element.innerHTML = "<span class='firstletter'>E</span><span class='restofword'>XCEEDS</span>";
                        $(parentID).fadeIn(speed, function(){
						  $(parentID).fadeOut(speed, function(){
						    element.innerHTML = "<span class='firstletter'>I</span><span class='restofword'>NSPIRES</span>";
                            $(parentID).fadeIn(speed, function(){
							  rotateInnerHTML(parentID, elementID, speed);
							});
						  });
						});
					  });
					});
				  });
                });
              });
            });
          });
        });
      });
    });	  
  });
}
function changeThumb(thumb) {
  x = 1;
  while (document.getElementById('thumb'+x)) {
	if (!hasClass(document.getElementById('thumb'+x),'active_img')){$('#thumb'+x).fadeTo(50, .4, function(){});} 
	x++;
  }
  if (thumb) {$("#"+thumb).fadeTo(50, 1, function(){});}
}
function changeMain(num) {
  x = 1;
  while (document.getElementById('thumb'+x)) {
    if (hasClass(document.getElementById('thumb'+x),'active_img')){removeClass(document.getElementById('thumb'+x),'active_img');}
	if (element = document.getElementById('spotlight_'+x)) {element.style.display = 'none';}
	$('#thumb'+x).fadeTo(200, .4, function(){});
	x++;
  }
  if (num) {
    $("#thumb"+num).fadeTo(50, 1, function(){});
    addClass(document.getElementById('thumb'+num),'active_img');
	$("#spotlight_"+num).fadeIn(100, function(){});
  }
}


function hasClass(ele,cls) {return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));}
function addClass(ele,cls) {if (!this.hasClass(ele,cls)) ele.className += " "+cls;}
function removeClass(ele,cls) {if (hasClass(ele,cls)) {var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');ele.className=ele.className.replace(reg,' ');}}
