//////////////////// BOXES ////////////////////

var boxBorder = 6;

function Box (boxId, maskId, contentId, buttonId) {
	this.boxObj = document.getElementById(boxId);
	this.maskObj = document.getElementById(maskId);
	this.contentObj = document.getElementById(contentId);
	this.buttonObj = document.getElementById(buttonId);
	this.buttonObj.objAddress = this;

	this.exactHeight = this.maskObj.offsetHeight;
	if (this.exactHeight <= boxBorder) {
		this.action = 'hiding';
	} else {
		this.action = 'showing';
	}

	this.buttonObj.onclick = function () {
		if (this.objAddress.action == 'hiding') {
			this.objAddress.action = 'showing';
			this.objAddress.Show();
			this.objAddress.buttonObj.className = 'button opened';
			this.objAddress.buttonObj.blur();
		}
		else if (this.objAddress.action == 'showing') {
			this.objAddress.action = 'hiding';
			this.objAddress.Hide();
			this.objAddress.buttonObj.className = 'button closed';
			this.objAddress.buttonObj.blur();
		}
		return false;
	}
}

Box.prototype = {
	Report:function () {
		if (typeof setBoxCookie == 'function') {
			var cookieInfo = '';
			for (var i = 0; i <= (boxesArray.length - 1); i++) {
				cookieInfo += boxesArray[i] + '=' + ((document.getElementById(boxesArray[i] + '-mask').offsetHeight <= boxBorder) ? 'closed' : 'opened') + 'AND';
			}
			cookieInfo = cookieInfo.substring(0, cookieInfo.length - 3);
			setBoxCookie(cookieInfo);
		}
	},
	Show:function () {
		if (this.action != 'showing') return;
		if (this.exactHeight < (this.contentObj.offsetHeight - 1)) {
			this.exactHeight += (this.contentObj.offsetHeight - this.exactHeight) / 5;
			this.maskObj.style.height = this.exactHeight + 'px';

			var self = this;
			window.setTimeout(function () { self.Show () }, 20);
		} else {
			this.exactHeight = this.contentObj.offsetHeight;
			this.maskObj.style.height = this.exactHeight + 'px';
			this.Report();
		}
		this.SetAlpha(this.contentObj, 100 * this.maskObj.offsetHeight / this.contentObj.offsetHeight);
	},
	Hide:function () {
		if (this.action != 'hiding') return;
		if (this.exactHeight > 1) {
			this.exactHeight -= this.exactHeight / 5;
			this.maskObj.style.height = this.exactHeight + 'px';

			var self = this;
			window.setTimeout(function () { self.Hide () }, 20);
		} else {
			this.exactHeight = 0;
			this.maskObj.style.height = this.exactHeight + 'px';
			this.Report();
		}
		this.SetAlpha(this.contentObj, 100 * this.maskObj.offsetHeight / this.contentObj.offsetHeight);
	},
	SetAlpha:function (obj, alpha) {
		obj.style.filter = 'alpha(opacity: ' + alpha + ')';
		obj.style.MozOpacity = alpha / 100;
    		obj.style.KhtmlOpacity = alpha / 100;
		obj.style.opacity = alpha / 100;
	}
}

var maxListBoxItems = 6;

function ListBox (boxId, maskId, contentId, buttonId) {
	this.prototype = new Box (boxId, maskId, contentId, buttonId);

	this.listItemsBase = '';
	this.listItems = new Array ();
	this.itemStart = 0;
	this.itemCount = 0;
	this.itemNr = 9999;
	this.scrolling = 0;

	var self = this;
	this.leftButton = document.getElementById(boxId + '-left');
	this.leftButton.onclick = function () {
		self.Scroll('left');
		return false;
	}
	this.rightButton = document.getElementById(boxId + '-right');
	this.rightButton.onclick = function () {
		self.Scroll('right');
		return false;
	}
}

ListBox.prototype = {
	ContentLoaded:function () {
		this.itemCount = this.listItems.length;
	},
	RefreshControlButtons:function () {
		if (this.itemStart > 0) this.leftButton.style.display = 'block';
		else this.leftButton.style.display = 'none';
		if (this.itemStart < (this.itemNr - this.itemCount)) this.rightButton.style.display = 'block';
		else this.rightButton.style.display = 'none';

		this.scrolling = 0;
	},
	Scroll:function (direction) {
		if (this.scrolling) return;
		switch (direction) {
			case 'left': {
				if (this.itemStart >= maxListBoxItems) {
					this.itemStart -= 6;
				} else {
					this.itemStart = 0;
				}
			} break;
			case 'right': {
				this.itemStart += 6;
			} break;
		}
		//console.log(this.listItemsBase);
		switch (this.listItemsBase) {
			case 'latest-entry': getLatestEntries (this.itemStart, this.itemCount);
			break;
		}
	}
}

var boxesHolder = { }

