/* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 *
 * Garmin - Vincent
 * 2008/02/21 add thickbox function loading overlay, cancel click stop load url or write 2 pnd
 * 2008/03/04 fix FireFox iframe onload item, GB_window in FF display:block, positon out screen, IE display:none
 * 2008/05/19 merge show/hide overflow Fn, fix positon form Drupal dsoctop, dsocleft
 */


/* by vincent for GOC ---------------------------------------------------------*/

var GB_pathToImage = "/connect/images/indicator_circle_thickbox.gif";

//on page load call tb_init
//$(document).ready(function(){
	imgLoader = new Image();// preload image
	imgLoader.src = GB_pathToImage;
//});

var GB_DONE = false;
var GB_hide_now = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var GB_btn_close = false; //show close button ?
var GB_btn_close_action = ''; //close btn onclick do what ?
var GB_click_overlay = false; //click overlay then hide
var GB_html_overflow = false; //when overlay , html scrollbar hide
var GB_btn_txt = "<img src='/connect/images/share2/close_icon.gif' alt='關閉視窗' />";
var GB_act = '';

function GB_show(caption, url, height, width, act, dl_) {
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  GB_btn_ = (GB_btn_close) ? GB_btn_txt : '';
  GB_act = act;

  var url_ = url;


var firefox     = $.browser.mozilla ;
if (firefox && dl_)
{
var top = (screen.height - (GB_HEIGHT + 110)) / 2;
var left = (screen.width - GB_WIDTH) / 2;

window.open(url_,"PopWin","toolbar=no,directries=no,scrollBars=no,status=no,left="+left+",top="+top+",height="+GB_HEIGHT+",width="+GB_WIDTH+"");
}

else
{
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>" +  GB_btn_ + "</div>");
    $("#GB_window img").click(function(act_){ GB_hide(); if (GB_act != undefined) { eval(GB_act); }
	});
	if (GB_click_overlay) { $("#GB_overlay").click(GB_hide); }
    $(window).resize(GB_position);
    GB_DONE = true;
  }

    if (GB_html_overflow) { $("html").css("overflow", "hidden"); }

  $("body").append("<div id='GB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
  $('#GB_load').show();
  $("#GB_frame").remove();
  $("#GB_window").append("<iframe id='GB_frame' src='"+url_+"' onload='GB_showIframe();' frameborder='0' marginheight='0' marginwidth='0'></iframe>");
  $("#GB_caption").html(caption);
  $("#GB_overlay").show();

  GB_hide_now = false;
  
  // fix IE select ovelap bug 
  if ($.browser.msie) { $("select").hide(); }
}

}


function GB_hide() {
  $("#GB_overlay, #GB_load").hide();
  $("html").css("overflow","");

  GB_hide_now = true;

   // fix IE select ovelap bug 
  if ($.browser.msie) {
  $("select").show();
  $("#GB_window").hide();
  document.execCommand("Stop");
  } else {
  $("#GB_window").css({"top":5000+"px", "left":5000+"px"}); // 4 some reason, in FF need to do this..
  window.stop();
  //iframe.attr('src', 'about:blank'); // Used to stop the loading in FF
  }
}

function GB_position() {
if (!GB_hide_now)
{
var de = document.documentElement;
var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
var dsoctop=document.all? iebody.scrollTop : pageYOffset;

var height = h < GB_HEIGHT ? h - 32 : GB_HEIGHT;
var top = (h - height)/2 + dsoctop;


if(GB_ANIMATION) {
	$("#GB_window").animate({ width: GB_WIDTH +"px", height: height +"px", left: ((w - GB_WIDTH)/2)+"px", top: top+"px" }, "fast","linear", function(){} );
	$("#GB_frame").css({ width: GB_WIDTH +"px", height: height +"px", left: ((w - GB_WIDTH)/2)+"px", top: top+"px" });
	} else {
	$("#GB_window, #GB_frame").css({ width: GB_WIDTH +"px", height: height +"px", left: ((w - GB_WIDTH)/2)+"px", top: top+"px" });
}


$("#GB_frame").css("height",height - 32 +"px");
$("#GB_overlay").css({height:h, top:dsoctop + "px", width:w});

}


}

/* by vincent for GOC 2008/02/21, 2008/03/04 fix 4 FF--------------------------------------*/
function GB_showIframe(){
$("#GB_load").remove();
GB_position();
//if(GB_ANIMATION) {$("#GB_window").slideDown('slow'); } else { $("#GB_window").show(); }
}

