var zIndexCount = 1001;
var drawnWindows=0;
var isIE = navigator.appVersion.match(/MSIE/) == "MSIE";
function centerWindow(winname){
var arrayPageSize = WindowUtilities.getPageSize();
var arrayPageScroll = WindowUtilities.getPageScroll();

height=$(winname).style.height.substring(0,$(winname).style.height.length-2); //strip px from numbers
width=$(winname).style.width.substring(0,$(winname).style.width.length-2); //strip px from numbers
$(winname).style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - height) / 2) + 'px');
$(winname).style.left = (((arrayPageSize[0] - width) / 2) + 'px');
}

function addHTML (html) {
  if (document.all)
    document.body.insertAdjacentHTML('beforeEnd', html);
  else if (document.createRange) {
    var range = document.createRange();
    range.setStartAfter(document.body.lastChild);
    var docFrag = range.createContextualFragment(html);
    document.body.appendChild(docFrag);
  }
  else if (document.layers) {
    var l = new Layer(window.innerWidth);
    l.document.open();
    l.document.write(html);
    l.document.close();
    l.top = document.height;
    document.height += l.document.height;
    l.visibility = 'show';
  }
}

var confirm_links = new Array();

function confirm_win(window_id,confirm_text,link){
	confirm_links[window_id]=link;
	if($(window_id)) {
		centerWindow(window_id);
		if($(window_id).style.display=='none'){
		WindowUtilities.disableScreen(this.className);
			Element.show(window_id);
		}
	 	return;
	}

	addHTML('<div id="'+window_id+'" class="confirmation_box" style="display:none;width:280px;" align="center">'+confirm_text+'<br>&nbsp;<br><div align="center"><input type="button" class="button" value="Yes" onclick="window.location=confirm_links[\''+window_id+'\'];"> <input type="button" value="No" class="button" onclick="WindowUtilities.enableScreen();Element.hide(\''+window_id+'\');"><br></div></div>');

	confirm_win(window_id,confirm_text,link);

}

function drawWindow(winname,title,width,height,contentdiv){
if(drawnWindows==0 && isIE){
		selects = document.getElementsByTagName("select");
		    for (var i = 0; i != selects.length; i++) {
		    	selects[i].style.visibility = "hidden";
	    }
}

if($(winname)) {
	centerWindow(winname);
	if($(winname).style.display=='none'){
		Element.show(winname);
		drawnWindows++;
	}
	return;
}

if(!$('window_TEMPLATE')) { alert('No window template found.'); return; }

var newWindow = $('window_TEMPLATE').cloneNode(true);


newWindow.id=winname;
newWindow.style.width=width;
newWindow.style.height=height;

s = new String(newWindow.innerHTML);
s=s.replace('WINWIDTH',width);
s=s.replace('WINWIDTH',width);
var part1height=0;
if(height<135)
	height=135;
part1height=height-115;
s=s.replace('PART1WINHEIGHT',''+part1height);
s=s.replace('PART1WINHEIGHT',''+part1height);
s=s.replace('PART1WINHEIGHT',''+part1height);
s=s.replace('PART1WINHEIGHT',''+part1height);
s=s.replace('WINTITLE',title);
s=s.replace('WINNAME',''+winname);
s=s.replace('WINNAME',''+winname);
s=s.replace('WINNAME',''+winname);
for(i=0;i<7;i++)
	s=s.replace('skip="true"','');

//alert(s);
newWindow.innerHTML=s;
//alert(newWindow.getElementsByTagName('div')[0].id);
var arrayPageSize = WindowUtilities.getPageSize();
var arrayPageScroll = WindowUtilities.getPageScroll();

newWindow.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - height) / 2) + 'px');
newWindow.style.left = (((arrayPageSize[0] - width) / 2) + 'px');

window.document.body.appendChild(newWindow);
//alert(newWindow.innerHTML);
$('window_content_'+winname).style.height=height-$(winname+'_title').style.width-27;
//$('window_content_'+winname).innerHTML=$(contentdiv).innerHTML;
$('window_content_'+winname).appendChild($(contentdiv));
//$('window_content_'+winname).insertBefore($('window_content_'+winname),$(contentdiv));
//Element.remove(contentdiv);

