$(document).ready(function(){

  $('#home div.itemContent img:first').css('margin-top', '20px');
  setRollovers();
  setWindows();
  showContent();
  if ($('body.home').length > 0){ introStart(); introStart2(); }

  // Find any .readMore's next to a <p> and move it inside the p (add a space, too)
  $('.itemContent p + .readMore').map(function(){
    $(this).prepend(' ').appendTo($(this).prev('p'));
  });
  
  // Make the signup box in the footer an ajax form.
  $('#newsletterSignup').ajaxForm({
     beforeSubmit:function(){ $("#footerMessage").html('<span style="color: #1b232a;">Sending...</span>'); },
     success:function(rtn){
       if(rtn=="success"){
         $('#newsletterSignup').hide();
         $('#footerMessage').css('position','relative').css('margin',0).html('<span style="color: #1b232a;">Thank you for signing up.</span>');
       } else {footerError();}
     }
  });
  
  $('form#searchForm').submit(function(){
  	if ($('form#searchForm #searchKeywords').val() == ""){
  		$('form#searchForm #searchKeywords').focus();
  		return false;
  	}
  	return true;
  });
  
  $('#commentBlock .postData .name a').attr('target','_blank');
  
  $('div.icon.rss')
    .mouseover(function(){ $(this).css('cursor', 'pointer').addClass('hover'); })
    .mouseout(function(){ $(this).css('cursor', '').removeClass('hover'); })
    .click(function(){ location.href = $(this).find('a').attr('href'); });
  
});

/*

after the html page loads, setRollovers() scans the HTML page for any img tag 
that has the class "rollOver". When it finds one, it attaches the mouseover events.

*/

function setRollovers()
{ imgs = document.getElementsByTagName("img")
  for (i=0;i<imgs.length;i++)
  { if (/rollOver/.test(imgs[i].className))
    {imgs[i].parentNode.onmouseover=function(){roll(this,true);}
      imgs[i].parentNode.onmouseout=function(){roll(this,false);}
      imgs[i].parentNode.onfocus=function(){roll(this,true);}
      imgs[i].parentNode.onblur=function(){roll(this,false);}
    }
  }
}

function setWindows()
{ var anchors = document.getElementsByTagName("a")
  for (var i=0; i<anchors.length; i++)
  { var anchor = anchors[i]
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "newWindow")
    { anchor.target = "_blank";
    }
  }
} 




/*

roll() handles the image rollovers.

*/

function roll(node,status)
{ nodeImg = node.firstChild
  srcImg = nodeImg.getAttribute("src")
  fileName = srcImg.substring(0,srcImg.length - 7)
  navState = srcImg.substring(srcImg.length - 7,srcImg.length - 4)
  fileType = srcImg.substring(srcImg.length - 3,srcImg.length - 0)
  
  if (status)
  { if (navState == "off")
    { fileName = fileName + "ovr." + fileType
      nodeImg.setAttribute("src",fileName)
    }
  }
  else
  { if ((navState != "sel") && (navState != "off"))
    { fileName = fileName + "off." + fileType
      nodeImg.setAttribute("src",fileName)
    }
  }
}




/*

showContent() displays the content and footer areas after page loads.

*/

function showContent()
{ var content = $('#content')
  content.css('visibility','visible');
  if (content.height() > 2450){
    //content.append('<div class="contentPadding" style="height:'+(content.height()-2400)+'px"></div>');
    $('#content .contentPadding').css('height', (content.height()-2400)+'px');
  }
  $('#footer').css('visibility','visible');
}




/*

showName() shows and hides the pop-up namesw in the Who We Are section.

*/

function showName(which,status)
{ if (status)
  { clearNames()
    document.getElementById("label" + which).style.visibility = "visible"
  }
  else
  { return
    //document.getElementById("label" + which).style.visibility = "hidden"
  }
}

function clearNames()
{ numDivs = document.getElementById("whoPhotos").getElementsByTagName("div").length
  for (i=0; i<numDivs; i++)
  { document.getElementById("whoPhotos").getElementsByTagName("div")[i].style.visibility = "hidden"
  }
}




/*

checkFooter() clears and sets the search box in the footer.

*/

