// browser platform judgment
var isWinIE;
var isMacIE;
var strUA = navigator.userAgent.toLowerCase();
if (strUA.indexOf("msie") != -1) {
	//ie
	if (strUA.indexOf("mac") != -1) {
		//mac ie
		isMacIE = true;
	} else {
		//win ie
		isWinIE = true;
	}
}

//if(document.layers)document.captureEvents(Event.MOUSEDOWN);

// Return To Top
function backToTop() {
	var x1 = 0;
	var x2 = 0;
	var x3 = 0;
	var y1 = 0;
	var y2 = 0;
	var y3 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	//window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
	window.scrollTo(x, Math.floor(y / 2));
	//if (x > 0 || y > 0) {
	if (y > 0) {
		window.setTimeout("backToTop()", 25);
	}
}

/* Smooth scrolling
   Changes links that link to other parts of this page to scroll
   smoothly to those links rather than jump to them directly, which
   can be a little disorienting.
   
   sil, http://www.kryogenix.org/
   
   v1.0 2003-11-11
   v1.1 2005-06-16 wrap it up in an object
   v1.2 2008-10-06 scroll with easing / interval changed to 20 msec : modified by junk
*/

var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  fixAllLinksAndImages: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
    // Get a list of all images in the page
    var allImages = document.getElementsByTagName('img');
    // Walk through the list
    for (var i=0;i<allImages.length;i++) {
      var img = allImages[i];
      ss.addEvent(img,'contextmenu',ss.cancelRClick);
      //ss.addEvent(img,'mousedown',ss.cancelClickR);
      ss.addEvent(img,'dragstart',ss.cancelDrag);
    }
  },

  cancelRClick: function() {
    return false;
  },

  cancelClickR: function() {
	if (isMacIE && (event.button == 2 || (event.ctrlKey || event.keyCode == 91))) {
		return false;
	}
  },

  cancelDrag: function() {
    return false;
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
    if (!destinationLink) destinationLink = document.getElementById(anchor);

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
    
    //
    ////
    // adjustment for win ie
    if (isWinIE) {
      desty -= 40;
    }
    if (desty<0) {
      desty = 0;
    }
    //
    
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL = setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',20);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    //
    ////window.scrollTo(0,wascypos + scramount);
    // easing by 50%
    window.scrollTo(0,dest - Math.floor((dest - wascypos)/2));
    //
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    /*
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      ////
      ////alert("Handler could not be removed");
      // assign event handlers for modern DOM browsers
      elm["on"+evType] = fn;
      return true;
      ////
    }
    */
      elm["on"+evType] = fn;
      return true;
  }
}

ss.STEPS = 25;

//ss.addEvent(window,"load",ss.fixAllLinks);
ss.addEvent(window,"load",ss.fixAllLinksAndImages);


function popupwin (url) {
	var winW = 565;
	var winH = 600;
	
	if (isMacIE) {
		//mac ie
		winW -= 16;
		winH -= 16;
	} else if (isWinIE) {
		//win ie
		winW += 2;
		winH += 2;
	}
	
	window.open("details/"+url, "", "width="+winW+", height="+winH+", menubar=no, toolbar=no, location=no, status=yes, resizable=yes, scrollbars=yes");
}

function openWin(url, name,W,H) {
window.open(url,name,'toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,location=no,width='+W+',height='+H+'');
}