/*
| This script controls the behaviour of the windows that are opened up as seperate windows
| during the plan analysis.
| Created by: Devlin Dunsmore
*/

/*
| This array contains Window objects that represent the possible
| seperate windows that can be opened during the plan analysis. 
*/
var windows=new Array(1);

/*
| These two variables will be incremented each time a new window is opened
| in order to get the cascading effect when the new windows are open.
*/
var x=screen.width-650+20;
var y=-30;

/*
| This variable represent the amount by which the y variable will
| be incremented by.
*/
var xIncrement=-20;
var yIncrement=30;


var windowCounter=0;


function openWindow(url){
	
	var win=null;
	var winOpen=false;
	var reset=true;	
	
		
	x+=xIncrement;
	y+=yIncrement;
	
	var windowName=url.substring(url.indexOf("=")+1,url.indexOf("&"));
		
	for(var i=0;i<windows.length;i++){
		win=windows[i];
				
		if(win!=null){
			var tempY = -30 + i * yIncrement;
			var tempX = 0;
			
			if(i==0)
				tempX = screen.width - 650 + 20 + i * xIncrement;
			else
				tempX = screen.width - 650 + 20 + (i-1) * xIncrement;
					
					
			if(!win.closed && win.location.href.indexOf(url)!=-1){		
				x-=xIncrement;
				y-=yIncrement;
				
												
				win.close();
				for(i;i<windows.length;i++){
					windows[i].focus();
				}
				win=window.open(url,windowName,"resizable=1,scrollbars=1,height=400,width=600,left="+tempX+",top="+tempY);
				
				winOpen=true;
				reset=false;
			}else if(!win.closed){
				reset=false;
				win.focus();
			}
		}
	}
	
	if(!winOpen && !reset){	
		win=window.open(url,windowName,"resizable=1,scrollbars=1,height=400,width=600,left="+x+",top="+y);
		windows[i]=win;
	}else if(!winOpen && reset){
		windows=new Array(1);
		x=screen.width-650+20;
		y=0;
		win=window.open(url,windowName,"resizable=1,scrollbars=1,height=400,width=600,left="+x+",top="+y);
		windows[1]=win;
	}
	
}

// Taken from free code at http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
	for (var i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
