function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=900,height=600,left = 240,top = 212');");
}

function popUpClick(URL,obj) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=900,height=600,left = 240,top = 212');");
	obj.href = "#";
	return false;
}


var serverScript = function() {  };
var SysMan = Class.create();
SysMan.prototype =  {
	registers: [],
	listeners: [],
	initialize: function() {
		},
	register: function(sName,fnObj) {
		this.registers[sName] = fnObj;
		alert("registered");
	},
	executeListeners: function() {
	
	},
	execute: function(sName,aArgs) {
		var fn = this.registers[sName];
		fn(aArgs);
	},
	test: function() {
		alert("sys manager");
	}	
};

sysManager = new SysMan();

historyChange = function(newLocation, historyData) {
	alert("back");
}

function loadSite() {
	Event.observe(window,"load",construct);
// start dhtmlHistory class

	dhtmlHistory.initialize();
	dhtmlHistory.addListener(function() { /*alert("beep");*/ });
}

function construct() { 
	if(isIE) {
		alert("Your Browser might not be compatible,\rPlease use FireFox or Internet Explorer 7");
		pngfix($("leftImage"));
		pngfix($("rightImage"));	
		pngfix($("centerImage"));	
		pngfix($("logoImage"));	
		pngfix($("badVistaImage"));
	
		for(var i = 0; i < document.images.length; i++) {
			pngfix(document.images[i]);
		}
	}
	var gHandler = new GlobalHandler();
	Ajax.Responders.register(gHandler);
	var menu = new Menu();

	Effect.toggle("siteLoading", "appear");
	eff = new Effect.Fade('siteLoading', { from: 1, to: 0, duration: 0.5  });
	
	$("site").style.visibility = "visible";	
//	menu.getSingleDialog().setText("Bla bla bla this is a test");
//	menu.getSingle().moveIn();
//	menu.showSingleDialog();

}

