	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, September 2005
	
	Update log:
	
	January 18th, 2006: Added feature that makes the window cover selectboxes(ref. problem with windowed and window less elements).
	January 29th, 2006: Fixed problem showing the windows in Opera
	Feb, 8th 2006:		Added support for creating new windows dynamically
	March,	11th, 2006: Added support for getting content from external files by use of Ajax
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/
		
	var readSizeFromCookie = false;	// Determines if size and position of windows should be set/retreved by use of cookie
	var windowMinSize = [80,30];	// Mininum width and height of windows.
	
	var moveCounter = -1;	
	var startEventPos = new Array();
	var startPosWindow = new Array();
	var startWindowSize = new Array();
	var initResizeCounter = -1;	
	var activeWindow = false;
	var activeWindowContent = false;	
	var windowSizeArray = new Array();
	var windowPositionArray = new Array();
	var currentZIndex = 10000;
	var windowStateArray = new Array();	// Minimized or maximized
	var activeWindowIframe = false;
	var divCounter = 0;
	var zIndexSet = false;
	var MSIEWIN = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Win')>=0 && navigator.userAgent.toLowerCase().indexOf('opera')<0)?true:false;
	var opera = navigator.userAgent.toLowerCase().indexOf('opera')>=0?true:false;
	var ajaxObjects = new Array();	
				
	function cancelEvent()
	{
		return (moveCounter==-1 && initResizeCounter==-1)?true:false;
	}
	function initMove(e)
	{		
		if(document.all)e = event;
		moveCounter = 0;
		switchElement(false,this);
		startEventPos = [e.clientX,e.clientY];
		startPosWindow = [activeWindow.offsetLeft,activeWindow.offsetTop];
		startMove();
		if(!MSIEWIN)return false;	
	}
	
	function startMove()
	{
		if(moveCounter>=0 && moveCounter<=10){
			moveCounter += 1;
			setTimeout('startMove()',10);
		}
	}
	
	function stopMove(e)
	{
		if(document.all)e = event;
		moveCounter=-1;
		initResizeCounter=-1;
		if(!activeWindow || !activeWindowContent)return;
		var state = '0';
		if(windowStateArray[activeWindow.id.replace(/[^0-9]/g,'')])state = '1';
	}
	
	function moveWindow(e)
	{
		if(document.all)e = event;
		if(moveCounter>=10){
			activeWindow.style.left = startPosWindow[0] + e.clientX - startEventPos[0]  + 'px';
			activeWindow.style.top = startPosWindow[1] + e.clientY - startEventPos[1]  + 'px';
		}	
		
		if(initResizeCounter>=10){
			var newWidth = Math.max(windowMinSize[0],startWindowSize[0] + e.clientX - startEventPos[0]);
			var newHeight = Math.max(windowMinSize[1],startWindowSize[1] + e.clientY - startEventPos[1]);
			activeWindow.style.width =  newWidth + 'px';
			activeWindowContent.style.height = newHeight  + 'px';		
			
			if(MSIEWIN && activeWindowIframe){
				activeWindowIframe.style.width = (newWidth) + 'px';	
				activeWindowIframe.style.height = (newHeight+20) + 'px';	
			}
		}
		if(!document.all)return false;
	}
	
	function initResizeWindow(e)
	{
		if(document.all)e = event;
		initResizeCounter = 0;
		switchElement(false,document.getElementById('dhtml_goodies_id' + this.id.replace(/[^\d]/g,'')));
		startWindowSize = [activeWindowContent.offsetWidth,activeWindowContent.offsetHeight];
		startEventPos = [e.clientX,e.clientY];		
		if(MSIEWIN)activeWindowIframe = activeWindow.getElementsByTagName('IFRAME')[0];
		startResizeWindow();
		return false;
	}
	
	function startResizeWindow()
	{
		if(initResizeCounter>=0 && initResizeCounter<=10){
			initResizeCounter++;
			setTimeout('startResizeWindow()',5);
		}
	}
	
	function switchElement(e,inputElement)
	{
		if(!inputElement)inputElement = this;
		var numericId = inputElement.id.replace(/[^0-9]/g,'');
		var state = '0';
		if(windowStateArray[numericId])state = '1';
			
		currentZIndex = currentZIndex/1 + 1;
		activeWindow = document.getElementById('dhtml_goodies_id' + numericId);	
		activeWindow.style.zIndex = currentZIndex;
		activeWindowContent = document.getElementById('windowContent' + numericId);
	}
	
	function hideWindow()
	{
		//switchElement(false,document.getElementById('dhtml_goodies_id' + this.id.replace(/[^\d]/g,'')));	
		activeWindow.style.display='none';		
	}
	
	function minimizeWindow(e,inputObj)
	{
		if(!inputObj)inputObj = this;
		var numericID = inputObj.id.replace(/[^0-9]/g,'');
		switchElement(false,document.getElementById('dhtml_goodies_id' + numericID));
		var state;	
		if(inputObj.src.indexOf('minimize')>=0){
			activeWindowContent.style.display='none';
			document.getElementById('resizeImage'+numericID).style.display='none';
			inputObj.src = inputObj.src.replace('minimize','maximize');	
			windowStateArray[numericID] = false;
			state = '0';		
		}else{			
			activeWindowContent.style.display='block';
			document.getElementById('resizeImage'+numericID).style.display='';
			inputObj.src = inputObj.src.replace('maximize','minimize');
			windowStateArray[numericID] = true;
			state = '1';
		}
	}
	
	function initWindows(e,divObj)
	{
		var divs = document.getElementsByTagName('DIV');
		
		if(divObj)
		{
			var tmpDivs = divObj.getElementsByTagName('DIV');
			var divs = new Array();
			divs[divs.length] = divObj;
			
			for(var no=0;no<tmpDivs.length;no++){
				divs[divs.length] = tmpDivs[no];
			}
		}
		
		for(var no=0;no<divs.length;no++)
		{
			
			if(divs[no].className=='dhtmlgoodies_window')
			{			
				if(MSIEWIN){
					var iframe = document.createElement('IFRAME');
					iframe.style.border='0px';
					iframe.frameborder=0;
					iframe.style.position = 'absolute';
					iframe.style.backgroundColor = '#FFFFFF';
					iframe.style.top = '0px';
					iframe.style.left = '0px';
					iframe.style.zIndex = 100;
					
					var subDiv = divs[no].getElementsByTagName('DIV')[0];
					divs[no].insertBefore(iframe,subDiv);
					
				}					
				if(divObj){
					divs[no].style.zIndex = currentZIndex;
					currentZIndex = currentZIndex /1 + 1;
				}
				
				divCounter = divCounter + 1;	
				if(divCounter==1)activeWindow = divs[no];		
				divs[no].id = 'dhtml_goodies_id' + divCounter;	
				divs[no].onmousedown = switchElement;
				if(readSizeFromCookie)var cookiePos = Get_Cookie(divs[no].id + '_attr') + '';	else cookiePos = '';
				if(divObj)cookiePos='';
				
				//var subImages = divs[no].getElementsByTagName('IMG');
//				var subImages = document.getElementsBySelector('.dhtmlgoodies_close_window');
//				
//				for(var no2=0;no2<subImages.length;no2++)
//				{
//					if(subImages[no2].className=='closeButton')
//					{
//						subImages[no2].id = 'closeImage' + divCounter;
//						subImages[no2].onclick = hideWindow;	
//					}					
//				}			
			}	
			if(divs[no].className=='dhtmlgoodies_windowMiddle' || divs[no].className=='dhtmlgoodies_window_bottom'){
				divs[no].style.zIndex = 1000;				
			}
			if(divs[no].className=='dhtmlgoodies_window_top'){
				divs[no].onmousedown = initMove;
				divs[no].id = 'top_bar'+divCounter;
				divs[no].style.zIndex = 1000;	
			}

			if(divs[no].className=='dhtmlgoodies_windowContent'){
				divs[no].id = 'windowContent'+divCounter;
				divs[no].style.zIndex = 1000;
				if(windowSizeArray[divCounter]){
					divs[no].style.height = windowSizeArray[divCounter][1] + 'px';
					divs[no].parentNode.parentNode.style.width = windowSizeArray[divCounter][0] + 'px';
					
					if(MSIEWIN){
						iframe.style.width = (windowSizeArray[divCounter][0]) + 'px';
						iframe.style.height = (windowSizeArray[divCounter][1]+20) + 'px';
					}
				}
			}			
		}	
		
		if(!divObj){
			document.body.onmouseup = stopMove;	
			document.body.onmousemove = moveWindow;
			document.body.ondragstart = cancelEvent;
			document.body.onselectstart = cancelEvent;
		}
		return divCounter;
	}

	function createNewWindow(width,height,left,top, title)
	{
		var div = document.createElement('DIV');
		div.className='dhtmlgoodies_window';
		document.body.appendChild(div);
		
		var topDiv = document.createElement('DIV');
		topDiv.className='dhtmlgoodies_window_top';
		div.appendChild(topDiv);
		
		var buttonDiv = document.createElement('DIV');
		buttonDiv.className='top_buttons';
		topDiv.appendChild(buttonDiv);
		
		
		var titleDiv = document.createElement('DIV');
		titleDiv.className = 'dhtmlgoodies_window_title';
		
		
		var titleTxt = (title != undefined) ?  document.createTextNode(title) : document.createTextNode('');
		
		
		titleDiv.appendChild(titleTxt);
		
		var titileWrapperDiv = document.createElement('DIV');
		titileWrapperDiv.className = 'top_title';
		topDiv.appendChild(titileWrapperDiv);
		
		topDiv.appendChild(titleDiv);
		
			
		var closeDiv = document.createElement('div');
		closeDiv.className = 'dhtmlgoodies_close_window';
		closeDiv.id= 'dhtmlgoodies_close_window_' + divCounter;
		closeDiv.onclick = hideWindow;
		
		var txt = document.createTextNode("Close X");
		closeDiv.appendChild(txt);
		buttonDiv.appendChild(closeDiv);
		
		var contentDiv = document.createElement('DIV');
		contentDiv.className='dhtmlgoodies_windowContent';
		div.appendChild(contentDiv);

		windowSizeArray[windowSizeArray.length] = [width,height]; /*del*/
		windowPositionArray[windowPositionArray.length] = [left,top]; /*del*/
		
        div.style.left = left + 'px';
        div.style.top = top + 'px';
        div.style.width = width + 'px';	
        div.style.height = height + 'px';
		
		return initWindows(false,div);
	}
	
	function showAjaxContent(ajaxIndex,windowId)
	{
		document.getElementById('windowContent' + windowId).innerHTML = ajaxObjects[ajaxIndex].response;		
	}
	
	function addContentFromUrl(url,windowId)
	{
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = url;	// Specifying which file to get
		ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxContent(ajaxIndex,windowId); };	// Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
	}
	
	/* This function illustrates how you can create a new custom window dynamically */	
	function customFunctionCreateWindow(urlToExternalFile,width,height,left,top, title)
	{
	    Left = left;
        Top = top;      
        
		closeAllWindows();
			
		var divId = createNewWindow(width,height,left,top, title);
		
		
					
		document.getElementById('windowContent' + divId).innerHTML = 
		    '<iframe src=' + urlToExternalFile + ' style="padding: 0px; margin: 0px; width: ' + width + 'px; height: ' + (height-15) + 'px;" frameborder="0" />';
	}		
	
	function closeAllWindows()
	{
    	var divs = document.getElementsByTagName('DIV');
					
	    for(var i=0; i < divs.length ;i++)
	    {
		    if(divs[i].className == 'dhtmlgoodies_window')
		    {	
		        divs[i].style.display = 'none';
		    }
        }
	}
	
	window.onload = initWindows;
	
	var xMouse,yMouse;
    if (document.layers)
        document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove =
    function (evt) 
    {
      xMouse = document.layers ? evt.pageX :
        document.all ? event.x :
        evt.clientX;
      yMouse = document.layers ? evt.pageY :
        document.all ? event.y :
        evt.clientY;        
    }; 
    
