 // subp.js
// based on cbedi1.js

document.write("<link rel='stylesheet' type='text/css' href='../subcommon/cbedi1_abs.css'>");
document.write("<"+"script type='text/javascript' src='../lib/cbe_core.js'></"+"script>");
document.write("<"+"script type='text/javascript' src='../lib/cbe_event.js'></"+"script>");
document.write("<"+"script type='text/javascript' src='../lib/cbe_util.js'></"+"script>");
document.write("<"+"script type='text/javascript' src='../lib/cbe_clip.js'></"+"script>");
document.write("<"+"script type='text/javascript' src='../lib/cbe_debug.js'></"+"script>");
document.write("<"+"script type='text/javascript' src='../lib/cbe_slide.js'></"+"script>");
document.write("<"+"script type='text/javascript' src='../lib/cbe_slide2.js'></"+"script>");

var app;
var numArr;
var pageNote;
var maxNote;

var flamasterObjs;
var normalObjs;


var prevScroll;
var mrowkiTop;


function slubOnLoad(subpage) {
   flamasterObjs = new Array();
   normalObjs = new Array();
   prevScroll = 0;
  app = new cbeDI1(
    150,  // column 1 width
    0,   // vertical margin
    50,   // horizontal margin
    2,    // inner margin
    700,   // menu slide time
	subpage
  );

  window.cbe.addEventListener("resize", winResizeListener, false);
  window.cbe.addEventListener("scroll", winScrollListener, false);

  app.paint();
}

function winResizeListener() {
  app.paint();
}

function winScrollListener() {
	if (app.subpage) {
		scrollMrowki(app.tm);
		//repositionMrowki();
	}
  var
    mm = cbeGetElementById('fmenumarker').cbe,
    mu = cbeGetElementById('floatingmenu').cbe;
	
  mu.slideTo(mm.offsetLeft(), mm.offsetTop() + document.cbe.scrollTop(),
app.tm);
	
		
}

function scrollMrowki(tm) {
	//repositionMrowki();
	var dwie_mrowki_maleEl = cbeGetElementById('dwie_mrowki_male').cbe
	with (dwie_mrowki_maleEl) {
		if ((top() > cbeGetElementById('col2').cbe.height()) || (top() < window.cbe.height() - height() - 200)) 
			repositionMrowki();
		slideTo(offsetLeft(), offsetTop() + document.cbe.scrollTop() - prevScroll, tm / 2);
	}
	prevScroll = document.cbe.scrollTop();
}

function repositionMrowki() {
	var cw = cbeGetElementById('col2').cbe.width() - 20;
	var wh = window.cbe.height();
	
		with (dwie_mrowki_maleEl = cbeGetElementById('dwie_mrowki_male').cbe) {
			moveTo(cw - width() - 10,wh - height() - 50);
			show();
			mrowkiTop = top();
	}

}

function cbeDI1(col1Width, vMargin, hMargin, iMargin,
slideTime, subpage) {
  // Properties
  this.subpage = subpage;
  this.cw = col1Width;
  this.vm = vMargin;
  this.hm = hMargin;
  this.im = iMargin;
  this.tm = slideTime;
  // Methods
  this.paint = function() {
    var dy, mu, mm,
//      hd = cbeGetElementById('header').cbe,
//      ft = cbeGetElementById('footer').cbe,
      c1 = cbeGetElementById('col1').cbe,
      c2 = cbeGetElementById('col2').cbe,
      cm = cbeGetElementById('col2marker').cbe,
	  sw = 30,
      ch1 = document.cbe.height(),
	  ch2 = document.cbe.height()-this.vm-(2*this.im) - 20;
	  
    c1.resizeTo(this.cw, ch1);
    c2.resizeTo(document.cbe.width()-this.hm-c1.width() - this.im - sw, ch2);
    dy = cm.offsetTop() + cm.height() - ch2;
    if (dy > 0) { ch1 += dy; ch2 += dy; }
//    hd.resizeTo(document.cbe.width()-(2*this.hm), this.hh);
//    hd.moveTo(this.hm, this.vm);
//    hd.show();
    c1.resizeTo(this.cw, ch1);
    c1.moveTo(0, 10);
    c1.show();
    c2.resizeTo(document.cbe.width()-this.hm-c1.width() - this.im - sw, ch2);
    c2.moveTo(c1.width(), 10);
	c2.clip(-50,c2.width()+50,c2.height() + 1000,0);
    c2.show();
//    ft.resizeTo(hd.width(), this.fh);
//    ft.moveTo(this.hm, c1.top()+c1.height()+this.im);
//    ft.show();
    var
      mm = cbeGetElementById('fmenumarker').cbe,
      mu = cbeGetElementById('floatingmenu').cbe;
    mu.resizeTo(this.cw-(2*mm.offsetLeft()), mu.height());
    mu.moveTo(mm.offsetLeft(), -mu.height());
    mu.show();
    mu.slideTo(mm.offsetLeft(), mm.offsetTop() + document.cbe.scrollTop(),
this.tm);
	if (subpage) { 
		resizeSub();
		scrollMrowki();	
	}
	else { resizePrincipal(); }
  } // end paint() method

  // Constructor Code
  this.paint();

} // end class cbeDI1



