// PanicPage v4.0 script (C) 2005 Panic, Inc.
// Home page functions.
// 
// Please contact us if you need to use this code. Not to be used without express written permission.
//
// 05/11/05 - Updating metrics for new page sizes
//            Adding language checking code
//
// 06/23/05 - Remove help tag code
//            Added live downloading
//
// 10/05/05 - The position of the drop target is not determined dynamically. Hooray.

var isNav4, isIE4, isMac, isNav6;

if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
  isNav4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) != 5) ? true : false
  isNav6 = ((navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 5) || (navigator.userAgent.indexOf("Opera") != -1)) ? true : false
  isIE4  = ((navigator.appName.indexOf("Microsoft") != -1) && (navigator.userAgent.indexOf("Opera") == -1)) ? true : false
  isMac  = (navigator.platform.indexOf("Mac") != -1) ? true : false
}

// Here are the latest download links.
// ClickURL is assigned to the rollover of the element in the HTML page.

function getDownload(clickURL) {
  if (clickURL == "candybar/")   return "http://www.panic.com/candybar/d/CandyBar-Latest.zip";
  if (clickURL == "stattoo/")    return "http://www.panic.com/stattoo/d/Stattoo-Latest.dmg";
  if (clickURL == "transmit/")   return "http://www.panic.com/transmit/d/Transmit-Latest.zip";
  if (clickURL == "desktastic/") return "http://www.panic.com/desktastic/d/Desktastic%203.0.dmg";
  if (clickURL == "unison/")     return "http://www.panic.com/unison/d/Unison-Latest.zip";
  if (clickURL == "coda/")       return "http://www.panic.com/coda/d/Coda-Latest.zip";
}

// ROLLOVER - Layer capable rollover function

function hiLite(imgDocID, imgObjName, imgLayerID) {
  if (imgLayerID) {
     if (isNav4 || isNav6) {
       if (isNav4) {
          document.eval(imgLayerID).document.images[imgDocID].src = eval(imgObjName + ".src");
       }
       else {
          theObj = document.getElementById(imgDocID);
          theObj.setAttribute("src", eval(imgObjName + ".src"));
       }
     }
     else {
       document.all[imgLayerID].document.images[imgDocID].src = eval(imgObjName + ".src");
     }
  }
  else {
    document.images[imgDocID].src = eval(imgObjName + ".src");
  }
} 

var oldX, oldY, movingStatus, layerClicked, what, clickURL, helpUp;
var dropperY, dropperX, dropperWidth, dropperHeight;

function winDeal(what) {

   if (isNav4 || isNav6) {
     if (isNav6) {
       theObj = document.getElementById(what);
       if (theObj.style.visibility == "hidden" || theObj.style.visibility == "") {
         theObj.style.visibility = "visible";
       } else {
         theObj.style.visibility = "hidden";
       }
     }
   }
   else {
     if (document.all[what].style.visibility == "hidden" || document.all[what].style.visibility == "") {
       document.all[what].style.visibility = "visible";
     } else {
       document.all[what].style.visibility = "hidden";
     }
   }
}

// Handle MouseDown