function checkFooter(obj,status)
{ if (status)
  { if (obj.value == "Enter your email address")
    {document.getElementById("stayUpdatedObj").setAttribute("value","")}
  }
  else
  { if ((obj.value == "") || (obj.value == null))
    { document.getElementById("stayUpdatedObj").setAttribute("value","Enter your email address")
    }
  }
}




/*

create a pop-up window to show video

*/

function openVideo(fileName,wdth,hght)
{	x = (screen.availWidth - wdth) / 2 
	y = ((screen.availHeight - hght) / 2 ) - 40
	attribs = "width=" + wdth + ",height=" + hght + ",left=" + x + ",top=" + y
	attribs1 = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,"
  popWin = window.open(fileName,"popWindow",attribs)
}




/*

routines for the slide show in the Work section.

*/

var titleText = new Array(20);
var linkText = new Array(20);

var slideTotal
var slideCurrent = 1
var wipeNum
var wiping = false
var barPos

function slideShow()
{ slideTotal = document.getElementById("slides").getElementsByTagName("DIV").length
  scroll(0,0)
  showMask()
  slideShowOpen()
  setCounter()
  setSlide()
  document.getElementById("slideName").innerHTML = titleText[slideCurrent]
  document.getElementById("slideLink").innerHTML = linkText[slideCurrent]
}

function showMask()
{ var pg = xDocSize();
  wdth = pg.w
  hght = pg.h
  document.getElementById("mask").style.width = wdth + "px"
  document.getElementById("mask").style.height = hght + "px"
  document.getElementById("mask").style.display = "block"
}

function slideShowOpen()
{ document.getElementById("slideShow").style.visibility = "visible"
}

function slideShowClose()
{ document.getElementById("mask").style.display = "none"
  document.getElementById("slideShow").style.visibility = "hidden"
}

function setCounter()
{ document.getElementById("slideCount").innerHTML = slideCurrent + "/" + slideTotal
}

function setSlide()
{ document.getElementById("slideCurrent").style.clip = "rect(0px,728px,388px,0px)"
  document.getElementById("slideCurrent").innerHTML = document.getElementById("slides").getElementsByTagName("DIV")[slideCurrent-1].innerHTML
}

function prevSlide()
{ if (wiping)
  {return}
  
  slideCurrent--
  
  barPos = 728
  //document.getElementById("slideBar").style.visibility = "visible"
  
  if (slideCurrent < 1)
  { slideCurrent = slideTotal
  }

  wiping = true
  document.getElementById("slideName").innerHTML = titleText[slideCurrent]
  document.getElementById("slideLink").innerHTML = linkText[slideCurrent]
  document.getElementById("slideNext").innerHTML = document.getElementById("slides").getElementsByTagName("DIV")[slideCurrent-1].innerHTML
  wipeNum = 0
  wipePrev()
  setCounter()
}

function nextSlide()
{ if (wiping)
  {return}
  
  slideCurrent++
  
  barPos = 728
  //document.getElementById("slideBar").style.visibility = "visible"
  
  if (slideCurrent > slideTotal)
  { slideCurrent = 1
  }
  
  wiping = true
  document.getElementById("slideName").innerHTML = titleText[slideCurrent]
  document.getElementById("slideLink").innerHTML = linkText[slideCurrent]
  document.getElementById("slideNext").innerHTML = document.getElementById("slides").getElementsByTagName("DIV")[slideCurrent-1].innerHTML
  wipeNum = 750
  wipeNext()
  setCounter()
}

function wipePrev()
{ document.getElementById("slideCurrent").style.clip = "rect(0px,728px,388px," + wipeNum + "px)"
  moveBar();
  wipeNum = wipeNum + 30
  if (wipeNum <= 760)
  { setTimeout("wipePrev()", 20)
  }
  else
  { setSlide()
    wiping = false
  }
}

function wipeNext()
{ document.getElementById("slideCurrent").style.clip = "rect(0px," + wipeNum + "px,388px,0px)"
  moveBar();
  wipeNum = wipeNum - 30
  if (wipeNum >= -30)
  { setTimeout("wipeNext()", 20)
  }
  else
  { setSlide()
    wiping = false
  }
}

function moveBar()
{ return
  document.getElementById("slideBar").style.marginLeft = (wipeNum + 35) + "px"
}