var boxesArray = new Array('latest-entries', 'random-photo', 'latest-tracks', 'sister-sites', 'online-friends');
var listBoxesArray = new Array('latest-entries');
function setBoxes () {
	for (var i = 0; i <= (boxesArray.length - 1); i++) {
		if (!document.getElementById(boxesArray[i])) continue;
		var boxType = boxesArray[i].isInArray(listBoxesArray) ? 'ListBox' : 'Box';
		eval('var ' + boxesArray[i].replace('-', '') + ' = new ' + boxType + ' ("' + boxesArray[i] + '", "' + boxesArray[i] + '-mask' + '", "' + boxesArray[i] + '-content' + '", "' + boxesArray[i] + '-button' + '");');
		boxesHolder[boxesArray[i].replace('-', '')] = eval(boxesArray[i].replace('-', ''));
	}
}

//////////////////// LIST ITEMS ////////////////////

var itemRollSpeed = 1;

function ListItem (aId, titleId) {
	this.itemObj = document.getElementById(aId);
	this.aObj = document.getElementById(aId + 'A') || document.getElementById(aId);
	this.titleId = titleId;
	this.titleObj = document.getElementById(titleId + 'A') ||  document.getElementById(titleId);

	this.aObj.objAddress = this;
	this.aObj.onmouseover = function () {
		this.objAddress.StartRolling ();
	}
	this.aObj.onmouseout = function () {
		this.objAddress.StopRolling ();
	}
	this.slided = 0;
}

ListItem.prototype = {
	Slide:function () {
		if (!this.nextSlide) {
			this.nextSlide = -174;
			this.currentMargin = 0;
		}

		this.currentMargin += (this.nextSlide - this.currentMargin) / 4;
		this.itemObj.style.marginLeft = this.currentMargin + 'px';
		if (Math.round(this.currentMargin) != this.nextSlide) {
			var self = this;
			window.setTimeout(function () { self.Slide (); }, 33);
		} else {
			this.itemObj.style.marginLeft = this.nextSlide + 'px';
			this.nextSlide = null;

			var aObjA = document.getElementById(this.itemObj.id + 'A');
			var aObjB = document.getElementById(this.itemObj.id + 'B');
			aObjA.innerHTML = aObjB.innerHTML.replace(this.titleId + 'B', this.titleId + 'A');
			aObjA.href = aObjB.href;
			aObjA.title = aObjB.title;
			this.titleObj = document.getElementById(this.titleId + 'A');
			this.slided = 1;
			this.itemObj.style.marginLeft = '0px';

			if (document.getElementById(this.itemObj.id + 'B').lastItem) this.parentBox.RefreshControlButtons();
		}
	},
	StartRolling:function () {
		this.titleObj.hovered = 1;
		if (this.titleObj.rolling != 1) {
			this.titleObj.rolling = 1;
			this.slided = 0;
			var self = this;
			window.setTimeout(function () { self.Roll (1); }, 1000);
		}
	},
	StopRolling:function () {
		this.titleObj.hovered = 0;
	},
	Roll:function (start) {
		var currWidth = this.titleObj.offsetWidth;

		if (start && !this.titleObj.hovered) {
			this.titleObj.rolling = 0;
			return;
		}
		var elWidth = 164;
		if (currWidth > elWidth) {
			if (!this.titleObj.currX) this.titleObj.currX = 0;
			if (!this.titleObj.direction) this.titleObj.direction = -1;

			var delay = 50;
			var keepRolling = 1;
			switch (this.titleObj.direction) {
				case -1: {
					if ((this.titleObj.currX + (itemRollSpeed * this.titleObj.direction)) > (elWidth - currWidth)) this.titleObj.currX = this.titleObj.currX + (itemRollSpeed * this.titleObj.direction);
					else {
						this.titleObj.currX = elWidth - currWidth;
						this.titleObj.direction = 1;
						delay = 2000;
					}
				} break;
				case 1: {
					if ((this.titleObj.currX + (itemRollSpeed * this.titleObj.direction)) < 0) this.titleObj.currX = this.titleObj.currX + (itemRollSpeed * this.titleObj.direction);
					else {
						this.titleObj.currX = 0;
						this.titleObj.direction = -1;
						delay = 2000;
						if (!this.titleObj.hovered) keepRolling = 0;
					}
				} break;
			}
			this.titleObj.style.marginLeft = this.titleObj.currX + 'px';

			if (keepRolling) {
				var self = this;
				if (!this.slided) window.setTimeout(function () { self.Roll () }, delay);
				else {
					this.slided = 0;
					this.rolling = 0;
				}
			} else this.titleObj.rolling = 0;
		}
	}
}

var listItemHolder = { }

var listsArray = new Array('latest-entry', 'latest-track');
var listsFatherReferences = {'latest-entry': 'latest-entries'};

function setListItems () {
	for (var i = 0; i <= (listsArray.length - 1); i++) {
		var parentBox = boxesHolder[listsFatherReferences[listsArray[i]] ? listsFatherReferences[listsArray[i]].replace('-', '') : ''];
		if (parentBox) parentBox.listItemsBase = listsArray[i];
		var j = 1;
		while (document.getElementById(listsArray[i] + j)) {
			eval('var ' + (listsArray[i].replace('-', '') + j) + ' = new ListItem ("' + (listsArray[i] + j) + '", "' + listsArray[i] + '-title' + j + '");');
			listItemHolder[listsArray[i].replace('-', '') + j] = eval(listsArray[i].replace('-', '') + j);
			if (parentBox) {
				eval(listsArray[i].replace('-', '') + j + '.parentBox = parentBox');
				parentBox.listItems.push (eval(listsArray[i].replace('-', '') + j));
			}
			j++;
		}
		if (parentBox) parentBox.ContentLoaded ();
	}

}

