/******************************************************************************/
/** Variables
/******************************************************************************/
var editFocus           = false;
var currentImageWidth   = 0;
var currentImageHeight  = 0;

/******************************************************************************/
/** Scans all A tags and sets their target attribute to what I want it to be
/******************************************************************************/
function parseLinkTargets(){
   if (!document.getElementsByTagName) return;
   var anchors = document.getElementsByTagName("a");
   for (var i=0; i<anchors.length; i++){
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "internal"){
//         anchor.target = "_self";
      }else{
         anchor.target = "rooksPhotoBlog";
      }
   }
}

/********************************************************************************/
function blurAnchors(){
  if(document.getElementsByTagName){
    var a = document.getElementsByTagName("a");
    for(var i = 0; i < a.length; i++){
      a[i].onfocus = function(){this.blur()};
    }
  }
}

/********************************************************************************/
function preparePage(w, h) {
   currentImageWidth    = w;
   currentImageHeight   = h;
   blurAnchors();
   setupKeyNav();
   doPosition();
}

/********************************************************************************/
function populateCommentsForm(){
   if (document.comments_form != null){
      document.comments_form.email.value = getCookie("mtcmtmail");
      document.comments_form.author.value = getCookie("mtcmtauth");
      document.comments_form.url.value = getCookie("mtcmthome");
   }
}

/********************************************************************************/
function setupKeyNav(){
   if( document.captureEvents ) {
      //non IE
      if( Event.KEYUP ) {
      //NS 4, NS 6+, Mozilla 0.9+
         document.captureEvents( Event.KEYUP );
      }
   }
   // when a key is pressed, run the alertkey function
   document.onkeyup = alertkey;
}

/********************************************************************************/
function setFocus(val){
   editFocus = val;
}

/********************************************************************************/
/** process the key event
/********************************************************************************/
function alertkey(e) {
   if (editFocus == true) return;
   if( !e ) {
      if( window.event ) {
         e = window.event;
      } else {
         return;
      }
   }

   if( typeof( e.which ) == 'number' ) {
      //NS 4, NS 6+, Mozilla 0.9+, Opera
      e = e.which;
   } else if( typeof( e.keyCode ) == 'number'  ) {
      //IE, NS 6+, Mozilla 0.9+
      e = e.keyCode;
   } else if( typeof( e.charCode ) == 'number'  ) {
      //also NS 6+, Mozilla 0.9+
      e = e.charCode;
   } else {
      return;
   }
    
   // left arrow
   if (e==37){
      prev();
   }else if (e==39){
      next();
   }else if (e == 68){
      toggleDetails();
   }else if (e == 72){
      location.href='http://www.rooks.ca';
   }
}


/********************************************************************************/
function doPosition() {
   var element          = document.getElementById('postImage');
   if (element == null) return;
   var images           = element.getElementsByTagName('object');
   var imageWidth       = currentImageWidth;
   var imageHeight      = currentImageHeight;
//   var imageWidth       = images.item(0).width;
//   var imageHeight      = images.item(0).height;

   var imageSidePadding = 30;
   var imageTopPadding  = 26;

   var topCenterMult    = 0.9;
   var sideCenterMult   = 1;

   var w                = getWindowWidth();
   var h                = getWindowHeight();

   var freeSideSpace    = w - imageSidePadding*2;
   var freeTopSpace     = h - imageTopPadding*2;

   var leftPos          = 0;
   var topPos           = 0;

   if (imageWidth >= freeSideSpace){
      leftPos  = imageSidePadding;
   }else{
      leftPos = (w - imageWidth) * sideCenterMult / 2;
   }

   if (imageHeight >= freeTopSpace){
      topPos  = imageTopPadding;
   }else{
      topPos = (h - imageHeight) * topCenterMult / 2;
   }

   document.getElementById('postImage').style.left = leftPos+"px";
   document.getElementById('postImage').style.top  = topPos+"px";
}

/******************************************************************************/
function findPosY(obj)
{
   var curtop = 0;
   if (obj.offsetParent)
   {
      while (obj.offsetParent)
      {
         curtop += obj.offsetTop
         obj = obj.offsetParent;
      }
   }
   else if (obj.y)
      curtop += obj.y;
   return curtop;
}


/******************************************************************************/
function getWindowWidth() {
   var myWidth = 0;
   if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myWidth = window.innerWidth;
   }else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
       myWidth = document.documentElement.clientWidth;
   }else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myWidth = document.body.clientWidth;
   }
   return myWidth;
}