// xDocSize r1, Copyright 2007 Michael Foster (Cross-Browser.com)
// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

function xDocSize()
{ var b=document.body, e=document.documentElement
  
  if (e)
  { esw = e.scrollWidth
    eow = e.offsetWidth
    esh = e.scrollHeight
    eoh = e.offsetHeight
  }
  if (b)
  { bsw = b.scrollWidth
    bow = b.offsetWidth
    bsh = b.scrollHeight
    boh = b.offsetHeight
  }
  return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)}
}








/*

email validation routines for the footer and comment forms

*/

function echeck(str)
{ var at = "@"
  var dot = "."
  var lat = str.indexOf(at)
  var lstr = str.length
  var ldot = str.indexOf(dot)
  
  if (str.indexOf(at)==-1)
  { return false
  }
  
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
  { return false
  }
  
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
  { return false
  }
  
  if (str.indexOf(at,(lat+1))!=-1)
  { return false
  }
  
  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
  { return false
  }
  
  if (str.indexOf(dot,(lat+2))==-1)
  { return false
  }
  
  if (str.indexOf(" ")!=-1)
  { return false
  }
  
  return true					
}

function validateFooter()
{  var emailFld = $('#stayUpdatedObj');
   var email = emailFld.val();

  if ((email == null) || (email == "")){
    emailFld.focus();
    footerError();
  } else if (echeck(email) == false){
    emailFld.focus()
    footerError();
  } else {$('#newsletterSignup').submit();}
}

function footerError()
{ $('#footerMessage').html('<span style="color: #a61212;">Please enter a valid email address.</span>');
}

function validateUnsubscribe()
{  var emailFld = $('#unsubscribeEmail');
   var email = emailFld.val();
  if ((email == null) || (email == "")){
    emailFld.focus();
   $('#message').html("<div style='color:#c90202;'>Please type in your email address.</div>");
  } else if (echeck(email) == false){
    emailFld.focus()
   $('#message').html("<div style='color:#c90202;'>Please type in a valid email address.</div>");
  } else {$('#unsubscribeForm').submit();}
}


function validateComment(){
  var nameElem = $('#commentName');
  var emailElem = $('#commentEmail');
  var commentElem = $('#commentBox');
  var errorMessage = ""
  
  if ((nameElem.val() == "") || (nameElem.val() == null)) { 
    nameElem.focus();
    errorMessage += "&bull; Please enter your name.<br />";
  }
  
  if ((emailElem.val() == null) || (emailElem.val() == "")){ 
    emailElem.focus();
    errorMessage += "&bull; Please enter a valid email address.<br />";
  }
  else if (echeck(emailElem.val()) == false){ 
    emailElem.focus();
    errorMessage += "&bull; Please enter a valid email address.<br />";
  }
  
  if (commentElem.val() == ""){
    commentElem.focus();
    errorMessage += "&bull; Please enter your comment.<br />";
  }
  
  if (errorMessage != ""){ 
    $('#commentMessage').html("<div style='color: #c90202;'>" + errorMessage + "</div>");
  }
  else
  {
    $('#comment_form').submit();
    $('#commentMessage').html("<span style='color: #1b232a;'>Thank you for submitting your comment.</span>");
  }
}




/*

Home Page Animation

*/


var introOpac = new Array(100,0,0,0,0,0)
var introCurr = 0


function introStart()
{ setInterval("introAnimate()",50)
  anim1 = setInterval("introRotate()",2500)
}

function introAnimate()
{ for (i=0; i<6; i++)
  { if (introOpac[i] != 0)
    { document.getElementById("animImg" + i).style.display = "block"
    }
    else
    { document.getElementById("animImg" + i).style.display = "none"
    }
    
  
    if ((i == introCurr) && (introOpac[i] != 100))
    { introOpac[i] = introOpac[i] + 10
    }
    else
    if ((i != introCurr) && (introOpac[i] != 0))
    { introOpac[i] = introOpac[i] - 10
    }
    
    obj = document.getElementById("animImg" + i)
    setOpacity(obj,introOpac[i])
  }
}

function introRotate()
{ introCurr++
  if (introCurr == 6)
  { introCurr = 0
  }
}

