/* 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)
 */

var GB_DONE      = false;
var GB_HEIGHT    = 600;
var GB_WIDTH     = 600;
var GB_ANIMATION = true;

function GB_show(caption, url, height, width ) {
  GB_HEIGHT = height || 400;
  GB_WIDTH = width || 400;
  if(!GB_DONE) {
    jQuery(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<span class='closeWindow'>Sluit venster</span></div>");
    jQuery("#GB_window span").click(GB_hide);
    jQuery("#GB_overlay").click(GB_hide);
    jQuery(window).resize(GB_position);
    GB_DONE = true;
  }

  jQuery("#GB_frame").remove();
  jQuery("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");
  jQuery("#GB_caption").html(caption);
  jQuery("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    jQuery("#GB_window").slideDown("slow");
  else
    jQuery("#GB_window").show();
	
  return false;
}

function GB_hide() {
  jQuery("#GB_window,#GB_overlay").hide();
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  jQuery("#GB_window").css(
  {
	  width:GB_WIDTH+"px",
	  height:GB_HEIGHT+"px",
      left: ((w - GB_WIDTH)/2)+"px",
	  top:"50%",
	  position:"fixed",
	  marginTop:((GB_HEIGHT/2)*-1)+"px" });
  	jQuery("#GB_frame").css("height",GB_HEIGHT - 32 +"px");
}

function GB_init( settings ) {
	jQuery("a.greybox").bind( "click", function( event ) {
		event.preventDefault();
		
		var t = this.title || this.innerHTML || this.href;
		GB_show(t,this.href, settings.height, settings.width );
	} );
}

function GB_alert( settings ) {
	//check if cookie has been set; if so, don't bother mr/mrs user :)
	if( ( settings.use_cookie == 'true' ) && ( GB_readCookie( "surveyalert" ) == "doNotShow" ) ) return;
	//check if time is expired
	if( settings.time_offset <= 0 ) return;
	
	
	//alert has not been shown so far. Show it when timeOffset expires
	setTimeout( function() { GB_showAlertWindow( settings ) }, ( settings.time_offset*10000 ) );
}

function GB_showAlertWindow( settings ) {
	//create html for window
	jQuery( "body" ).append( "<div id=\"GB_overlay\"></div><div id=\"surveyAlert\"><div id=\"surveyAlertHead\"><a href=\"#\" id=\"sa_close\">sluit venster</a></div><div id=\"surveyAlertContent\">" + settings.info + "</div><p id=\"surveyAlertFooter\"><a href=\"" + settings.url + "\" class=\"greybox\">start</a></p></div>");
	jQuery( "#sa_close, #surveyAlertFooter a, #GB_overlay" ).bind( "click", function( event ){ event.preventDefault(); jQuery("#surveyAlert, #GB_overlay").detach(); } );
	GB_init( settings );
	
	if( settings.use_cookie == 'true' )
		GB_createCookie( "surveyalert", "doNotShow", settings.cookie_lifetime );
}

function GB_createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function GB_readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