window.onload = function () {
	setMenu ();
	setBoxes ();
	setListItems ();

  	if (document.getElementById('banner')) {
  		//var flashObj = new SWFObject('/blog/photoslice_banner.swf', "photoslicebanner", 468, 60, '9', '#263733');
  		//flashObj.write('banner');
  	}
}

window.onresize = function () {
	if (MenuObj) MenuObj.RollOver(MenuObj.initialPageName);
}



MenuObj = {
	isHovered:false,
	isSliding:false,
	isResizing:false,
	Init:function () {
		var pageId = this.initialPageName + '-button';
		this.aObj = document.getElementById(pageId);

		this.bgObj = document.getElementById('buttons-bg');
		this.bgObj.style.width = this.aObj.offsetWidth + 'px';
		this.bgObj.style.left = this.FindCoord(this.aObj)[0] + 'px';
		this.bgObj.style.top = this.FindCoord(this.aObj)[1] + 'px';
		this.bgObj.style.display = 'block';
	},
	RollOver:function (pageName, returnSlide) {
		if (!this.bgObj) this.Init();

		this.isHovered = true;
		if (!returnSlide) this.bgObj.className = 'focused';

		this.aObj = document.getElementById(pageName + '-button');

		this.currentX = this.bgObj.style.left.substring(0, this.bgObj.style.left.length - 2) * 1;
		this.targetX = this.FindCoord(this.aObj)[0];

		this.targetWidth = this.aObj.offsetWidth;
		this.currentWidth = this.bgObj.offsetWidth;
		var widthDif = this.targetWidth - this.currentWidth;
		this.widthSpeed = ((widthDif > 0) ? widthDif : -widthDif) / 10;

		if (!this.isSliding) {
			this.isSliding = true;
			this.NextSlide();
		}
		if (!this.isResizing) {
			this.isResizing = true;
			this.Resize();
		}
	},
	NextSlide:function () {
		var xDif = this.targetX - this.currentX;
		this.xSpeed = ((xDif > 0) ? xDif : -xDif) / 5;
		this.nextX = this.targetX + Math.round(xDif * 30 / 100);

		if (this.targetX != this.nextX) this.Slide();
		else {
			this.bgObj.style.left = this.targetX + 'px';
			this.bgObj.style.width = this.targetWidth + 'px';
			this.isSliding = false;
		}
	},
	Slide:function () {
		if (this.nextX == Math.round(this.currentX)) this.NextSlide();
		else {
			if (this.nextX > this.currentX) this.currentX += ((this.nextX - this.currentX > this.xSpeed) ? this.xSpeed : (this.nextX - this.currentX));
			else this.currentX -= ((this.currentX - this.nextX > this.xSpeed) ? this.xSpeed : (this.currentX - this.nextX));

			this.bgObj.style.left = this.currentX + 'px';

			var self = this;
			window.setTimeout(function () { self.Slide () }, 20);
		}
	},
	Resize:function () {
		if (this.targetWidth == Math.round(this.currentWidth)) this.isResizing = false;
		else {
			if (this.targetWidth > this.currentWidth) this.currentWidth += ((this.targetWidth - this.currentWidth > this.widthSpeed) ? this.widthSpeed : (this.targetWidth - this.currentWidth));
			else this.currentWidth -= ((this.currentWidth - this.targetWidth > this.widthSpeed) ? this.widthSpeed : (this.currentWidth - this.targetWidth));

			this.bgObj.style.width = this.currentWidth + 'px';

			var self = this;
			window.setTimeout(function () { self.Resize () }, 20);
		}
	},
	RollOut:function () {
		this.isHovered = false;

		var self = this;
		window.setTimeout(function () { self.RolledOut () }, 200);
	},
	RolledOut:function () {
		if (!this.isHovered) {
			this.bgObj.className = 'blurred';
			this.RollOver(this.initialPageName, 1);
		}
	},
	FindCoord:function (obj) {
		if (!obj) return false;
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
}

var buttonsArray = new Array('home', 'photo', 'about', 'contact', 'extra_button');
function setMenu () {
	for (var i = 0; i <= (buttonsArray.length - 1); i++) {
		var tempButton = document.getElementById(buttonsArray[i] + '-button');
		if (tempButton) {
			if (tempButton.className == "on") MenuObj.initialPageName = buttonsArray[i];
			tempButton.onmouseover = function () {
				if (MenuObj) MenuObj.RollOver(this.id.replace('-button', ''));
			}
			tempButton.onmouseout = function () {
				if (MenuObj) MenuObj.RollOut();
			}
		}
	}
	if (!MenuObj.initialPageName) MenuObj.initialPageName = buttonsArray[0];
	MenuObj.Init();
}
