/*  pageResize modul v0.3 by M.5.IT
 *  pageResize is freely distributable under the terms of an M.5.IT-style license.
 *  For details, see the M.5.IT website http://m5it.org
/*--------------------------------------------------------------------------*/

/*var Position = {
        cumulativeOffset: function(element) {
            var valueT = 0, valueL = 0;
            do {
                valueT += element.offsetTop  || 0;
                valueL += element.offsetLeft || 0;
                element = element.offsetParent;
            } while (element);
        return [valueL, valueT];
    }
};*/

var objSize = {
   Copy: function(text) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text);
  }
},
	
   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;
    }  
    var pageHeight, pageWidth;

    // 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;
    }

    return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
  },
  
  rPx: function(value) {
		return value.substring(0, value.length-2);
  },
  
  resetObjPos: function(posID, resID, left, top, vis) {
   //    pos[0] = left;          pos[1] = top;
   var pos = Position.cumulativeOffset($(posID));
   $(resID).style.left = (pos[0] + left)+'px';
   $(resID).style.top  = (pos[1] + top)+'px';
   $(resID).style.visibility = vis;
  },
  
  hover: function(id, path) {
   var p = "url("+path+")";
   $(id).setStyle({backgroundImage:p });
  },
  
  /* need to include scriptaculous.js */
  pCText: "<table border='0' width='100%'>"+
          "  <tr>"+
		  "    <td>"+
		  "        <div id='pCTextHL'> Page resize config 0.3b</div>"+
		  "        <div id='pCTextHR' onclick='objSize.pageCoatOff();'>( X )</div>"+
		  "    </td>"+
		  "  </tr>"+
		  "  <tr>"+
		  "    <td>"+
		  "        <table width='100%'>"+
		  "          <tr>"+
		  "            <td>"+
		  "<div id='pCTextM'>"+
		  "<h4>Set page horizontal side space: </h4>"+
		  "<table><tr><td>"+
		  "<div id='track1' class='track' style='width:200px;background-color:#aaa;height:5px;'>"+
    	  "<div id='handle1' class='handle' style='width:5px;height:10px;background-color:#f00;cursor:move;'> </div>"+
		  "</td><td>"+
		  "<div id='debug1' class='debug'></div>"+
		  "</td></tr></table>"+
		  "</div>"+
		  "<div id='pCTextM'>"+
		  "<h4>Set page vertical side space: </h4>"+
		  "<table><tr><td>"+
		  "<div id='track2' class='track' style='width:200px;background-color:#aaa;height:5px;'>"+
    	  "<div id='handle2' class='handle' style='width:5px;height:10px;background-color:#f00;cursor:move;'> </div>"+
		  "</td><td>"+
		  "<div id='debug2' class='debug'></div>"+
		  "</td></tr></table>"+
		  "</div>"+
		  "            </td>"+
		  "          </tr>"+
		  "        </table>"+
		  "    </td>"+
		  "  </tr>"+
		  "</table>",
		  
  pageCoatOff: function(){
	$('myCoat').remove();
	$('myOut').remove();
  },
  
  pageCoatOn: function() {
  	var objPs = this.getPageSize();
	
	var outLeft = (parseInt(objPs['pageWidth'])/2)-120;
	var outTop  = (parseInt(objPs['pageHeight'])/2)-120;
	
    var myCoat = document.createElement('div');
	
	myCoat.style.position = "absolute";
	myCoat.style.top = "0px";
	myCoat.style.left = "0px";
	myCoat.style.width = objPs['pageWidth'];
	myCoat.style.height = objPs['pageHeight'];
	myCoat.style.background = "#000";
	myCoat.style.MozOpacity      = '.50';
    myCoat.style.opacity         = '.50';
    myCoat.style.visibility      = 'visible';
	myCoat.setAttribute('id', 'myCoat');
		
	var myOut = document.createElement("div");
	myOut.style.background = "#fff";
	myOut.style.position ='absolute';
	myOut.style.top  = outTop+'px';
	myOut.style.left = outLeft+'px';
	myOut.style.width = '300px';
	myOut.style.height = 'auto';
	myOut.style.border = 'solid 2px #000';
	myOut.style.visibility = 'hidden';
	myOut.setAttribute('id', 'myOut')
	
	document.body.appendChild(myCoat);
	
	new Effect.Grow(myCoat);
	
	var ps = objSize.getPageSize();
	new Effect.Event({ afterFinish:function(){
			  setTimeout("$('myOut').style.visibility = 'visible';", 1500);
			  document.body.appendChild(myOut);
			  $(myOut).innerHTML = objSize.pCText;
			  
			  new Control.Slider('handle1','track1',{range:$R(0,100),
                  values:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100],
				  onChange:function(v){
				  	  var x = (((ps['pageWidth']/2)/100)*v);
					  myPR.sidesW = x;
					  myPR.now();
					  
					  myCO.set("handle1", x, 1);
				      $('debug1').innerHTML=parseInt(x)+'px';
				  },
				  onSlide:function(v) { 
				      var x = (((ps['pageWidth']/2)/100)*v);
				      $('debug1').innerHTML=parseInt(x)+'px';
				 }});
				 
				 new Control.Slider('handle2','track2',{range:$R(0,100),
                  values:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100],
				  onChange:function(v){
				  	  var x = (((ps['pageHeight']/2)/100)*v);
					  myPR.sideHeight = x;
					  myPR.now();
					  
					  myCO.set("handle2", x, 1);
				      $('debug2').innerHTML=parseInt(x)+'px';
				  },
				  onSlide:function(v) { 
				      var x = (((ps['pageHeight']/2)/100)*v);
				      $('debug2').innerHTML=parseInt(x)+'px';
				 }});
    }, position: 'end' });
  }
};