var Menu = Class.create();
Menu.prototype = {
	ver: 0.5,
	buttons: [
		["main","double","main.php?ajax=1&left=1","main.php?ajax=1&right=1"], 
		["galleries", "single", "galleries.php?ajax=1"], 
		["register", "double", "register.php?ajax=1&left=1","register.php?ajax=1&right=1"], 
		["crew", "single" ,"crew.php?ajax=1" ], 
		["playlists", "single", "playlists.php?ajax=1&center=1"], 
		["events", "double", "events.php?ajax=1&left=1", "events.php?ajax=1&right=1" ],
		["forum", "none", "http://blog.dopamine.co.il"]
	],
//["forum", "single", "forum.php"], 
	visibleDialog: -1,
	dialogs: [],
	activeButton: null,
	animation: false,
	logo: "down",
	logoNode: null,
	type: "menu",
	reduced: false,
	initialize: function() {
		this.dialogs.push(new Dialog("centerBox", 500, 0, false));
		this.dialogs.push(new DoubleDialog("leftBox","rightBox", false));
		
		for(var i = 0; i < this.buttons.length; i++) {
			buttonId = this.buttons[i][0] + "Link";
			oMenuItem = $(buttonId);
//			oMenuItem = $(this.buttons[i][0]);
			Event.observe(oMenuItem, "click", this.onClick.bind(this));
		}
/*		
		$('forumLink').onclick = function() {
			popUp("http://blog.dopamine.co.il/");
			return false;
		}
*/
		this.dialogs[0].oMenu = this;
		this.dialogs[1].oMenu = this;
		this.logoNode = $('logo');
	},
	getSingleDialog: function() {
		return this.dialogs[0];
	},
	getDoubleDialog: function() {
		return this.dialogs[1];	
	},
	getActive: function() {
//		alert(this.dialogs.length);
		return this.dialogs[this.visibleDialog];
	},
	getActiveType: function() {
		if(this.visibleDialog===0) { 
			return "single";
		} else if(this.visibleDialog==1) { 
			return "double"; 
		} else { return "none"; }
	},
	getInactive: function() {
		sActive = this.getActiveType();
		if(sActive=="single") {
			return this.dialogs[1];
		} else { return this.dialogs[0]; }
	},
	setActiveType: function(sType) {
		if(sType == "single") {
			this.visibleDialog = 0;
		} else if(sType == "double") { 
			this.visibleDialog = 1;
		} else { this.visibleDialog = -1; }
	},
	getSettings: function(sId) {
		for(var i = 0; i < this.buttons.length; i++) {
			if(this.buttons[i][0]==sId) {
				return this.buttons[i];
			}
		}
		return null;
	},
	showDoubleDialog: function(oDomNodeLeft, oDomNodeRight) {
		sActive = this.getActiveType();
		oDouble = this.getDoubleDialog();
		if(!this.animation) {
			this.animation = true; 
			if(sActive == "single") {
				oSingle = this.getSingleDialog();
				oSingle.afterFinish = function() {
					this.setActiveType("none");
					this.animation = false;
					this.showDoubleDialog(oDomNodeLeft, oDomNodeRight);
				}.bind(this);
				oSingle.moveOut();
			} else if(sActive == "none") {
				this.setActiveType("double");
				oDouble.getLeftDialog().setText(oDomNodeLeft.innerHTML);
				oDouble.getRightDialog().setText(oDomNodeRight.innerHTML);
				oDouble.afterFinish = function() { 
					this.animation = false;
					this.getDoubleDialog().leftDialog.visible = true;
					this.getDoubleDialog().rightDialog.visible = true;
					this.getSingleDialog().visible = false;
					try {
						serverScript(this);
						serverScript = function() { };
					} catch(e) { }
				}.bind(this);
				oDouble.moveIn();
			} else { 
				this.animation = false;	
				oDouble.swapText(oDomNodeLeft.innerHTML, oDomNodeRight.innerHTML, serverScript);
//				oDouble.swapText(oDomNodeLeft.innerHTML, oDomNodeRight.innerHTML, Prototype.emptyFunc);
			}
		}
	},
	showSingleDialog: function(oDomNode) {
		sActive = this.getActiveType();
		oSingle = this.getSingleDialog();
		if(!this.animation) {
			this.animation = true;
			if(sActive == "double") {
				oDouble = this.getDoubleDialog();
				oDouble.afterFinish = function() { 
					this.setActiveType("none"); 
					this.animation = false;
					this.showSingleDialog(oDomNode);
				}.bind(this);
				oDouble.moveOut();
			} else if(sActive=="none") {
				this.setActiveType("single");
				oSingle.afterFinish = function() { 
					this.animation = false; 
					this.getDoubleDialog().leftDialog.visible = false;
					this.getDoubleDialog().rightDialog.visible = false;
					this.getSingleDialog().visible = true;
					try {					
						serverScript(this);
						serverScript = function() { };						
					} catch(e) { }		
				}.bind(this);
				oSingle.setText(oDomNode.innerHTML);
				oSingle.moveIn();
			} else { 
				this.animation = false;
				oSingle.swapText(oDomNode.innerHTML, serverScript);
			} 
		}
	},
	onClick: function(evt) {
		Event.stop(evt);
		oClicked = Event.element(evt).parentNode.parentNode;
		if(this.logo=="down") {
			this.logo = "up";
			eff = new Effect.MoveBy(this.logoNode, -240, 0, {
				afterFinish: function() {
					this.fireButton(oClicked);
				}.bind(this)
			});
		} else {
			this.fireButton(oClicked);
		}
		return false;
	},
	invokeLink: function(aSettings) {
		if( this.currentInvoke === undefined || this.currentInvoke === null) {
			// init invocation !
			sType = "page"+aSettings[0]+"Pv";
			dhtmlHistory.add(sType, aSettings);
			this.currentInvokeData = []; // empty string
			this.currentInvoke = aSettings[0];
			this.currentInvokeCount = 2;
			this.currentInvokeMax = aSettings.length - 1;
			this.invokeLink(aSettings);
		} else if(this.currentInvokeCount <= this.currentInvokeMax) {	
			var url = aSettings[this.currentInvokeCount];
			this.currentInvokeCount++;
			if($("0currentInvokeDiv0")) {
				$("0currentInvokeDiv0").parentNode.removeChild($("0currentInvokeDiv0"));
			}
			var oDomNode = Builder.node("div",{ id: "0currentInvokeDiv0"});
			Element.setStyle(oDomNode, { visibility: "hidden", width:"1px", height:"1px", position:"absolute",top: "1px", left:"1px" });
			document.body.appendChild(oDomNode,document.body.firstChild);
			
			this.currentInvokeData.push(oDomNode);
			$("siteLoadingText").innerHTML = "Loading " + url + "...";

			var eff = new Ajax.Updater(oDomNode, url, {
				evalScripts: true,
				method: "get",
				insertion: Insertion.Top,
				onComplete: function(t) {
					xtr = t.responseText;
					xtr.evalScripts();
					this.invokeLink(aSettings);
				}.bind(this)
			});
		} else {
			this.currentInvoke = null;
			var oRight;
			var oLeft;
			this.activeButton = aSettings[0];
			if(aSettings[1]=="double") {
				oRight = this.currentInvokeData.pop();
				oLeft = this.currentInvokeData.pop();
				this.showDoubleDialog(oLeft, oRight);
			} else if(aSettings[1] == "single") { 

				oRight = this.currentInvokeData[0];
				this.showSingleDialog(oRight);
			} else if(aSettings[1] == "left") {
				oLeft = this.currentInvokeData[0];
				this.getDoubleDialog().leftDialog.swapText(oLeft.innerHTML);
			} else if(aSettings[1] == "right") {
				oRight = this.currentInvokeData[0];
				this.getDoubleDialog().rightDialog.swapText(oRight.innerHTML);
			} else if(aSettings[1] == "center") {
				oLeft = this.currentInvokeData[0];
				this.getSingleDialog().swapText(oLeft.innerHTML);
			}			
			// clean up loader domNode
			this.currentInvokeData = [];
			if($("0currentInvokeDiv0")) {
				$("0currentInvokeDiv0").parentNode.removeChild($("0currentInvokeDiv0"));
			}
		}
	},
	updateDisplay: function(sType, aContent) {
	
	},
	fireButton: function(oElement) {
		var aSettings = this.getSettings(oElement.id);
		if(aSettings!==null && this.activeButton !== aSettings[0] && !this.animation) {
			this.activeButton = aSettings[0];
			if(aSettings[1]=="none") {
				if(this.getActiveType()!="none") {
//					this.getActive().moveOut();
				}
				this.reduceContent(aSettings[2]);
				// now lets make the body move up...
			} else {
				if(this.reduced) {
					this.expandContent();
				}
				this.invokeLink(aSettings);
			}
		}
	},
	reduceContent: function(address) {
		if(!this.reduced) {
			if($('__external')!==null) {
				this.unloadExternal();
			}
			new Effect.Morph('contentArea',{style:{height:'140px'},
				afterFinish: function() {
					this.reduced = true;
					this.loadExternal(address);
				}.bind(this)
			});
		}
	},
	expandContent: function() {
		if(this.reduced) {
			this.unloadExternal();
			new Effect.Morph('contentArea',{style:{height:'565px'},
				afterFinish: function() {
					this.reduced = false;
				}.bind(this)
			});
		}
	},
	loadExternal: function(address) {
		Element.setStyle('site', { height: '170px'});
		oNode = Builder.node('iframe', {'src':address, 'id':'__external', 'scroller': 'none'} );
//		$('site').appendChild(oNode);
		$('site').parentNode.insertBefore(oNode, $('normalLinks'));
//		alert(oNode.firstChild.tagName);
	},
	unloadExternal: function() {
		new Effect.Fade('__external');
		$('__external').remove();
		Element.setStyle('site', { height: '599px'});
	}
};