function alignR(elem, x) {
	elem.left(x - elem.width());
}
function alignB(elem, y) {
	elem.top(y - elem.height());
}

/* both elements have to be class cbe */
function switchedImg(elemSrc, elemDst) {
	elemDst.moveTo(elemSrc.left(), elemSrc.top());
	
	elemSrc.zIndex(100)
	elemDst.zIndex(90);
	elemSrc.show();
	elemDst.hide();
	elemSrc.addEventListener("mouseOver", switchFlamOn);
	elemDst.addEventListener("mouseOut", switchFlamOff);
	
	flamasterObjs.push(elemDst);
	normalObjs.push(elemSrc);
}

function switchFlamOn(e) {
	allFlamOff();
	e.cbeCurrentTarget.hide();
	e.cbeCurrentTarget.nextSibling.show();
}

function allFlamOff() {
	for (var i = 0; i < flamasterObjs.length; i++) {
		flamasterObjs[i].hide();
	}
	 
	for (var i = 0; i < normalObjs.length; i++) {
		normalObjs[i].show();
	}
}

function switchFlamOff(e) {
	allFlamOff();
	//e.cbeCurrentTarget.hide();
	e.cbeCurrentTarget.previousSibling.show();
}	


/*TODO
function mouseNadListener(ELEMENT) {
	ELEMENT.hide()
	ELEMENT.nextSiblingId.show()
}
*/

function alignToLeft(elLeft, el, offset, y) {
	el.moveTo(elLeft.left() + elLeft.width() + offset, y);
	el.show();
}

function getTotalLen(el) {
	var total = 0;
	total += el.width();
	
	
	while (el.nextSibling != null) {
		el = el.nextSibling;
		total += el.width();
	}
	
	return total;
	
}

function resizeSub() {
	var ch = cbeGetElementById('col2').cbe.height() - 20;
	var cw = cbeGetElementById('col2').cbe.width() - 20;
	var wh = window.cbe.height();
	
	floor = Math.floor;
	
	
	
	with (textEl = cbeGetElementById('text').cbe) {
		// FIXME -- text setting
		//moveTo(20,0);
		show();
	}
	
	repositionMrowki();
	
	var totalLen = floor(getTotalLen(cbeGetElementById('foto').cbe) / 2);
	
	var elCount = 10;
	var hspace = floor((cw - totalLen) / (elCount + 1)); 
	var topY = 10;
	
	with (fotoEl = cbeGetElementById('foto').cbe) {
			moveTo(hspace,topY); 
			show();
	}
	switchedImg(fotoEl, cbeGetElementById('foto-f').cbe);
	
	with (fraszkiEl = cbeGetElementById('fraszki').cbe) {
			alignToLeft(fotoEl, fraszkiEl, hspace, topY);
	}
	switchedImg(fraszkiEl, cbeGetElementById('fraszki-f').cbe);
	
	with (prezentyEl = cbeGetElementById('prezenty').cbe) {
		alignToLeft(fraszkiEl, prezentyEl, hspace, topY);
	}
	switchedImg(prezentyEl, cbeGetElementById('prezenty-f').cbe);

	with (kwiatyEl = cbeGetElementById('kwiaty').cbe) {
		alignToLeft(prezentyEl, kwiatyEl, hspace, topY);
	}
	switchedImg(kwiatyEl, cbeGetElementById('kwiaty-f').cbe);
	
	
	with (koncertEl = cbeGetElementById('koncert').cbe) {
		alignToLeft(kwiatyEl, koncertEl, hspace, topY);
	}
	switchedImg(koncertEl, cbeGetElementById('koncert-f').cbe);

	with (podatkiEl = cbeGetElementById('podatki').cbe) {
		alignToLeft(koncertEl, podatkiEl, hspace, topY);
	}
	switchedImg(podatkiEl, cbeGetElementById('podatki-f').cbe);
	
	with (nieskEl = cbeGetElementById('niesk').cbe) {
		alignToLeft(podatkiEl, nieskEl, hspace, topY);
	}
	switchedImg(nieskEl, cbeGetElementById('niesk-f').cbe);

	with (kiedygdzieEl = cbeGetElementById('kiedygdzie').cbe) {
		alignToLeft(nieskEl, kiedygdzieEl, hspace, topY);
	}
	switchedImg(kiedygdzieEl, cbeGetElementById('kiedygdzie-f').cbe);
	
	with (aktualnosciEl = cbeGetElementById('aktualnosci').cbe) {
		alignToLeft(kiedygdzieEl, aktualnosciEl, hspace, topY);
	}
	switchedImg(aktualnosciEl, cbeGetElementById('aktualnosci-f').cbe);

	with (powrotEl = cbeGetElementById('powrot').cbe) {
		alignToLeft(aktualnosciEl, powrotEl, hspace, topY);
	}
	switchedImg(powrotEl, cbeGetElementById('powrot-f').cbe);
	
}


