// External stuff

var DomLoaded =
{
  onload: [],
  loaded: function()
  {
    if (arguments.callee.done) return;
    arguments.callee.done = true;
    for (i = 0;i < DomLoaded.onload.length;i++) DomLoaded.onload[i]();
  },
  load: function(fireThis)
  {
    this.onload.push(fireThis);

    if (arguments.callee.initialized) return;
    arguments.callee.initialized = true;

    if (document.addEventListener) 
      document.addEventListener("DOMContentLoaded", DomLoaded.loaded, null);
    if (/KHTML|WebKit/i.test(navigator.userAgent))
    { 
      var _timer = setInterval(function()
      {
        if (/loaded|complete/.test(document.readyState))
        {
          clearInterval(_timer);
          delete _timer;
          DomLoaded.loaded();
        }
      }, 10);
    }
    /*@cc_on @*/
    /*@if (@_win32)
    var proto = "src='javascript:void(0)'";
    if (location.protocol == "https:") proto = "src=//0";
    document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            DomLoaded.loaded();
        }
    };
    /*@end @*/
     window.onload = DomLoaded.loaded;
  }
};


if (window.addEventListener)
{
	window.addEventListener("load", fixLayout, false);
	window.addEventListener("resize", fixLayout, false);
}

function fixLayout()
{
	var d = document.getElementById("pagewidth");
	if (d)
	{
		d.style.minHeight = document.documentElement.clientHeight + "px";
	}
}

function preload(url, destination_id, _width, _height, loaded, vars){ // users should not pass loaded as a var
	// this function needs domLoad to work correctly
	if(!loaded){ // loaded should not be passed in by default
		domLoad(url, destination_id, _width, _height); // test that the dom is available
		return;
	}
	if(document.getElementById(destination_id) != null){
		var target_url = encodeURIComponent(url);
	  var FO = { movie:"/flash/preloader.swf", width:_width+"", height:_height+"", majorversion:"8", build:"0", wmode:"transparent", xi:"false", flashvars:("target_url="+target_url+"&extra_params="+(vars ? encodeURI(vars) : ''))};
	  UFO.create(FO, destination_id);
	}
}

function domLoad(url, id, _width, _height){
	// This function is used with preload to wait for the dom to load before trying to insert the preloader
	// arguments are passed from preload and back to preload when dom is loaded
	var _t = setInterval(function() {
		if ((document.getElementsByTagName("body")[0] != null || document.body != null) && document.getElementById(id) != null) {
			preload(url, id, _width, _height, true);
			clearInterval(_t);
		}
	}, 250);
	if (typeof document.addEventListener != "undefined") {
		document.addEventListener("DOMContentLoaded", function() {preload(url, id, _width, _height, true);  clearInterval(_t); } , null); // Gecko, Opera 9+
	}
}

function launch(page) {
  openwin = this.open(page,"Popup",'location=no,scrollbars=yes,toolbar=no,menubar=no,resizable=no,width=600,height=500,top=20,left=20');
}

function tinymceFileBrowser(field_name, url, type, win)
{
  var cmsURL = "/image_uploads/tinymce?";
  var searchString = window.location.search;
  tinyMCE.openWindow({
    file: cmsURL + '&' + searchString + '&type=' + type,
    title: "File Browser",
    width: 650,
    height: 650,
    close_previous: "no"
  }, {
    window: win,
    input: field_name,
    resizable: "yes",
    scrollable: "yes",
    editor_id: tinyMCE.getWindowArg("editor_id")
  });

  return false;
}