var Dialog = Class.create();
Dialog.prototype = {
	domNode: null,
	viewTop: 0,
	viewLeft: 0,
	visible: false,
	animation: false,
	scrollStep: 200,
	scrollTop: -387,
	type: "single",
	oScroller: null, 
	initialize: function(node, moveY, moveX, startVisible) {
		this.domNode = $(node);
		var fChild = this.domNode.firstChild;
		this.textNode = null;
		this.scrollerNode = null;
		while( fChild.nextSibling !== null) {
			fChild = fChild.nextSibling;
			switch(fChild.className) {
				case "scroller":
					this.scrollerNode = fChild;
					break;
				case "textBoxWrapper":
					this.textNode = fChild;
					break;
			}
		}
		// This is the Scroller
		//Event.observe(this.scrollerNode.firstChild,"click", this.scrollUp.bind(this));
		//Event.observe(this.scrollerNode.lastChild,"click", this.scrollDown.bind(this));	
		//alert("init scroller");
		//alert(this.scrollerNode.firstChild.nextSibling.innerHTML)
		this.oScroller = new Control.Slider(this.scrollerNode.firstChild.nextSibling, this.scrollerNode, {
			axis: "vertical",
			onchange: function(value) {
				alert('change');
			},
			onslide: function(value) {
				alert('slide');
			}
		});
		this.oScroller.options.onChange = function(value) {
			//activeprofile.height = value;
			//updatebankdescription();
			//setresizedesc();
			currentHeight = parseInt(Element.getHeight(this.textNode.firstChild),10)+this.scrollTop ;
			moveBySize = (-1)*((value * currentHeight).toFixed());
			if(moveBySize>0) return;
			Element.setStyle(this.getContentPane().firstChild.firstChild, {top: moveBySize + "px", position: "relative" })
		}.bind(this);

		this.oScroller.options.onSlide = function(value) {
			currentHeight = parseInt(Element.getHeight(this.textNode.firstChild),10) +this.scrollTop;
			moveBySize = (-1)*((value * currentHeight).toFixed());
			if(moveBySize>0) return;
			Element.setStyle(this.getContentPane().firstChild.firstChild, {top: moveBySize + "px", position: "relative" })
//			$('debug').innerHTML = "OnSlide: " + value + '<br />' + parseInt(Element.getHeight(this.textNode.firstChild),10) + 
//				'<br />class: ' + this.textNode.firstChild.firstChild.className+'<br />'+moveBySize+'<br />' +this.getContentPane().firstChild.className;				
		}.bind(this);
		this.viewTop = 	parseInt(Element.getStyle(this.domNode, "top"),10);
		this.viewLeft = parseInt(Element.getStyle(this.domNode, "left"),10);
		this.destX = moveX;
		this.destY = moveY;
		var awayTop = (this.viewTop + moveY) + 'px';
		var awayLeft = (this.viewLeft + moveX) + 'px';
		if(!startVisible) {
			Element.setStyle(this.domNode, { top: awayTop, left: awayLeft }); // hide it !
			this.visible = false;
		} else { this.visible = true; }
	}, 
	getContentPane: function() {
		return this.textNode;
	},
	getScrollerPane: function() {
		return this.scrollerNode;
	},
	moveMe: function(sign) {
		Element.setStyle(this.getContentPane(), { top : this.scrollTop });
		var currentTop = parseInt(Element.getStyle(this.domNode, "top"),10);
		var currentLeft = parseInt(Element.getStyle(this.domNode, "top"),10);
		if(this.viewTop == currentTop && this.viewLeft == currentLeft && this.animation) { return false; }
		this.animation = true;
		var eff = new Effect.MoveBy(this.domNode, (sign)*this.destY, (sign)*this.destX, {
			afterFinish: function() {
				this.animation = false;
				this._afterFinish();
			}.bind(this)
		}); 
		return true;
	},
	moveOut: function() { 
		if(this.visible) {
			this.visible = false;
			this.moveMe(1);
		} else { return false; } 
	},
	moveIn: function() {
		if(!this.visible) {
			this.visible = true;
			this.moveMe(-1);
		} else { return false; } 
	},
	setText: function(sContent) {
		this.textNode.firstChild.firstChild.innerHTML = sContent; // triple coated :( IE suck
	},
	funcsLookup: function() {
	},
	_finishSwap: function(htmlString, callBack) {
		if(this.animation) {
			this.rewind();
			this.setText(htmlString);
			Element.setStyle(this.getContentPane(), { top : this.scrollTop });
			eff = new Effect.SlideDown(this.textNode, {
				afterFinish: function() {
					this.animation = false;
					this._afterFinish(); 
					try { 
						callBack(this); 
						callBack = function() { };
						serverScript = function() { };
					} catch(e) { }
				}.bind(this)
			});
		}
	},
	swapText: function(htmlString, callBack) {
		if(this.visible && !this.animation) {
			this.animation = true;
			eff = new Effect.SlideUp(this.textNode, {
				afterFinish: function() {
					this._finishSwap(htmlString, callBack);
				}.bind(this)
			});
			
			return true;
		} else { return false; }
	},
	afterFinish: function() { },
	_afterFinish: function() {
		this.afterFinish();
		this.afterFinish = function() { };
	},
	scrollUp: function() {
		this.scroll(1);
	
	},
	scrollDown: function() {
		this.scroll(-1);	
	},
	scroll: function(dir) {
		if(!this.animation) {
			var currentTop = parseInt(Element.getStyle(this.textNode, 'top'),10);
			var height = parseInt(Element.getHeight(this.textNode.firstChild),10) + this.scrollTop;
			var step = this.scrollStep*dir;
			var scale = (currentTop - this.scrollTop)*-1;
			var scaleMax = (height + this.scrollTop);
			//alert(scaleMax + "\r\n Height is: " + height + "\r\n CurrentTop: " + currentTop);
			//var scaleMax = height;
			var scaleNext = scale + step*-1; // reverse the step for virtual calculation
 			if (scaleNext < 0 && scale === 0 || scaleNext > scaleMax && scale == scaleMax) {
				this.animation = false;
				return false;
				// TODO: something in the upper clause doesnt work right !
			} else if(scaleNext > scaleMax && scale < scaleMax) {
				// lets change the step... ref to height
				step = scale - scaleMax;
				//getting close to the bottom
			} else if(scaleNext < 0 && scale > 0) {
				step = this.scrollTop - currentTop;
				// getting close to the top
				// lets change the step... ref to 0
			}
			this.animation = true;
			var eff = new Effect.MoveBy(this.getContentPane(), step ,0, {
				afterFinish: function() {
					this.animation = false;
				}.bind(this)
			});
		}
	},
	rewind: function() {
		old = Element.getStyle(this.getContentPane(), 'top');
		Element.setStyle(this.getContentPane(), { top : this.scrollTop+"px" });
//		alert(this.scrollTop + " : " + old + " -- " + Element.getStyle(this.getContentPane(), 'top'));;
	}
};