function doDown(e) {
  
  // Calculate click position

  if (isNav4 || isNav6) {
    theTarget = e.target;
    xWin = e.pageX;
    yWin = e.pageY;
  }
  else {
    theTarget = window.event.srcElement;
    xWin = window.event.clientX;
    yWin = window.event.clientY + document.body.scrollTop;    
  }
  
  // Disable everything for Nav 4. It's just too much of a pain in the ass.
  
  if (theTarget.name && theTarget != "") {
    if (theTarget.name.indexOf("-icon") != -1 && !isNav4) {
    
      iconName = theTarget.name.substring(0,theTarget.name.indexOf("-"));
    
	  // Initialize some stuff.
      
      layerClicked = "IconDrag";
      
      if (isNav6) { 
        theLayer = document.getElementById(layerClicked);
        // alert("Layer: " + layerClicked + " Object: " + theLayer + " Left: " + theLayer.style.left);
      }
      
      // Establish the first click coordinate
      
      if (isNav4 || isNav6) {
        xCoord = e.layerX;
        yCoord = e.layerY;
      }
      else {
        if (isMac) {
          xCoord = window.event.offsetX;
          yCoord = window.event.offsetY;
        }
        else {
          // On windows, the Y is not relative to page scroll, so compensate
          xCoord = window.event.offsetX;
          yCoord = window.event.offsetY + document.body.scrollTop;
        }
      }
      
      // Find out what button was pressed -- only drag if it's the left
      
      if (isNav4 || isNav6) {
        theButton = e.which;
      }
      else {
        theButton = event.button;
      }
      
      // If it's the left...
      
      if (theButton == 1) {
      
        // Set the Icon correctly
      
          if (isIE4) {
            if (isMac) {
              if (document.images['DragImage'].src.indexOf("images/drag-"+iconName+".png") == -1) {
                document.images['DragImage'].src = "images/drag-"+iconName+".png";
              }
            }
            else {
              if (document.images['DragImage'].src.indexOf("images/drag-"+iconName+"-old.gif") == -1) {
                document.images['DragImage'].src = "images/drag-"+iconName+"-old.gif";
              }
            }
          }
          if (isNav6) {
            theObj = document.getElementById("DragImage");
            if (theObj.getAttribute("src") != "images/drag-"+iconName+".png") {
              theObj.setAttribute("src", "images/drag-"+iconName+".png");
            }
            
            // Object's style.left can't be read until it is first set.
            theLayer.style.left = xWin - 64;
            theLayer.style.top = yWin - 64;       
          }

	// Find where the DROPPER target is dynamically

	dropperY = 0;
	dropperX = 0;
	dropperFind = document.getElementById("Dropper");
	dropperWidth = document.getElementById("Dropper").offsetWidth;
	dropperHeight = document.getElementById("Dropper").offsetHeight;

      	 do {
           dropperY += dropperFind.offsetTop;
           dropperX += dropperFind.offsetLeft;

           // alert(dropperY + " " + dropperX + " / " + dropperFind.offsetTop + " " + dropperFind.offsetLeft + " " + dropperFind.offsetHeight);

         } while ( dropperFind = dropperFind.offsetParent )

         // alert(dropperY + " " + dropperX + " " + dropperWidth + " " + dropperHeight);
      
         // Start Drag
 
         movingStatus = "drag";
               	 
      	 // Initialize mouse tracking
         
	     if (isNav4 || isNav6) { 
  	       document.captureEvents(Event.MOUSEMOVE);
           if (isNav6) {
             oldX = e.clientX;
             oldY = e.clientY;
             startX = parseInt(theLayer.style.left)
	         startY = parseInt(theLayer.style.top)
	         // alert(oldX + " and " + oldY + " and " + startX + " and " + startY);
           }
         }
	     else {
	       oldX=window.event.offsetX;
	       oldY=window.event.offsetY;
         }
                      
      	// Set the INITIAL drag icon layer to the mouse position
        // Also, get the initial mouse position
      
    	if (isNav4 || isNav6) {
    	  if (isNav4) {
    	     // No code
    	  }
    	  if (isNav6) {
    	     snapStartX = parseInt(theLayer.style.left);
    	     snapStartY = parseInt(theLayer.style.top);
    	  }
    	}
  		else {
    	    if (isMac) {
     		  document.all[layerClicked].style.pixelLeft = window.event.clientX - oldX;
      		  document.all[layerClicked].style.pixelTop  = window.event.clientY - oldY;
      		  
      		  snapStartX = document.all[layerClicked].style.pixelLeft;
      		  snapStartY = document.all[layerClicked].style.pixelTop;
      		  
      		}
      		else {
      		  document.all[layerClicked].style.pixelLeft = window.event.clientX - oldX;
      		  document.all[layerClicked].style.pixelTop  = (window.event.clientY - oldY) + document.body.scrollTop;
      		       		  
      		  snapStartX = document.all[layerClicked].style.pixelLeft;
      		  snapStartY = document.all[layerClicked].style.pixelTop;
      		}
    	}    
         
        dropperHighlight = 0;
        layerShowing = "false";
         
	    document.onmousemove = drag;
	     
        return false;
      }
    }
  }
}

