// JavaScript Document

var defwindowwidth = 300;
var defwindowheight = 200;
var defwindowshadow = 10;
var defwindowopacity = 0.5;
var defwindowtop = 250;
var defwindowleft = "auto";
var defwindowCSS = "";
var defwindowcloseButton = true;
var defwindowautoScroll = false;	




function getScrollPos() {
	if (document.all || window.opera) var scrollPos=document.documentElement.scrollTop;
	else  var scrollPos=document.body.scrollTop;
	return scrollPos;
}
function getElement(id){
      if (document.all)
      return document.all[id];
      if (document.getElementById(id))
      return document.getElementById(id);
      return null;
}
function resizeShadow(id,shadowSize) {
	if (!shadowSize) shadowSize = defwindowshadow;
	if (getElement(id)) {
	matHeight = $("#"+id).height()+(2*shadowSize);
	eval ("$(\'#shadow"+id+"\').css({height: \'"+matHeight+"px\'});");
	}
}
function javaWindowClose(id,targetElement) {
	if (getElement(targetElement)) {var targetElement = getElement(targetElement)}
	else if (getElement('javaPlace')) {var targetElement = getElement('javaPlace')}
	else var targetElement = false;

	if	(getElement(id) && targetElement) {
		targetElement.removeChild(getElement(id));
		if (getElement("shadow"+id)) {
			targetElement.removeChild(getElement("shadow"+id));
		}
		$.post("query.php?type=littleQuery",{type: 'closeDialog', content: id });
	}
}

function makeWindowsCloseButton() {
		return "<div class=\"javaMakeWindowCloseButton\" onclick=\"javaWindowClose(this.parentNode.id)\"></div>";
}
function makeAndLoadWindow(id, classID, width, height, shadow, opacity, url, targetElement, posTop, posLeft, css, closeEnable, autoScroll, content) {
	var defValue = new Array("width","height","shadow","opacity","top","left","CSS","closeButton","autoScroll");
	var defValueIndex = new Array("width","height","shadow","opacity","posTop","posLeft","css","closeEnable","autoScroll");

	for (var key in defValue) {
		eval ("if ("+defValueIndex[key]+" == \"def\" || "+defValueIndex[key]+" == '') "+defValueIndex[key]+" = defwindow"+defValue[key]+";");
	}

	if (getElement(id)) javaWindowClose(id);
	
	if (getElement(targetElement)) {var targetElement = getElement(targetElement)}
	else if (getElement('javaPlace')) {var targetElement = getElement('javaPlace')}
	else var targetElement = false;
	
	if (autoScroll == "" || autoScroll == true || autoScroll == "auto" ) var scrollPos = getScrollPos();
	else var scrollPos = 0;
	
	if (!getElement(id)) {
		if (targetElement) {
			
			var posLeftPx = posLeft;
			if (posLeftPx == "auto") var posLeftPx = (($(window).width() / 2) - ( width / 2));
			if (posLeft+width > $(window).width()) posLeftPx = posLeft-(posLeft+width-$(window).width())
			if (shadow > 0 || shadow == "full") {
				//alert(shadow);
				if (shadow == "full") {
					shWidth = $(window).width();
					shHeight = ($(document).height()+400)+"px";
					shPosTop = scrollPos-200;
					shPosLeft = 0;
				}
				else {
					shWidth = (width+(2*shadow));
					if (height == "auto") {
						shHeight = "auto";
					}
					else {
						shHeight = (height+(2*shadow))+"px";
						height += "px";
					}
					shPosTop = (posTop-shadow)+scrollPos;
					shPosLeft = (posLeftPx-shadow);
				}
				var newShadowElement = document.createElement('div');
				newShadowElement.setAttribute('id','shadow'+id);
				newShadowElement.setAttribute('class','javaMakeWindowShadow '+classID+'Shadow');
				newShadowElement.setAttribute('style','width:'+shWidth+'px;height:'+shHeight+';top:'+shPosTop+'px;left:'+shPosLeft+'px');
				targetElement.appendChild(newShadowElement);
				if (opacity == "") var opacity = 0.2;
				$('#shadow'+id).fadeTo(0,opacity);
			}
			var newElement = document.createElement('div');
			newElement.setAttribute('id',id);
			newElement.setAttribute('class',classID+' roundedge javaWindow');
			newElement.setAttribute('style','position:absolute;width:'+width+'px;height:'+height+';top:'+(posTop+scrollPos)+'px;left:'+posLeftPx+'px;'+css);
			if (closeEnable == "onclick") newElement.setAttribute('onclick','javaWindowClose(this.id)');
			targetElement.appendChild(newElement);
		}
	}
	var additionContent = "";
	if (closeEnable == true) {
		additionContent = makeWindowsCloseButton();
	}
	if (posLeft == "auto") {
		scriptResiceContent = "posTop = (($(window).width() / 2) - ( "+width+" / 2));\
							   $('#"+id+"').css(\"left\", posTop ); ";
		if (shadow != "full") {
	      scriptResiceContent += "$('#shadow"+id+"').css(\"left\", (posTop-"+shadow+"));";
		}
		additionContent += "<script> \
			$(window).resize(function(){	"+scriptResiceContent+" }); \
			</script>";
	}
	if (autoScroll == "auto") {
		additionContent += "<script> \
		var $ = jQuery.noConflict(); \
		$(window).scroll(function () { \
	      $('#shadow"+id+"').css(\"top\", (";
		if (shadow == "full") additionContent += "-200";
		else additionContent += (posTop-shadow);
		additionContent +=	"+getScrollPos()) ); \
	      $('#"+id+"').css(\"top\", ("+posTop+"+getScrollPos())); \
    });	 </script>";
	}
	if (!additionContent) additionContent="";
	$("#"+id).html(additionContent+"<br />LOADING....");
	if (url!=false) $.get(url,function(data){$("#"+id).html(additionContent+data);});
	else $("#"+id).html(additionContent+content);
	if (height == "auto") {
		matHeight = $("#"+id).height()+(2*shadow);
		eval ("$(\'#shadow"+id+"\').css({height: \'"+matHeight+"px\'});");
	}
}
