function PageSplitter() {
  this.wrapperId = 'splitter-wrapper-dont-use-for-styling';
  this.frameId = 'page-splitter-frame';
  this.frameHolderId = 'page-splitter-frame-holder';
};

PageSplitter.prototype.frameLoad = function() {
  this.resizeSplit();
};

PageSplitter.prototype.frameUnLoad = function() {
  if(!this.isKilling) {
    this.hideFrame();
    this.resizeSplit();
  }
};

PageSplitter.prototype.kill = function() {
  this.isKilling = true;
  this.hideFrame();
  this.restorePage();
  this.isKilling = false;
  this.isSplit = false;
};

PageSplitter.prototype.reloadPage = function() {
  this.kill();
  if(window.location.href.indexOf('?') != -1)
  window.location.href = window.location.href.replace(/\?.*/,'');
  else window.location.href +='?rt='+(new Date).getTime();
};

PageSplitter.prototype.redirect = function(url) {
  window.location.href = url;
};


PageSplitter.prototype.frameResize = function() {
  var frame = this.getFrame();
  var oDoc = get_iframe_doc(frame);
  frame.style.height = 0;
  var y;
  if(oDoc.documentElement || oDoc.body) {
    y = oDoc.documentElement.scrollHeight;
    if(!y) y = oDoc.body.offsetHeight;
    if ( isIE ) y+=15;
    var wY = window_size().y;
    if( y < 300 ) y = 300;
    if( wY < y ) y = wY;
    frame.style.height = y+'px';
  }
};

PageSplitter.prototype.hideFrame = function(fn) {
  this.getFrame().style.height = 0;
};

PageSplitter.prototype.splitPage = function(url) {
  if(!this.isSplit) {
    addEvent(this.getWrapper(),'click',preventDefault,false);
    this.isSplit = true;
    this.styleSplit();
    this.getFrame().src = url;
    this.resizeSplit();
    var me = this;
    window.onresize = function(){
      me.resizeSplit();};
  }
};

PageSplitter.prototype.resizeSplit = function() {
  this.frameResize();
  var frameHolderOffset = this.getFrameHolder().offsetHeight;
  var y = window_size().y;
  var divY = y - frameHolderOffset;
  if(divY<0) divY = 0;
  this.getWrapper().style.height = divY +'px';
};

PageSplitter.prototype.restorePage = function() {
  window.onresize = null;
  removeEvent(this.getWrapper(),'click',preventDefault,false);
  this.unStyleSplit();
  remove_el(this.getFrameHolder());
};

PageSplitter.prototype.styleSplit = function() {
  var scrollPos = getScrollPos();
  this.getWrapper().scrollTop = scrollPos.y;
  className.add(get_by_tag('HTML')[0],'split');
  this.getWrapper().style.height = window_size().y + 'px';
  this.getWrapper().scrollTop = scrollPos.y;
  this.getWrapper().scrollLeft = scrollPos.x;
  window.scrollTo(scrollPos.x, 0);
};

PageSplitter.prototype.unStyleSplit = function() {
  var scrollTop = this.getWrapper().scrollTop;
  className.kill(get_by_tag('HTML')[0],'split');
  this.getWrapper().style.height = '';
  window.scrollTo(getScrollPos().x, scrollTop);
};

//util methods
PageSplitter.prototype.getWrapper = function() {
  return get_by_id(this.wrapperId);
};

PageSplitter.prototype.getFrameHolder = function() {
  var frameHolder = get_by_id(this.frameHolderId);
  if(!frameHolder)
  frameHolder = make_el('DIV',{id:this.frameHolderId,
			       style:{width:'100%'}},document.body);
  return frameHolder;
};

PageSplitter.prototype.getFrame = function() {
  var frame = get_by_id(this.frameId);
  if(!frame)
  frame = make_el('IFRAME',{src:'about:blank',
			    id:this.frameId,
			    style:{width:'100%',
				   height:'300px'}},
		       this.getFrameHolder());
  return frame;
};

function checkForSplitter(e) {
  var srcEl=get_srcEl(e);
  while(!className.test(srcEl,'pagesplitter')
	&& (srcEl = srcEl.parentNode));
  if(srcEl) {
    preventDefault(e);
    addPageSplitter(srcEl.href);
    return false;
  };
  return true;
};


function addPageSplitter(url) 
{
  if(!window.pageSplitter)
    window.pageSplitter = new PageSplitter();

  if ( url.indexOf('?') != -1 )
	  window.pageSplitter.splitPage(url+'&js=pagesplitter');
  else 
	  window.pageSplitter.splitPage(url+'?js=pagesplitter');
	  
};
addEvent(document,'click',checkForSplitter,false);