var pageResize = Class.create();
pageResize.prototype = {
    initialize: function() {
        this.id_header = myCN.id_page_header;
        this.id_unHead = myCN.id_page_uheader;
        this.id_lcon   = myCN.id_page_lcon;
        this.id_rcon   = myCN.id_page_rcon;
        this.id_foot   = myCN.id_page_footer;
        
        this.id_but_switch = myCN.id_page_B_switch;
        this.id_but_menu   = myCN.id_page_B_show;
		this.id_but_coat   = myCN.id_page_B_coat;
        /*###################################################*/
        this.menuSide      = myCN.set_pageMenuSide;
		this.menuDisable   = myCN.set_pageMenuDisable;
		
		if(myCO.get("handle1")!=-1)
		    this.sidesW = myCO.get("handle1"); // horizontal by slide
		else
		    this.sidesW = myCN.set_pageHMargin; // horizontal by config
		if(myCO.get("handle2")!=-1)
		    this.sideHeight = myCO.get("handle2"); // vertical side space by slide
		else
		    this.sideHeight = myCN.set_pageVMargin; // vertical side space by config
		
		/*###################################################*/
        this.sideWidth     = myCN.set_pageMenuWidth;
        this.HeaderHeight  = myCN.set_pageHeadHeight;
        this.UHeaderHeight = myCN.set_pageUHeadHeight;
        this.FooterHeight  = myCN.set_pageFootHeight;
        /*###################################################*/
		this.border        = 0; // ce je border 1 px, more odstet 2 kr sta na vsaki strani
        this.contHeight    = 0; // auto calculate 
        this.menuShow  = 'show';               
        this.objPS   = 0;
        this.pWidth  = 0;
        this.pHeight = 0;
    },
    
    now: function(side) {
      var myCOSwitch = myCO.get('myPRSwitch');
      if(myCOSwitch == -1) {
        if((typeof(side)=='undefined') || (side=='')) {
            if(this.menuSide == '')
                this.menuSide = 'left';
        }
        else this.menuSide = side;
      }
      else this.menuSide = myCOSwitch;
             
        this.objPS   = objSize.getPageSize();
        this.pWidth  = this.objPS.windowWidth; 
		
		if (this.sideHeight > 0) {
			this.pHeight = this.objPS.windowHeight-this.sideHeight;
		}
		else this.pHeight = this.objPS.windowHeight;
		
        this.contHeight = this.pHeight -((this.HeaderHeight+this.border)+
                                        (this.UHeaderHeight+this.border)+
                                        (this.FooterHeight+this.border));
                               
        var headTop  ,headLeft ,headWidth ,headHeight;
        var uheadTop ,uheadLeft,uheadWidth,uheadHeight;
        var lconTop  ,lconLeft ,lconWidth ,lconHeight, tmpLLeft;
        var rconTop  ,rconLeft ,rconWidth ,rconHeight, tmpRLeft;
        var footTop  ,footLeft ,footWidth ,footHeight;
        
        rconWidth = this.pWidth-(this.sideWidth+(this.border*2));
        
        if(this.menuSide=='left') {
			if (this.sidesW>0) {
				//alert("Left LandR");
				tmpLLeft = 0;
				tmpRLeft = this.sideWidth + this.border;
			}
			else {
				tmpLLeft = 0;
				tmpRLeft = this.sideWidth + this.border;
			}
        }
        else if(this.menuSide=='right') {
			if (this.sidesW>0) {
				//alert("Right LandR");
				tmpRLeft = (this.sideHeight/2)
				tmpLLeft = (rconWidth-(this.sideHeight*3))+2;
			}
			else {
				tmpRLeft = 0;
				tmpLLeft = rconWidth;
			}
        }
        else alert('ERROR: pageResize() [ side ] undefined!');
        
        /*HEADER*/
		if (this.sidesW>0) {
			headTop = 0;
			
			headLeft = this.sidesW;
			headWidth = (this.pWidth - this.border) - (this.sidesW)*2;
			
			headHeight = this.HeaderHeight;
		}
		else {
			headTop = 0;
			headLeft = 0;
			headWidth = this.pWidth - this.border;
			headHeight = this.HeaderHeight;
		}
		
        /*UNDER HEADER - TOP MENU */
		if (this.sidesW>0) {
			uheadTop = this.HeaderHeight;
			
			uheadLeft = this.sidesW;
			uheadWidth = (this.pWidth - this.border) - (this.sidesW)*2;
			
			uheadHeight = this.UHeaderHeight;
		}
		else {
			uheadTop = this.HeaderHeight;
			uheadLeft = 0;
			uheadWidth = this.pWidth - this.border;
			uheadHeight = this.UHeaderHeight;
		}
        /* Left or Menu content */
		if (this.sidesW>0) {
			lconTop = this.HeaderHeight + this.UHeaderHeight + this.border;
			if (this.menuSide == 'right') {
				//lconLeft = tmpLLeft + parseInt(this.sidesW);
				lconLeft = (parseInt(this.sidesW))+rconWidth-(parseInt(this.sidesW)*2);
			}
			else {
				lconLeft = tmpLLeft + parseInt(this.sidesW);
			}
			lconWidth = this.sideWidth;
			lconHeight = this.contHeight;
		}
		else {
			lconTop = this.HeaderHeight + this.UHeaderHeight + this.border;
			lconLeft = tmpLLeft;
			lconWidth = this.sideWidth;
			lconHeight = this.contHeight;
		}
        /* Right or Main content */
		if (this.sidesW>0) {
			rconTop = this.HeaderHeight + this.UHeaderHeight + this.border;
			rconLeft = tmpRLeft+parseInt(this.sidesW);
			rconWidth  = rconWidth-(parseInt(this.sidesW)*2);
			rconHeight = this.contHeight;
		}
		else {
			rconTop = this.HeaderHeight + this.UHeaderHeight + this.border;
			rconLeft = tmpRLeft;
			//rconWidth   = rconWidth;
			rconHeight = this.contHeight;
		}
        /* Footer */
		if (this.sidesW>0) {
			footTop = this.HeaderHeight + this.border +
			this.UHeaderHeight +
			this.border +
			this.contHeight;
			footLeft = this.sidesW;
			footWidth = this.pWidth - this.border-(parseInt(this.sidesW)*2);
			footHeight = this.FooterHeight;
		}
		else {
			footTop = this.HeaderHeight + this.border +
			this.UHeaderHeight +
			this.border +
			this.contHeight;
			footLeft = 0;
			footWidth = this.pWidth - this.border;
			footHeight = this.FooterHeight;
		}
        
		if(this.sideHeight>0) {
			headTop  += ((this.sideHeight)/2);
			uheadTop += ((this.sideHeight)/2);
			lconTop  += ((this.sideHeight)/2);
			rconTop  += ((this.sideHeight)/2);
			footTop  += ((this.sideHeight)/2);
			
			if(this.menuSide=='right') {
				rconLeft -= ((this.sideHeight)/2);
			}
		}
		
        /*HEADER*/
        $(this.id_header).style.top    = headTop+'px';
        $(this.id_header).style.left   = headLeft+'px';
        $(this.id_header).style.width  = headWidth+'px';
        $(this.id_header).style.height = headHeight+'px';
        /*UNDER HEADER - TOP MENU */
        $(this.id_unHead).style.top    = uheadTop+'px';
        $(this.id_unHead).style.left   = uheadLeft+'px';
        $(this.id_unHead).style.width  = uheadWidth+'px';
        $(this.id_unHead).style.height = uheadHeight+'px';
        /* Left or Menu content */
        $(this.id_lcon).style.top    = lconTop+'px';
        $(this.id_lcon).style.left   = lconLeft+'px';
        $(this.id_lcon).style.width  = lconWidth+'px';
        $(this.id_lcon).style.height = lconHeight+'px';
        /* Right or Main content */
        $(this.id_rcon).style.top    = rconTop+'px';
        $(this.id_rcon).style.left   = rconLeft+'px';
        $(this.id_rcon).style.width  = rconWidth+'px';
        $(this.id_rcon).style.height = rconHeight+'px';
        /* Footer */
        $(this.id_foot).style.top    = footTop+'px';
        $(this.id_foot).style.left   = footLeft+'px';
        $(this.id_foot).style.width  = footWidth+'px';
        $(this.id_foot).style.height = footHeight+'px';
		
		if(this.menuDisable=='yes') {
			$(this.id_but_menu).style.visibility   = 'hidden';
			$(this.id_but_switch).style.visibility = 'hidden';
			$(this.id_but_coat).style.visibility   = 'hidden';
			this.hideMenu();
		}
    },
    
    SWITCH: function() {
        if(this.menuSide=='right') {
            //alert(myCO.get('myPRSwitch'));
            myCO.set('myPRSwitch', 'left', 1);
            this.now('left');
        }
        else if(this.menuSide=='left') {
            //alert(myCO.get('myPRSwitch'));
            myCO.set('myPRSwitch', 'right', 1);
            this.now('right'); 
        }
        else {
            alert('check what u write');        
        }
    },
    
    hideMenu: function() {
      if(this.menuShow=='hide') {
          this.menuShow='show';
          this.now(this.menuSide);
          $(this.id_lcon).style.visibility = 'visible';
      }
      else if (this.menuShow == 'show') {
	  	//alert("hidding!");
	  	this.menuShow = 'hide';
	  	$(this.id_lcon).style.visibility = 'hidden';
	  	
	  	if (this.sideHeight > 0) {
			var odst = (this.sidesW*2);
			//alert("odst: "+odst);
			$(this.id_rcon).style.left = this.sidesW+'px';
			$(this.id_rcon).style.width = (this.pWidth - this.border)-odst + 'px';
	  	}
	  	else {
	  		if ($(this.id_rcon).style.left != '0px') {
				$(this.id_rcon).style.left = '0px';
			}
	  		$(this.id_rcon).style.width = (this.pWidth - this.border) + 'px';
	  	}
	  }
	  else 
	  	alert('pageResize() wish undefined!');
    } 
}