function drag(e) {

  if (movingStatus == "drag") {
    // window.status = "Dragging...";
    
    // First, is the window still hidden? If so, show it.
    
    if (layerShowing == "false") {
       winDeal(layerClicked);
       layerShowing = "true";
    }
     
    // Actually move the layer
    
    if (isNav4 || isNav6) {
      if (isNav6) {
        // Nav6: Track the difference, and add the starting position to it  
        theLayer.style.left = startX + e.clientX - oldX;
        theLayer.style.top = startY + e.clientY - oldY;
        // window.status = "Drag On! " + startX + " / " + startY + " - " + e.pageX + " / " + e.pageY + " - " + oldX + " / " + oldY + " - " + theLayer.style.left + " / " + theLayer.style.top;
      }
    }
    else {
      if (isMac) {
        document.all[layerClicked].style.pixelLeft = window.event.clientX - oldX;
        document.all[layerClicked].style.pixelTop  = window.event.clientY - oldY;
        // window.status = "Drag On: " + window.event.clientX + " / " + window.event.clientY + " - " + oldX + " / " + oldY;
      }
      else {
        // On windows, the Y is fixed not relative to page scroll, so compensate
        document.all[layerClicked].style.pixelLeft = window.event.clientX - oldX;
        document.all[layerClicked].style.pixelTop  = (window.event.clientY - oldY) + document.body.scrollTop;
      }
    }
    
    // Is the mouse over the dropper? If so, highlight it.

    if (isNav6) {
      currentX = e.pageX;
      currentY = e.pageY;
    }
    if (isIE4) {
      currentX = window.event.clientX;
      currentY = window.event.clientY;
    }

    // window.status = "Drag! " + currentX + "/" + currentY + " - " + dropperX + "/" + dropperY + " - " + dropperWidth + "x" + dropperHeight;
     
    if ((currentX > dropperX && currentX < (dropperX + dropperWidth)) && (currentY > dropperY && currentY < (dropperY + dropperHeight))) {
       if (dropperHighlight == 0) {
         if (isIE4) {
           document.images['Dropper'].src = "images/header-2-on.gif";
         }
         if (isNav6) {
           theObj = document.getElementById("Dropper");
           theObj.setAttribute("src", "images/header-2-on.gif");
         }
         dropperHighlight = 1;
       }
    }
    else {
      if (dropperHighlight == 1) {
         dropperHighlight = 0;
         if (isIE4) {
           document.images['Dropper'].src = "images/header-2.gif";
         }
         if (isNav6) {
           theObj = document.getElementById("Dropper");
           theObj.setAttribute("src", "images/header-2.gif");
         }
      }
    }
        
    return false;
  }
}

function doUp(e) {
  if (movingStatus == "drag") {
  
    // Did the user simply click the link? If so, take them where they want to go
  
    if (isNav6) {
       if (oldX == e.clientX && oldY == e.clientY) {
          if (layerShowing == "true") {
            layerShowing = "false";
            winDeal(layerClicked);
          }
          window.location = clickURL;
          movingStatus = "false";
          document.onmousemove = null;
          return false;
       }
    }
    else {
       if (oldX == window.event.offsetX && oldY == window.event.offsetY) {
          if (layerShowing == "true") {
            layerShowing = "false";
            winDeal(layerClicked);
          }
          window.location = clickURL;
          movingStatus = "false";
          document.onmousemove = null;
          return false;
       }
    }
   
    // Drag has ended. Tear down all drag events.
    
    // window.status = "Drag stop.";
    movingStatus = "false";
    document.onmousemove = null;
    if (isNav4) {
      document.releaseEvents(Event.MOUSEMOVE);
    }
    
    // Did the icon reach the drag target?
    // Yes!!
    
    if (dropperHighlight == 1) {

       // Stop highlighting the dropper

       dropperHighlight = 0;
       if (isIE4) {
         document.images['Dropper'].src = "images/header-2.gif";
       }
       if (isNav6) {
         theObj = document.getElementById("Dropper");
         theObj.setAttribute("src", "images/header-2.gif");
       }
       
       winDeal(layerClicked);

       // Do the actual download here!

       fileURL = getDownload(clickURL);
       window.location = fileURL;
    }
    
    // If not, animate "snapping back"
    
    else {    
      if (isNav4 || isNav6) {
      
        if (isNav4) {
  		// No code
        }
        if (isNav6) {
          snapEndX = parseInt(theLayer.style.left);
          snapEndY = parseInt(theLayer.style.top);
        }
  
        snapSteps = 5;
        snapTimer = 10;
      
        snapCurrent = 0;
      
        snapStepX = (snapEndX - snapStartX) * ((1 - 0.9) / (1 - Math.pow(0.9,snapSteps)));
        snapStepY = (snapEndY - snapStartY) * ((1 - 0.9) / (1 - Math.pow(0.9,snapSteps)));
        
        myTimer = setTimeout("snapBack()", snapTimer);
      }
      else {
        snapEndX = document.all[layerClicked].style.pixelLeft;
        snapEndY = document.all[layerClicked].style.pixelTop;
        snapSteps = 12;
        snapTimer = 10;
        snapCurrent = 0;
        snapStepX = (snapEndX - snapStartX) * ((1 - 0.9) / (1 - Math.pow(0.9,snapSteps)));
        snapStepY = (snapEndY - snapStartY) * ((1 - 0.9) / (1 - Math.pow(0.9,snapSteps)));
        myTimer = setTimeout("snapBack()", snapTimer);
      }      
    }
    
    // Return click.
    
    return false;
  }
}