//    var xMouse,yMouse;
//    if (document.layers)
//        document.captureEvents(Event.MOUSEMOVE);
//    document.onmousemove =
//    function xyCompatabilty(e) {
//	    var posx = 0;
//	    var posy = 0;
//	    if (!e) var e = window.event;
//	    if (e.pageX || e.pageY) 	{
//		    posx = e.pageX;
//		    posy = e.pageY;
//	    }
//	    else if (e.clientX || e.clientY) 	{
//		    posx = e.clientX + document.body.scrollLeft
//			    + document.documentElement.scrollLeft;
//		    posy = e.clientY + document.body.scrollTop
//			    + document.documentElement.scrollTop;
//	}
//	// posx and posy contain the mouse position relative to the document
//	// Do something with this information
//	xMouse = posx;
//	yMouse = posy;
//	};

    //use Left and Top for opening Login.aspx from UserRegistration.aspx (after click on "Already a member?")
    var Left;
    var Top;
                 
    function CustomFunctionCreateWindowScrollTop(urlToExternalFile,width,height,left,top, title)
    {
        customFunctionCreateWindow(urlToExternalFile,width,height,left ,top+document.documentElement.scrollTop, title);
    }
    
    function CustomFunctionCreateWindowRelative(urlToExternalFile,width,height,left,top, title)
    {                                
        if(xMouse != undefined && yMouse != undefined)
        {
            customFunctionCreateWindow(urlToExternalFile,width,height,xMouse+left ,yMouse+top+document.documentElement.scrollTop, title);
        }
        else
        {
           CustomFunctionCreateWindowScrollTop(urlToExternalFile,width,height,left ,top, title); 
        }
    } 
    
    function CustomFunctionCreateWindowIdentity(urlToExternalFile,width,height,left,top, title)
    {
      
       if (!MSIEWIN)
        customFunctionCreateWindow(urlToExternalFile,width,height,xMouse-width ,yMouse+top+document.documentElement.scrollTop, title);
       else
        CustomFunctionCreateWindowRelative(urlToExternalFile,width,height,left,top, title);
    
    }  
    
    function CustomFunctionCreateWindowManualy(urlToExternalFile,width,height,left,top, title)
    {
       
        if (!MSIEWIN)
        customFunctionCreateWindow(urlToExternalFile,width,height,xMouse-width ,yMouse+top+document.documentElement.scrollTop, title);
       else
       customFunctionCreateWindow(urlToExternalFile,width,height,xMouse-width ,yMouse+top+document.documentElement.scrollTop, title);
        //CustomFunctionCreateWindowRelative(urlToExternalFile,width,height,left,top, title);
    }
    
    function sendImageToParent(imageUrl)
    {
        if( parent.page != null ) //page is the global variable
        {
            parent.page.addOn.setImage(imageUrl);
            
            closeAllWindows();
        }
        else //get from admin to manageVideo (it's one more iframe in the page)
        {
            var manageVideo = window.frames['manageVideoFrame'];
            
            if( manageVideo != null )
            {
                manageVideo.page.addOn.setImage(imageUrl);
                
                manageVideo.closeAllWindows();
            }            
        }        
    }

    function hideCloseButton()
    {
        //there are more than 1 closeButton
    	//because of invisible windows
                                       
       var closeWin = document.getElementById('dhtmlgoodies_close_window_' + (divCounter -1));
				
	   closeWin.style.display = 'none';                        
    }