var DoubleDialog = Class.create();
DoubleDialog.prototype = {
	visible: false,
	animation: false,
	type: "double",
	initialize: function(nodeLeft, nodeRight, startVisible) {
		this.leftDialog = new Dialog(nodeLeft,0, -500, startVisible);
		this.rightDialog = new Dialog(nodeRight, 0 , 300, startVisible);
		this.visible = startVisible;

	}, 
	getLeftDialog: function() {
		return this.leftDialog;
	},
	getRightDialog: function() {
		return this.rightDialog;
	},
	moveMe: function(sign) {
		if(this.animation) { return false; }	
		this.getLeftDialog().rewind();
		this.getRightDialog().rewind();				
		this.animation = true;
		parr = new Effect.Parallel([
			new Effect.MoveBy(this.leftDialog.domNode, (sign)*this.leftDialog.destY, (sign)*this.leftDialog.destX, {
				afterFinish: function() {
					this.leftAfterFinish();
				}.bind(this)
			}),
			new Effect.MoveBy(this.rightDialog.domNode, (sign)*this.rightDialog.destY, (sign)*this.rightDialog.destX, {
				afterFinish: function() {
					this.rightAfterFinish();
				}.bind(this)
			})
		], {
			afterFinish: function() {
				this.animation = false;
				this._afterFinish();
			}.bind(this)
		});
	},
	moveOut: function() { 
		if(this.visible) {
			this.visible = false;
			this.moveMe(1);
		} else { return false; }
	},
	moveIn: function() {
		if(!this.visible) {
			this.visible = true;
			this.moveMe(-1);
		} else { return false; }
	},
	_finishSwap: function(htmlStringLeft, htmlStringRight , callBack) {
		if(this.animation) {
			this.getLeftDialog().setText(htmlStringLeft);
			this.getRightDialog().setText(htmlStringRight);
			this.getLeftDialog().rewind();
			this.getRightDialog().rewind();	
			var eff = new Effect.Parallel([ 
				new Effect.SlideDown(this.leftDialog.textNode, {sync: true}),
				new Effect.SlideDown(this.rightDialog.textNode, {sync:true})
			], {
				afterFinish: function() {
					this.animation = false;
					try { 
						callBack(this); 	
						callBack = function() { };
						serverScript = function() { };
					} catch(e) { }
				}.bind(this)
			});			
			return true;			
		}
	},
	swapText: function(htmlStringLeft, htmlStringRight, callBack) {
		if(this.visible && !this.animation) {				
			this.animation = true;
			var eff = new Effect.Parallel([ 
				new Effect.SlideUp(this.leftDialog.textNode, {sync: true}),
				new Effect.SlideUp(this.rightDialog.textNode, {sync:true})
			], {
				afterFinish: function() {
					this._finishSwap(htmlStringLeft, htmlStringRight, callBack);
				}.bind(this)
			});
			return true;
		} else { return false; }
	},
	_afterFinish: function() {
		this.afterFinish();
		this.afterFinish = function() { };
	},
	afterFinish: function() { },
	rightAfterFinish: function() { },
	leftAfterFinish: function() { }
};

var GlobalHandler = Class.create();
GlobalHandler.prototype = {
	initialize: function() { },
	onCreate: function() {
		//alert("controller created");
		Element.show("siteLoading");
		$("siteLoading").style.display = "block";
		$("siteLoading").style.visibility = "visible";
		$("siteLoading").style.zIndex = 1000;
		$("siteLoading").style.position = "absolute";
		$("siteLoading").style.width = "350px";
		Element.show("siteLoading"); 
	},
	onComplete: function() {
		if(Ajax.activeRequestCount === 0){
			eff = new Effect.Fade('siteLoading', { from: 1, to: 0, duration: 0.5  });
		}
	}
};

