//*********************************************
// Scrollable Shortcut popup
// By Nguyen Thanh Duong, Ikkosha Osaka, Japan
// duong@ikkosha.co.jp
// Date: 2008.10.12
//*********************************************

var shortcutLength = 100;			// Size of iframe 
var shortcutWidth = 100;
var bodyWidth = 1000;				// Width of main page

var scTop = 165;					    // Position of shortcut (from top)
var scLeft = bodyWidth;				// Adjust when resize

var outSize = 24;					// Inactive shortcut width 
var inSize = shortcutWidth + 34;	// Active shortcut width 

var myWidth = 0, myHeight = 0;		// Window size
var pShowed = false;				// Shortcut active ?
var scrollTimeOut;

function sc_getWinSize() {
  if( typeof( window.innerWidth ) == 'number' ) {
    // Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
	if (document.width != window.innerWidth)
		myWidth = window.innerWidth - 16;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    // IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    // IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  scLeft = (myWidth < bodyWidth) ? bodyWidth : bodyWidth + (myWidth - bodyWidth) / 2;
}

function createPopup() {
	if (shortcutWidth < 60) {
		shortcutWidth = 60;
		inSize = shortcutWidth + 34;
	}
	if (shortcutLength < 43) {
		shortcutLength = 43;
	}

	$("#spopup").html(
	  "<table cellspacing=\"0\" cellpadding=\"0\">\n" +
	  "  <tr>\n" +
	  "    <td width=\"34\">\n" +
	  "    <table cellspacing=\"0\" cellpadding=\"0\">\n" +
	  "      <tr>\n" +
	  "	       <td><img id=\"close\" name=\"close\" src=\"http://gmba.doshisha.ac.jp/img/shortcut/close.gif\" width=\"34\" height=\"81\" border=\"0\"></td>\n" +
	  "      </tr>\n" +
	  "      <tr>\n" +
	  "        <td height=\"" + (shortcutLength - 42) + "\" background=\"http://gmba.doshisha.ac.jp/img/shortcut/left.gif\"></td>\n" +
	  "      </tr>\n" +
	  "      <tr>\n" +
	  "	       <td><img src=\"http://gmba.doshisha.ac.jp/img/shortcut/bottomleft.gif\" width=\"34\" height=\"10\" border=\"0\"></td>\n" +
	  "      </tr>" +
	  "    </table></td>\n" +
	  "    <td width=\"" + shortcutWidth + "\"><table cellspacing=\"0\" cellpadding=\"0\">\n" +
	  "    <tr>\n" +
	  "      <td width=\"" + shortcutWidth + "\" height=\"39\" background=\"http://gmba.doshisha.ac.jp/img/shortcut/topbg.gif\" align=\"left\">\n" +
      "      <img src=\"http://gmba.doshisha.ac.jp/img/shortcut/top.gif\" height=\"39\" border=\"0\"></td>\n" +
	  "    </tr>\n" +
	  "    <tr>\n" +
	  "	     <td width=\"" + shortcutWidth + "\" height=\"" + shortcutLength + "\">\n" +
	  "       <iframe width=\"" + shortcutWidth + "\" height=\"" + shortcutLength + "\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"no\"  scrolling=\"no\" src=\"http://gmba.doshisha.ac.jp/shortcut.html\"></iframe></td>\n" +
	  "    </tr>\n" +
	  "    <tr>\n" +
	  "      <td width=\"" + shortcutWidth + "\" height=\"10\" background=\"http://gmba.doshisha.ac.jp/img/shortcut/bottom.gif\"></td>\n" +
	  "    </tr>\n" +
	  "    </table></td>\n" +
	  "  </tr>\n" +
	  "</table>\n");
 }

function shortcutOut() {
	$("#spopup").html("<img id=\"shortcut\" name=\"shortcut\" src=\"http://gmba.doshisha.ac.jp/img/shortcut/shortcut.gif\" width=\"24\" height=\"81\" border=\"0\">");

	$("#shortcut").click(function () {
		createPopup();
		pShowed = true;
		$("#spopup").animate({left: scLeft - inSize + "px", width: inSize + "px"}, 300, function() { shortcutIn(); });
	}).mouseout(function () {
		$(this).attr("src", "http://gmba.doshisha.ac.jp/img/shortcut/shortcut.gif");
	}).mouseover(function () {
		$(this).attr("src", "http://gmba.doshisha.ac.jp/img/shortcut/shortcutb.gif");
	});
}

function shortcutIn() {
	$("#close").click(function () {
		pShowed = false;
		$("#spopup").animate({left: scLeft - outSize + "px", width: outSize + "px"}, 300, function() { shortcutOut(); });
	}).mouseout(function () {
		$(this).attr("src", "http://gmba.doshisha.ac.jp/img/shortcut/close.gif");
	}).mouseover(function () {
		$(this).attr("src", "http://gmba.doshisha.ac.jp/img/shortcut/closeb.gif");
	});
}

function init() {
	sc_getWinSize();
	$('body').append("<div id=\"spopup\" style=\"display:none; overflow:none; opacity:0.9; filter:alpha(opacity=90); position:absolute\">Shortcut</div>");

	$("#spopup").html("<img id=\"shortcut\" name=\"shortcut\" src=\"http://gmba.doshisha.ac.jp/img/shortcut/shortcut.gif\" width=\"24\" height=\"81\" border=\"0\">");
	$("#spopup").css("top", scTop + "px");
	$("#spopup").css("left", scLeft - outSize + "px");
	$("#spopup").css("width", "24px");
	$("#spopup").css("display", "block");
	$("#spopup").css("z-index", 1001);
}

function scrollSc() {
	clearTimeout(scrollTimeOut);
	scrollTimeOut = setTimeout("doScroll()", 300)
}

function doScroll() {
	var scrTop;

	if (typeof(window.pageXOffset) == 'number') 
	{
		 // Netscape compliant
		 // scrollX = window.pageXOffset;
		 scrTop = window.pageYOffset;
	} 
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) 
	{
		// DOM compliant
		// scrollX = document.body.scrollLeft;
		scrTop = document.body.scrollTop;
	} 
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) 
	{
		 // IE6 standards compliant mode
		 // scrollX = document.documentElement.scrollLeft;
		 scrTop = document.documentElement.scrollTop;
	}

	if (!scrTop)
		scrTop = 0;

	var scrTo;

	if (scrTop < scTop)
		scrTo = scTop;
	else
		scrTo = scrTop + 15;

	if (scrTo != $("#spopup").css("top")) {

		$('#spopup').animate({top: scrTo + 'px'}, 1000);
	}
}

$(document).ready(function(){

	init();

	$("#shortcut").click(function () {
		createPopup();
		pShowed = true;
		$("#spopup").animate({left: scLeft - inSize + "px", width: inSize + "px"}, 300, function() { shortcutIn(); });
	}).mouseout(function () {
		$(this).attr("src", "http://gmba.doshisha.ac.jp/img/shortcut/shortcut.gif");
	}).mouseover(function () {
		$(this).attr("src", "http://gmba.doshisha.ac.jp/img/shortcut/shortcutb.gif");
	});

	$(window).scroll(function () {
		scrollSc();
	}).resize(function () {
		sc_getWinSize();
		if (pShowed)
			$("#spopup").css("left", scLeft - inSize + "px");
		else
			$("#spopup").css("left", scLeft - outSize + "px");

		scrollSc();
	});
});