/******************************************************************************/
function getWindowHeight() {
   var myHeight = 0;
   if( typeof( window.innerWidth ) == 'number' ) {
      //Non-IE
      myHeight = window.innerHeight;
   }else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
      //IE 6+ in 'standards compliant mode'
      myHeight = document.documentElement.clientHeight;
   }else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
      //IE 4 compatible
      myHeight = document.body.clientHeight;
   }
   return myHeight;
}

/******************************************************************************/
function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

/******************************************************************************/
function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

/******************************************************************************/
function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

/******************************************************************************/
function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

/******************************************************************************/
function rememberMe (f, HOST) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie('mtcmtauth', f.author.value, now, '/', HOST, '');
    setCookie('mtcmtmail', f.email.value, now, '/', HOST, '');
    setCookie('mtcmthome', f.url.value, now, '/', HOST, '');
}

/******************************************************************************/
function forgetMe (f, HOST) {
    deleteCookie('mtcmtmail', '/', HOST);
    deleteCookie('mtcmthome', '/', HOST);
    deleteCookie('mtcmtauth', '/', HOST);
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

/******************************************************************************/
function validatePost(formObj, HOST){
   var res = checkFields(formObj);

   if (!res){
      return false;
   }
   rememberMe(formObj, HOST);
   return true;
}

/******************************************************************************/
function checkFields(formObj){
   var author = formObj.author;
   var email  = formObj.email;
   var text   = formObj.text;

   if (author.value.length == 0){
   alert('Please Enter Your Name!');
   author.focus();
   return false;
   }
   if (email.value.length == 0){
   alert('Please Enter Your Email Address!');
   email.focus();
   return false;
   }
   if (text.value.length == 0){
   alert('Please Enter A Comment!');
   text.focus();
   return false;
   }

   return true;
}

var fadeDiff = 0; // this should be based on the colours
var curColour = 170;

/******************************************************************************/
function dimNav(){
   fadeDiff = -5;
   doFade();
}

/******************************************************************************/
function showNav(){
   fadeDiff = 5;
   doFade();
}

/******************************************************************************/
function doFade(){
   var light = 170;
   var dark  = 55;

   if (fadeDiff < 0 && curColour <= dark){
      curColour = dark;
   }else if (fadeDiff > 0 && curColour >= light){
      curColour = light;
   }else{
      curColour += fadeDiff;
      setTimeout("doFade()",20);
   }

   var anchors = document.getElementsByTagName("a");
   var spans = document.getElementsByTagName("span");
   for (var i=0; i<anchors.length; i++){
      var anchor = anchors[i];
      anchor.style.color="rgb("+curColour+","+curColour+","+curColour+")";
   }
   for (var i=0; i<spans.length; i++){
      var sp = spans[i];
      sp.style.color="rgb("+curColour+","+curColour+","+curColour+")";
   }
}

var lastMoveTime;
var dimmed;

/******************************************************************************/
function startColourTimer(){
   lastMoveTime = new Date();
   setTimeout('checkToDim()', 500);
}

/******************************************************************************/
/** Repeatedly checks to see if we need to dim the nav. If so, do so.
/******************************************************************************/
function checkToDim(){
   var t = new Date();
   if ( dimmed != "true" && (t.getTime() - lastMoveTime.getTime()) > 10000){
      dimNav();
      dimmed = "true";
   }
   setTimeout('checkToDim()', 500);
}

/******************************************************************************/
function setNavActive(){
   lastMoveTime = new Date();
   dimmed = "false";
   showNav();
}

/******************************************************************************/
/** Used to outsmart the google toolbar
/******************************************************************************/
function restoreStyles(){
   if(event.srcElement.style.backgroundColor != ""){
      event.srcElement.style.backgroundColor = "";
   }
}

/******************************************************************************/
/** Used to outsmart the google toolbar
/******************************************************************************/
function setListeners(){
   inputList = document.getElementsByTagName("INPUT");
   for(i=0;i<inputList.length;i++){
      inputList[i].attachEvent("onpropertychange",restoreStyles);
      inputList[i].style.backgroundColor = "";
   }
   selectList = document.getElementsByTagName("SELECT");
   for(i=0;i<selectList.length;i++){
      selectList[i].attachEvent("onpropertychange",restoreStyles);
      selectList[i].style.backgroundColor = "";
   }
}

/******************************************************************************/
function commentPopup (c) {
   window.open(c, 'rookscomments', 'width=500, height=500, scrollbars=yes, status=yes');
}
window.onresize      = doPosition;