var Windows = new Array();
var belenusHomeRootPath = belenusHome.location.href.substring(0, belenusHome.location.href.length-belenusHome.location.search.length);
  belenusHomeRootPath = belenusHomeRootPath.substring(0, belenusHomeRootPath.lastIndexOf("/")+1);

/****************************************************************************************************************\
|** Window params
|** Windows['window'][0] = ''										<-- undefined param... maybe for later...
|** Windows['window'][1] = 'url'									<-- the url
|** Windows['window'][2] = 'name'									<-- window name
|** Windows['window'][3] = 'width=600,height=400'					<-- window properties
|** Windows['window'][4] = 'left=20,top=20'							<-- pos x|y, empty string == center 
|** Windows['window'][5] = new Array("Tab Caption", lockable, duplicate, scrolling) <-- tab able (if empty... it will not include into the tab registry!) (duplicate 0=yes, 1=no & reload page, 2=no &don't reload of the page in tab)
|** Windows['window'][99] = null									<-- reference to the window itself
|** 
\****************************************************************************************************************/

/*****************************************************************************************************************/
// !! customize here please.... !!
var basketWinName = "basket"; 

var winNames = new Array("basket", "info", "help", "search", "basketPopup", "printPopup","imagePrint", 
    "languageChooser", "splPrintPopup", "notes", "favorites", "movenote", "movefavorite", "drawingPrint"); // the names of the different windows...