function setOpacity(obj,num)
{ obj.style.MozOpacity = (num / 100)
  obj.style.opacity = (num / 100)
  obj.style.filter = "alpha(opacity=" + num + ")"
}




/* SHARE THIS */
function share_this_show(id, orientation, url, title, bodytext)
{

	var form = $('#share_this_form');
	var link = $('#share_this_link_' + id);
	
	form.css('left', link.position().left + 'px');
	form.toggle();

	if (orientation == "top") {
	   form.css('top', (link.position().top - form.height() - 10) + 'px');
	}
	else if (orientation == "bottom") {
	   form.css('top', (link.position().top + link.height() + 3) + 'px');
	}	
	
	if ($("#share_this_delicious"))     {$("#share_this_delicious").attr('href', "http://del.icio.us/post?url=" + url + "&title=" + title + "&notes=" + bodytext);}
	if ($("#share_this_digg"))          {$("#share_this_digg").attr('href', "http://digg.com/submit?phase=2&url=" + url + "&title=" + title + "&bodytext=" + bodytext);}
	if ($("#share_this_furl"))          {$("#share_this_furl").attr('href', "http://furl.net/storeIt.jsp?u=" + url + "&t=" + title);}
	if ($("#share_this_netscape"))      {$("#share_this_netscape").attr('href', "http://www.netscape.com/submit/?U=" + url + "&T=" + title);}
	if ($("#share_this_yahoo_myweb"))   {$("#share_this_yahoo_myweb").attr('href', "http://myweb2.search.yahoo.com/myresults/bookmarklet?u=" + url + "&t=" + title);}
	if ($("#share_this_technorati"))    {$("#share_this_technorati").attr('href', "http://www.technorati.com/faves?add=" + url + "");}
	if ($("#share_this_google_bmarks")) {$("#share_this_google_bmarks").attr('href', "  http://www.google.com/bookmarks/mark?op=edit&bkmk=" + url + "&title=" + title + "&annotation=" + bodytext);}
	if ($("#share_this_newsvine"))      {$("#share_this_newsvine").attr('href', "http://www.newsvine.com/_wine/save?u=" + url + "&h=" + title);}
	if ($("#share_this_blinklist"))     {$("#share_this_blinklist").attr('href', "http://blinklist.com/index.php?Action=Blink/addblink.php&Url=" + url + "&Title=" + title);}
	if ($("#share_this_reddit"))        {$("#share_this_reddit").attr('href', "http://reddit.com/submit?url=" + url + "&title=" + title);}
	if ($("#share_this_blogmarks"))     {$("#share_this_blogmarks").attr('href', "http://blogmarks.net/my/new.php?mini=1&url=" + url + "&title=" + title);}
	if ($("#share_this_magnolia"))      {$("#share_this_magnolia").attr('href', "http://ma.gnolia.com/bookmarklet/add?url=" + url + "&title=" + title);}
	if ($("#share_this_windows_live"))  {$("#share_this_windows_live").attr('href', "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=" + url + "&title=" + title + "&top=1");}
	if ($("#share_this_tailrank"))      {$("#share_this_tailrank").attr('href', "http://tailrank.com/share/?link_href=" + url + "&title=" + title);}
	if ($("#share_this_mrwong"))        {$("#share_this_mrwong").attr('href', "http://www.mister-wong.de/index.php?action=addurl&bm_url=" + url + "&bm_description=" + title);}
	if ($("#share_this_stumbleupon"))   {$("#share_this_stumbleupon").attr('href', "http://www.stumbleupon.com/submit?url=" + url + "&title=" + title);}
}



function share_this_tab(tab) 
{
	var tab1 = document.getElementById('share_this_tab1');
	var tab2 = document.getElementById('share_this_tab2');
	var body1 = document.getElementById('share_this_social');
	var body2 = document.getElementById('share_this_email');
	
	switch (tab) {
		case '1':
			tab2.className = '';
			tab1.className = 'selected';
			body2.style.display = 'none';
			body1.style.display = 'block';
			break;
		case '2':
			tab1.className = '';
			tab2.className = 'selected';
			body1.style.display = 'none';
			body2.style.display = 'block';
			break;
	}
} 