newWindow.style.position='absolute';
zIndexCount++;
//ert(zIndexCount);
newWindow.style.zIndex=zIndexCount;

//process();
if(self.pngFix) pngFix();

newWindow.style.display='block';
new Draggable(newWindow,{starteffect:null,endeffect:null, handle: winname+'_title'});
drawnWindows++;
}

function eraseWindow(winname){
	Element.hide(winname);
	drawnWindows--;
	if(drawnWindows<=0 && isIE){
		selects = document.getElementsByTagName("select");
		    for (var i = 0; i != selects.length; i++) {
		    	selects[i].style.visibility = "visible";
		    }
	}

}

var WindowUtilities = {
 	getPageScroll :function() {
		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}

		arrayPageScroll = new Array('',yScroll)
		return arrayPageScroll;
	},

	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	getPageSize: function(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}

		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
		return arrayPageSize;
	},

 	disableScreen: function(className) {
		WindowUtilities.initLightbox(className);
		var objBody = document.getElementsByTagName("body").item(0);

		// prep objects
	 	var objOverlay = $('overlay_modal');

		var arrayPageSize = WindowUtilities.getPageSize();

		// Hide select boxes as they will 'peek' through the image in IE
		if (isIE) {
			selects = document.getElementsByTagName("select");
		    for (var i = 0; i != selects.length; i++) {
		    	selects[i].style.visibility = "hidden";
		    }
		}

		// set height of Overlay to take up whole page and show
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		objOverlay.style.display = 'block';
	},

 	enableScreen: function() {
	 	var objOverlay = $('overlay_modal');
		if (objOverlay) {
			// hide lightbox and overlay
			objOverlay.style.display = 'none';

			// make select boxes visible
			if (isIE) {
				selects = document.getElementsByTagName("select");
			    for (var i = 0; i != selects.length; i++) {
					selects[i].style.visibility = "visible";
				}
			}
			objOverlay.parentNode.removeChild(objOverlay);
		}
	},

	// initLightbox()
	// Function runs on window load, going through link tags looking for rel="lightbox".
	// These links receive onclick events that enable the lightbox display for their targets.
	// The function also inserts html markup at the top of the page which will be used as a
	// container for the overlay pattern and the inline image.
	initLightbox: function(className) {
		// Already done, just update zIndex
		if ($('overlay_modal')) {
			Element.setStyle('overlay_modal', {zIndex: 1023});
		}
		// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
		else {
			var objBody = document.getElementsByTagName("body").item(0);
			var objOverlay = document.createElement("div");
			objOverlay.setAttribute('id', 'overlay_modal');
			objOverlay.className = "overlay_" + className
			objOverlay.style.display = 'none';
			objOverlay.style.position = 'absolute';
			objOverlay.style.top = '0';
			objOverlay.style.backgroundColor = '#FFFFFF';
opacity = 60;
	objOverlay.style.opacity = (opacity / 100);
    objOverlay.style.MozOpacity = (opacity / 100);
    objOverlay.style.KhtmlOpacity = (opacity / 100);
    objOverlay.style.filter = "alpha(opacity=" + opacity + ")";

			objOverlay.style.left = '0';
			objOverlay.style.zIndex = 1023;
		 	objOverlay.style.width = '100%';

			objBody.insertBefore(objOverlay, objBody.firstChild);
		}
	},

	setCookie: function(value, parameters) {
    document.cookie= parameters[0] + "=" + escape(value) +
      ((parameters[1]) ? "; expires=" + parameters[1].toGMTString() : "") +
      ((parameters[2]) ? "; path=" + parameters[2] : "") +
      ((parameters[3]) ? "; domain=" + parameters[3] : "") +
      ((parameters[4]) ? "; secure" : "");
  },

  getCookie: function(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
    } else {
      begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
      end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
  }
}

