/*
* FormWondow 1.1
* Inspired by Thickbox
* By Douglas Gintz (http://www.artbuilders.com)
* Copyright (c) 2008 Douglas Gintz
* Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/




var fw_pathToImage = "/img/loadingAnimation.gif";
$(document).ready(function(){ 
    imgLoader = new Image();// preload image
    imgLoader.src = fw_pathToImage;
});

function fw_show(caption, url, width, height, section, action, id) {//function called when the user clicks on a thickbox link
	try {
		var baseURL = url;
		var id = id;
		var section = section; //certificates,regulations
		var action = action; //either adding in case we use act id or we use row id if editing
		var width = width;
		var height = height;
        if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
            $("body","html").css({height: "100%", width: "100%"});
            $("html").css("overflow","hidden");
            if (document.getElementById("FW_HideSelect") === null) {//iframe to hide select elements in ie6
               $("body").append("<iframe id='FW_HideSelect'></iframe><div id='FW_overlay'></div><div id='FW_window'></div>");
               $("#FW_overlay").click(fw_remove);
               $("div.form-button-close").click(fw_remove);
           }
        }else{//all others
            if(document.getElementById("FW_overlay") === null){
                $("body").append("<div id='FW_overlay'></div><div id='FW_window'></div>");
                $("#FW_overlay").click(fw_remove);
                $("div.form-button-close").click(fw_remove);
            }
        }

        if(fw_detectMacXFF()){
            $("#FW_overlay").addClass("FW_overlayMacFFBGHack");//use png overlay so hide flash
        }else{
            $("#FW_overlay").addClass("FW_overlayBG");//use background and opacity
        }

        if(caption===null){caption="";}
        $("body").append("<div id='FW_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
        $('#FW_load').show();//show loader


		FW_WIDTH = (width*1) + 30 || 630; //defaults to 630 if no paramaters were added to URL
		FW_HEIGHT = (height*1) + 40 || 440; //defaults to 440 if no paramaters were added to URL
		ajaxContentW = FW_WIDTH - 30;
		ajaxContentH = FW_HEIGHT - 45;

		$("#FW_iframeContent").remove();
         
        $("#FW_window").append("<div id='FW_title'><div id='FW_ajaxWindowTitle'>"+caption+"</div><div id='FW_closeAjaxWindow'><a href='#' id='FW_closeWindowButton' title='Close'><span class=\"standard-win-close-btn\"><img src=\"/img/forms/btw_win_close.gif\" width=\"19\" height=\"19\" alt=\"Close Window\"  /></span></a></div></div><iframe frameborder='0' hspace='0' src='"+baseURL+"?id="+id+"&section="+section+"&action="+action+"' id='FW_iframeContent' name='FW_iframeContent"+Math.round(Math.random()*1000)+"' onload='fw_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
	    $("#FW_window").append("<div class=\"form-button-close\"><a href=\"#\">Close Window</a></div>");


		$("#FW_closeWindowButton").click(fw_remove);
		$("div.form-button-close").click(fw_remove);

		fw_position();
		if($.browser.safari){//safari needs help because it will not fire iframe onload
			$("#FW_load").remove();
			$("#FW_window").css({display:"block"});
		}

		   $("#FW_window").css({display:"block"});

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

	} catch(e) {
		//nothing here
	}
}
//helper functions below
function fw_showIframe(){
    $("#FW_load").remove();
    $("#FW_window").css({display:"block"});
	//make draggable
	//$('#FW_window').draggable({ iframeFix: false, handle: '#FW_title' ,opacity: 0.88,cursor: 'move'   });
	//getter
	//var iframeFix = $('#FW_window').draggable('option', 'iframeFix');
	//setter
	//$('#FW_window').draggable('option', 'iframeFix', true);
	
}
function fw_remove() {
    $("#FW_imageOff").unbind("click");
    $("#FW_closeWindowButton").unbind("click");

	
    $("#FW_window").fadeOut("fast",function(){$('#FW_window,#FW_overlay,#FW_HideSelect').trigger("unload").unbind().remove();});
    $("#FW_load").remove();
	//remove drag
	//$('#FW_window').draggable('destroy');
	document.onkeydown = "";
	document.onkeyup = "";
	refreshPage()
	return false;
}
function fw_position() {
    $("#FW_window").css({marginLeft: '-' + parseInt((FW_WIDTH / 2),10) + 'px', width: FW_WIDTH + 'px'});
    if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
        $("#FW_window").css({marginTop: '-' + parseInt((FW_HEIGHT / 2),10) + 'px'});
    }
}


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