function resizePrincipal() {
	var cw = cbeGetElementById('col2').cbe.width() - 20;
	var ch = cbeGetElementById('col2').cbe.height() - 20;
	
	
	with (mrowkiEl = cbeGetElementById('mrowki').cbe) {
			moveTo(cw - width() - 50,ch - height() - 80);
			zIndex(50);
			show();
	}
	
	with (bierzemySlubEl = cbeGetElementById('bierzemy-slub').cbe) {
			moveTo(20,20);
			zIndex(50);
			show();
	}

	
	with (koncertEl = cbeGetElementById('koncert').cbe) {
			moveTo(cw - width() - 120,ch - height() - 50);
			show();
	}
	switchedImg(koncertEl, cbeGetElementById('koncert-f').cbe);
	
	with (podatkiEl = cbeGetElementById('podatki').cbe) {
			moveTo(cw - width() - 430,ch - height() - 140);
			show();
	}
	switchedImg(podatkiEl, cbeGetElementById('podatki-f').cbe);

	with (kwiatyEl = cbeGetElementById('kwiaty').cbe) {
			moveTo(cw - width() - 420,ch - height() - 235);
			show();
	}
	switchedImg(kwiatyEl, cbeGetElementById('kwiaty-f').cbe);

	with (aktualnosciEl = cbeGetElementById('aktualnosci').cbe) {
			moveTo(cw - width() - 80,ch - height() - 135);
			show();
	}
	switchedImg(aktualnosciEl, cbeGetElementById('aktualnosci-f').cbe);

	with (fotoEl = cbeGetElementById('foto').cbe) {
			moveTo(cw - width() - 30,ch - height() - 365); 
			show();
	}
	switchedImg(fotoEl, cbeGetElementById('foto-f').cbe);

	with (fraszkiEl = cbeGetElementById('fraszki').cbe) {
			moveTo(cw - width() - 190,ch - height() - 430);
			show();
	}
	switchedImg(fraszkiEl, cbeGetElementById('fraszki-f').cbe);

	with (kiedygdzieEl = cbeGetElementById('kiedygdzie').cbe) { // 240 340
			moveTo(cw - width() - 240,ch - height() - 340);
			//moveTo(0,0);
			show();
	}
	switchedImg(kiedygdzieEl, cbeGetElementById('kiedygdzie-f').cbe);

	with (prezentyEl = cbeGetElementById('prezenty').cbe) {
			moveTo(cw - width() - 35,ch - height() - 420);
			//moveTo(0,0);
			show();
	}
	switchedImg(prezentyEl, cbeGetElementById('prezenty-f').cbe);

	with (wielkaEl = cbeGetElementById('wielka').cbe) {
			moveTo(cw - width() - 20, 20);
			//moveTo(0,0);
			show();
	}
	switchedImg(wielkaEl, cbeGetElementById('wielka-f').cbe);

	with (nieskEl = cbeGetElementById('niesk').cbe) {
			moveTo(cw - width() - 330,ch - height() - 20);
			show();
	}
	switchedImg(nieskEl, cbeGetElementById('niesk-f').cbe);

}	