if (isNav4) {
  document.captureEvents(Event.MOUSEUP|Event.MOUSEDOWN);
}

document.onmousedown=doDown;
document.onmouseup=doUp;

// Snap Back Animation

function snapBack() {
   snapCurrent++;
   // window.status = "Snap Back Step #" + snapCurrent + " (move by " + snapStepX + " / " + snapStepY + ")";
   
   snapStepX = snapStepX * 0.9;
   snapStepY = snapStepY * 0.9;
   
   if (isNav4 || isNav6) {
     if (isNav4) {
     }
     if (isNav6) {
       theLayer.style.left = parseInt(theLayer.style.left) - snapStepX;
       theLayer.style.top = parseInt(theLayer.style.top) - snapStepY;
     }
   }
   else {
      document.all[layerClicked].style.pixelLeft = document.all[layerClicked].style.pixelLeft - snapStepX;
      document.all[layerClicked].style.pixelTop = document.all[layerClicked].style.pixelTop - snapStepY;
   }
   
   if (snapCurrent == snapSteps) {
     clearTimeout(myTimer);
     // window.status = "Snap complete.";
     winDeal(layerClicked);
     layerShowing = "false";
   }
   else {
     myTimer = setTimeout("snapBack()", snapTimer);
   }
}

/////////////////////////
//
// Language Tools
//
/////////////////////////

// Send the user -- only once -- to the best possible page for them.

function checkLanguage(lang) {

    // Find the language the user is running
    
    var browserLang = navigator.language ? navigator.language : navigator.userLanguage;

    // See if there's already a set language in a Cookie.

    if (getCookie("Language")) {
    
        // alert("Cookie already set. Do nothing, as we've already checked once.");
           
    } else {
    
        // There's no cookie! So, we need to start from scratch.
        
        if (browserLang.indexOf(lang) == 0) {
    
            // Is the browser language the same as the page language?
            // In that case, we're here already. Set the cookie.
            // alert("Same language, set cookie");

            document.cookie = "Language="+escape(lang)+"; path=/; domain=.panic.com";
            
        } else {
        
            // We're NOT at the right page, so we need to go to the right page.
            // alert("Wrong page. Go to right page.");
            
            if (browserLang.indexOf("ja") == 0) {
                gotoLanguage("ja");
            } else {
                gotoLanguage("en");
            }
        }
    }
}

function gotoLanguage(lang) {
	
    //alert("Go to "+lang);
	
    var ja_page = '/jp/index.html';
    var en_page = '/index.html';

    // alert("Setting cookie to "+lang);
    document.cookie = "Language="+escape(lang)+"; path=/; domain=.panic.com";

    if (lang == "ja") {
        document.location = ja_page;
    } else {
        document.location = en_page;
    }
    
}

// UTIL: Get a single cookie
// Source: http://www.netspade.com/articles/javascript/cookies.xml

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }	
    return unescape(dc.substring(begin + prefix.length, end));
}

