/* 
 * Internet Blackout v0.4
 * By John Ferlito http://www.internetblackout.com.au/
 * Copyright (c) 2009 John Ferlito
 *
 * Lots of code borrowed from thickbox
 */

/*
 * Changelog
 *
 * 0.3
 * - Fix typo in jqueryReady - Thanks ycros
 *
 * 0.4
 * - Closing quote missing on iframe name attribute
 * - Fix cookie path
 *
 * 0.5
 * - Fix on pages without jquery
 *
 * 0.6
 * - Fix on page with jQuery again
 * 
 * 0.7
 * - Popup will now be 620px wide
 *
 */

urlBase = 'http://www.internetblackout.com.au/ib/';
imgUrl = urlBase + 'close.png';
iframe = 'http://www.internetblackout.com.au/iframe/';

jquerySrc = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
cookieSrc = urlBase + "jquery.cookie.js";
cssSrc = urlBase + "/blackout.css";
var $ibj;

if (typeof jQuery == "undefined") {
    loadJQuery(jquerySrc);
}
else {
    $ibj = jQuery;
   loadJQueryCookie();
}

function black_it_out() {
    $ibj(document).ready(function($){

	var cookie = $ibj.cookie('ibj_shown');
	if (cookie != 'true') {
		$ibj.cookie('ibj_shown', 'true', { path: '/'});
		// Load CSS
		var link = document.createElement("link");
		link.setAttribute("rel", "stylesheet");
		link.setAttribute("type", "text/css");
		link.setAttribute("href", cssSrc);
		if (typeof link != "undefined") {
		    document.getElementsByTagName("head")[0].appendChild(link);
		}


		$ibj("body").append("<div id='IB_overlay'></div><div id='IB_window'></div>");
		$ibj("#IB_window").append("<iframe frameborder='0' scrolling='no' hspace='0' src='"+iframe+"' id='IB_iframeContent' name='IB_iframeContent'"+Math.round(Math.random()*1000)+"> </iframe>");
		$ibj("#IB_overlay").click(IB_remove);

		if(tb_detectMacXFF()) {
		    $ibj("#IB_overlay").addClass("IB_overlayMacFFBGHack");//use png overlay so hide flash
		}
		else {
		    $ibj("#IB_overlay").addClass("IB_overlayBG");//use background and opacity
		}

		$ibj("#IB_window").prepend("<div id='IB_closeWindow'><a href='#' id='IB_closeWindowButton' title='Close'>Close</a></div>");
		$ibj("#IB_closeWindowButton").click(IB_remove);

		document.onkeydown = function(e) {
		    if (e == null) { // ie
			keycode = event.keyCode;
		    } else { // mozilla
			keycode = e.which;
		    }
		    if(keycode == 27){ // close
			IB_remove();
		    }
		};
	}

    });

    function IB_remove() {
        $ibj("#IB_closeWindowButton").unbind("click");
        $ibj("#IB_window").fadeOut("fast", function() {
            $ibj('#IB_window,#IB_overlay').trigger("unload").unbind().remove();
        });
        document.onkeydown = "";

        return false;
    }

    function tb_detectMacXFF() {
        var userAgent = navigator.userAgent.toLowerCase();
        if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
            return true;
        }
    }

}


function loadJQuery(jquerySrc) {
  // First inject the script into the DOM
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript");
  script.setAttribute("src", jquerySrc);
  if (typeof script != "undefined") {
      document.getElementsByTagName("head")[0].appendChild(script);
  }

  jqueryReady(0);
}


function jqueryReady(time_elapsed) {
  if (typeof jQuery == "undefined") {
    if (time_elapsed <= 5000) {
      setTimeout("jqueryReady(" + (time_elapsed + 200) + ")", 200); // set a timer to check again in 200 ms.
    } else {
      alert("Timed out while loading jQuery.")
    }
  } else {
    $ibj = jQuery.noConflict();

    loadJQueryCookie();
  }
}


function loadJQueryCookie() {
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript");
  script.setAttribute("src", cookieSrc);
  if (typeof script != "undefined") {
    document.getElementsByTagName("head")[0].appendChild(script);
  }

  jqueryCookieReady(0);
}

function jqueryCookieReady(time_elapsed) {
  if (typeof $ibj.cookie == "undefined") {
    if (time_elapsed <= 5000) {
      setTimeout("jqueryCookieReady(" + (time_elapsed + 200) + ")", 200); // set a timer to check again in 200 ms.
    } else {
      alert("Timed out while loading jQueryCookie.")
    }
  } else {
    black_it_out();
  }
}