// look description above

  // create the basket window... 
	Windows[basketWinName] = new Array();
	Windows[basketWinName][0] = '';
	Windows[basketWinName][1] = 'basket/index.html';
	Windows[basketWinName][2] = 'basket';
	Windows[basketWinName][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows[basketWinName][4] = '';
  Windows[basketWinName][5] = new Array(belenusHome.ld['nav_basket'],false,2,0);
	Windows[basketWinName][99] = null;
  
	// create the basket window... 
	Windows[basketWinName+"Extra"] = new Array();
	Windows[basketWinName+"Extra"][0] = '';
	Windows[basketWinName+"Extra"][1] = 'basket/index.html';
	Windows[basketWinName+"Extra"][2] = 'basket';
	Windows[basketWinName+"Extra"][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows[basketWinName+"Extra"][4] = '';
  Windows[basketWinName+"Extra"][5] = '';
	Windows[basketWinName+"Extra"][99] = null;  
  
  // create the info window...
  Windows['info'] = new Array();
  Windows['info'][0] = '';
  Windows['info'][1] = 'info.html';
  Windows['info'][2] = 'info';
  Windows['info'][3] = 'width=300,height=400,scrollbars=no,resizable=no';
  Windows['info'][4] = '';
  Windows['info'][5] = '';
  Windows['info'][99] = null;	
  
  // create the help window...
  Windows['help'] = new Array();
  Windows['help'][0] = '';
  Windows['help'][1] = 'help/'+belenusHome.currentLanguage+'/default.htm';
  Windows['help'][2] = 'help';
  Windows['help'][3] = 'width=760,height=500,scrollbars=yes,resizable=yes,toolbar=yes';
  Windows['help'][4] = '';
  Windows['help'][5] = '';
  Windows['help'][99] = null;		
  
  // create the search window...
  Windows['search'] = new Array();
  Windows['search'][0] = '';
  Windows['search'][1] = 'search/index.html';//index.html';
  Windows['search'][2] = 'search';
  Windows['search'][3] = 'width=450,height=450,resizable=yes';
  Windows['search'][4] = '';
  Windows['search'][5] = new Array(belenusHome.ld['nav_search'],false,0,0);
  Windows['search'][99] = null;			
  
  // create the addToBasketConfirm window...
  Windows['basketPopup'] = new Array();
  Windows['basketPopup'][0] = '';
  Windows['basketPopup'][1] = 'basket/confirm_window.html';
  Windows['basketPopup'][2] = 'basketPopup';
  Windows['basketPopup'][3] = 'width=600,height=120';
  Windows['basketPopup'][4] = '';
  Windows['basketPopup'][5] = '';
  Windows['basketPopup'][99] = null;	
  
  // create the basketPrintPopup window...
  Windows['printPopup'] = new Array();
  Windows['printPopup'][0] = '';
  Windows['printPopup'][1] = 'basket/print.html';
  Windows['printPopup'][2] = 'printPopup';
  Windows['printPopup'][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
  Windows['printPopup'][4] = '';
  Windows['printPopup'][5] = '';
  Windows['printPopup'][99] = null;		
  
  // create image print popup
  Windows['imagePrint'] = new Array();
  Windows['imagePrint'][0] = '';
  Windows['imagePrint'][1] = 'about:blank';
  Windows['imagePrint'][2] = 'imagePrint';
  Windows['imagePrint'][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
  Windows['imagePrint'][4] = '';
  Windows['imagePrint'][5] = '';
  Windows['imagePrint'][99] = null;	
  
  // create language chooser popup
  Windows['languageChooser'] = new Array();
  Windows['languageChooser'][0] = '';
  Windows['languageChooser'][1] = 'data_gui_language_chooser.html';
  Windows['languageChooser'][2] = 'languageChooser';
  Windows['languageChooser'][3] = 'width=430,height=260';
  Windows['languageChooser'][4] = '';
  Windows['languageChooser'][5] = '';
  Windows['languageChooser'][99] = null;	
  
  // create the spl print window...
  Windows['splPrintPopup'] = new Array();
  Windows['splPrintPopup'][0] = '';
  Windows['splPrintPopup'][1] = 'about:blank';
  Windows['splPrintPopup'][2] = 'splPrintPopup';
  Windows['splPrintPopup'][3] = 'width=800,height=600,scrollbars=yes,resizable=no';
  Windows['splPrintPopup'][4] = '';
  Windows['splPrintPopup'][5] = '';
  Windows['splPrintPopup'][99] = null;		
  
  // create the notes window...
  Windows['notes'] = new Array();
  Windows['notes'][0] = '';
  Windows['notes'][1] = '../jsp/personals/notes.html';
  Windows['notes'][2] = 'notes';
  Windows['notes'][3] = 'width=500,height=600,scrollbars=yes,resizable=yes';
  Windows['notes'][4] = '';
  Windows['notes'][5] = new Array(belenusHome.ld['p_titleN'],false,2,0);
  Windows['notes'][99] = null;	
  
  // create the favorites window...
  Windows['favorites'] = new Array();
  Windows['favorites'][0] = '';
  Windows['favorites'][1] = '../jsp/personals/favorites.html';
  Windows['favorites'][2] = 'favorites';
  Windows['favorites'][3] = 'width=500,height=600,scrollbars=yes,resizable=yes';
  Windows['favorites'][4] = '';
  Windows['favorites'][5] = new Array(belenusHome.ld['p_titleF'],false,2,0);
  Windows['favorites'][99] = null;							
  
  // create the move notes window...
  Windows['movenote'] = new Array();
  Windows['movenote'][0] = '';
  Windows['movenote'][1] = '../jsp/personals/move.html';
  Windows['movenote'][2] = 'movenote';
  Windows['movenote'][3] = 'height=400,width=280';
  Windows['movenote'][4] = '';
  Windows['movenote'][5] = '';
  Windows['movenote'][99] = null;							
  
  // create the move favorites window...
  Windows['movefavorite'] = new Array();
  Windows['movefavorite'][0] = '';
  Windows['movefavorite'][1] = '../jsp/personals/move.html';
  Windows['movefavorite'][2] = 'movefavorite';
  Windows['movefavorite'][3] = 'height=400,width=280';
  Windows['movefavorite'][4] = '';
  Windows['movefavorite'][5] = '';
  Windows['movefavorite'][99] = null;		
  
  // create drawing print popup
	Windows['drawingPrint'] = new Array();
	Windows['drawingPrint'][0] = '';
	Windows['drawingPrint'][1] = 'drawing_print.html';
	Windows['drawingPrint'][2] = 'drawingPrint';
	Windows['drawingPrint'][3] = 'width=800,height=600,scrollbars=yes,resizable=yes';
	Windows['drawingPrint'][4] = '';
  Windows['drawingPrint'][5] = '';
	Windows['drawingPrint'][99] = null;	 					
  
/*****************************************************************************************************************/
  

/*****************************************************************************************************************/
// internal functions
  
function getCenterCoordinates(wName) { // returns the start position
  var winProps = Windows[wName][3];
  if(winProps.indexOf("width=") != -1 && winProps.indexOf("height=") != -1) {
    var x = "left=";
    var y = "top=";
    var difValues = winProps.split(",");
    for(i in difValues) {
      if(difValues[i].indexOf("width") != -1) {
        x += (screen.width / 2) - (parseInt(difValues[i].split("=")[1]) / 2);
      }
      if(difValues[i].indexOf("height") != -1) {
        y += (screen.height / 2) - (parseInt(difValues[i].split("=")[1]) / 2);
      }
    }
    return (x+","+y);
  } else return "left=0,top=0";
}

/*****************************************************************************************************************/



/*****************************************************************************************************************/
// object methods

function openWindow(wName, queryStr, reload) {
  if(Windows[wName] != null) {	// test if the wished window is defined at the WindowManager....
		if(Windows[wName][5] != "" && belenusHome.useTabManager && queryStr.indexOf("loadPage=") == -1) {
      Windows[wName][99] = belenusHome.TAM.appendTab(Windows[wName][2],belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr),Windows[wName][5][0],Windows[wName][5][1],Windows[wName][5][2],Windows[wName][5][3]);
    } else { // not included as tab in the registry
    var xtr = Windows[wName][4]; // set pos x,y.....
    if(Windows[wName][4] == "") {
      xtr = getCenterCoordinates(wName) // if pos x,y param is empty... set window centered
    }
      
    if(Windows[wName][99] != null) { // if the window contains a window obj..... and not null
    
      if(Windows[wName][99].closed == false) { // if the window is open....
        if(reload) {
         //Windows[wName][99].location.reload();
         if(Windows[wName][99].location.search.indexOf("?") != -1) {
          if(Windows[wName][99].location.search != "?"+queryStr) {
            if(uri != "about:blank") Windows[wName][99].location.href = belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr);
          } else {
            Windows[wName][99].location.reload()
          }
         }
        }
        Windows[wName][99].focus();
      } else { // if the window is closed, but in the past it has exists...
        var uri = Windows[wName][1];
        if(uri != "about:blank") uri = belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr);
        Windows[wName][99] = belenusHome.open(uri,Windows[wName][2],Windows[wName][3].concat(",").concat(xtr)); // open window with the right parameters...
        Windows[wName][99].name = Windows[wName][2];
      }
      
    } else { // if it was null
      var uri = Windows[wName][1];
      if(uri != "about:blank") uri = belenusHomeRootPath.concat(Windows[wName][1]).concat("?").concat(queryStr)
      Windows[wName][99] = belenusHome.open(uri,Windows[wName][2],Windows[wName][3].concat(",").concat(xtr)); // open window with the right parameters...
      Windows[wName][99].name = Windows[wName][2];			
    }
    
	  }	
  } else {
    alert("The window, that you try to open, is not defined in the WindowManager!");
  }
}


function closeWindow(wName) {
  if(Windows[wName] != null) {
    if(Windows[wName][99] != null) {
      if(Windows[wName][99].closed == false) {
        Windows[wName][99].close();
        Windows[wName][99] = null;
      }
    }
  } else {
    alert("The window, that you try to close, is not defined in the WindowManager!");
  }
}


function closeAllWindows() {
  for(i in winNames) {
    closeWindow(winNames[i]);
  }
}


function getWindowReference(wName) {
  if(Windows[wName] != null) {
    if(Windows[wName][99] != null) {
      return Windows[wName][99];
    } else {
      if(Windows[wName][5] != "") {
      return Windows[wName][99];
      } else return null;// the window wasn't init....
    }
  } else {
    alert("The window, that you try to open, is not defined in the WindowManager!\n& can't be referred\nThe returning value of this function is null");
    return null;
  }
}

/*****************************************************************************************************************/




/*****************************************************************************************************************\
|** WindowManager
|** WindowManager.openWindow(wName, queryStr, reload) 
|** WindowManager.closeWindow(wName)
|** WindowManager.closeAllWindows()
|** WindowManager.getWindowReference(wName) <-- return a window object or null if it dosn't exists or wasn't init...
|**
\*****************************************************************************************************************/


function WindowManager() { // add the methods to the object...
  this.openWindow = openWindow;
  this.closeWindow = closeWindow;
  this.closeAllWindows = closeAllWindows;
  this.getWindowReference = getWindowReference;
}

/*****************************************************************************************************************/


/*****************************************************************************************************************/
// make the object... init the WindowManager.....

belenusHome.WM = new WindowManager();

/*****************************************************************************************************************/

