window.addEvent("domready", function(){
	fInitSubmenu();
	//
	if ($("loginDownload")) initFormLogin();
	if ($("changePassword")) initFormChangePassword();
	if ($("uploadForm")) initFormUpload();
	if ($("forgotPassword")) initFormForgotPassword();
	if ($("CreaterFolderForm")) initFormCreaterFolderForm();
	
	initMainNav();		//create main navigator
	initPrint();		//create print button
	initTreeMenu();
});
//
//
var initMainNav = function(){
	if (!$("mainNav")) return;
	var d = document;
	/*$$("ul#mainNav li").each(function(li, i){
		var a = li.getFirst();
		var ul = a.getNext();
		var isFirstLevel = (li.getParent() == $("mainNav"))
		if (isFirstLevel) {
			li.addEvent("mouseenter", function(e){
				var u = li.getFirst().getNext();
				if (u && u.hasClass("selected")) d.s = u;
				if (d.s) d.s.setStyle("visibility", "hidden");
				if (ul) ul.setStyle("visibility", "visible");
				a.setStyle("background", "#999");
			});
			li.addEvent("mouseleave", function(e){
				var u = li.getFirst().getNext();
				if (u && u.hasClass("selected")) d.s = u;
				if (ul) ul.setStyle("visibility", "hidden");
				if (d.s) d.s.setStyle("visibility", "visible");
				if (!a.hasClass("selected")) a.setStyle("background", "");
			});
		}
	});*/
	
	//
	$$("ul#mainNav a").each(function(a, i){
		a.addEvent("click", function(e){
			// reset selected items
			$$("ul#mainNav a").each(function(a, i){
				a.removeClass("selected");
				a.setStyle("background", "");
			});
			$$("ul#mainNav ul").each(function(ul, i){
				ul.removeClass("selected");
			});
			// set selected items
			var pUL = a.getParent().getParent();
			var sUL = (pUL && pUL == $("mainNav")) ? a.getNext() : pUL;
			var sPA = (pUL && pUL == $("mainNav")) ? a : pUL.getPrevious();
			var sSA = (pUL && pUL == $("mainNav") && sUL) ? sUL.getFirst().getFirst() : a;
			//
			if (sUL) sUL.addClass("selected");
			if (sPA) sPA.addClass("selected");
			if (sSA) sSA.addClass("selected");
		});
	});
}
//
//
var initPrint = function(){
	if ($$("p.print").length != 1) return;
	$$("p.print")[0].addEvent("click", function(e){
		new Event(e).stop();
		try {
			window.print();
		} catch(e) {
			return;
		}
	});
}
//
//
var initTreeMenu = function(){
	//
	// get all trees
	var tree = $$("ul.tree");
	if (tree.length == 0) return;
	document.treeMenuTweening = false;
	//
	tree.each(function(ul){
		//
		ul.getElements("ul").each(function(subUL){
			new Element("a").setProperty("href", "javascript:void();").addClass("node").setText("[+/-]").injectBefore(subUL.getPrevious());
			if (subUL.getElements("ul").length == 0) {
				subUL.addClass("treeTop");
			}
		});
		//
		ul.getElements("a").each(function(a){
			if (a.hasClass("node")) {
				//
				// set click behaviour to open sub
				var nextUL = a.getNext().getNext();
				if (nextUL) {
					// define properties
					//a._ul = nextUL.setStyle("overflow", "hidden");
						a._ul = nextUL.setStyles({
						overflow: "hidden",
						height: 0
					});
					a._opened = false;
					a._fx = new Fx.Style(nextUL, "height", {
						duration: 400,
						transition: Fx.Transitions.linear,
						onComplete: function(){
							document.treeMenuTweening = false;
						}
					}).set(0);
					//
					a.getParent().addClass("subOff");
					// click here
					a.addEvent("click", function(e){
						new Event(e).stop();						
						var thisButton = $(this);
						//
						// if something is moving then stop
						if (document.treeMenuTweening) return;
						document.treeMenuTweening = true;
						//
						// expand sub & parent height
						if (thisButton._fx) {
							var newHeight = getElementHeight(thisButton._ul);
							thisButton._fx.stop().start((thisButton._opened == false) ? newHeight : 0);
							thisButton._opened = !thisButton._opened;
							getNestedParent(thisButton, ul).each(function(nestedButton){
								if (nestedButton._fx) {
									nestedButton._fx.stop().start(nestedButton.getNext().getNext().getCoordinates().height+((thisButton._opened == true) ? newHeight : -newHeight));
								}
							});
							//
							// apply style for list tag & button links
							thisButton.toggleClass("nodeShow");
						} // end if
					}); // end event
				}
			}
			//
			//
		}); // end each a
		ul.getElements("a").each(function(a){
			if (a.hasClass("node") && a.getNext().hasClass("selected")) {
				a._opened = true;
				a._fx.set(getElementHeight(a._ul));
				a.toggleClass("nodeShow");
				getNestedParent(a, ul).each(function(nestedButton){
					nestedButton._opened = true;
					nestedButton._fx.set(getElementHeight(nestedButton._ul));
					nestedButton.toggleClass("nodeShow");
				});
			}
			//
			if (a.hasClass("selected") && !a.getPrevious()) {
				getNestedParent(a, ul).each(function(nestedButton){
					nestedButton._opened = true;
					nestedButton._fx.set(getElementHeight(nestedButton._ul));
					nestedButton.toggleClass("nodeShow");
				});
			}
		});
		//
	}); // end each ul tree
	//
	// get element height by its default natural height
	function getElementHeight(el) {
		var element = $(el);
		if (!element) return 0;
		//
		var oldHeight = element.getCoordinates().height;
		// open its natual height
		var newHeight = element.setStyles({
			height: "auto",
			visibility: "hidden",
			position: "absolute"
		}).getCoordinates().height;
		// reset
		element.setStyles({
			height: oldHeight,
			visibility: "visible",
			position: "static"
		});
		//
		return newHeight;
	}
	//
	//
	// get all the parents, grandparents links of the current link
	function getNestedParent(element, topElement) {
		var parentArray = new Array();
		var nestedParent = $(element).getParent().getParent();
		while (nestedParent && nestedParent != $(topElement) && nestedParent.getPrevious() && nestedParent.getPrevious().getPrevious()) {
			parentArray.push(nestedParent.getPrevious().getPrevious());
			nestedParent = nestedParent.getParent().getParent();
		}
		return $A(parentArray);
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
var initFormLogin = function() {
	var alertXML = new fLoadXML("login.xml", true);
	alertXML.fSuccess = function(){
		new validerAlert(
			"formLoginAlertBox", 
			"loginDownload", 
			"submitBtn", 
			this.aXml['form'][0].field, 
			{
				font: "11px/11px Tahoma",
				txtColor: "#666",
				appTxtColor: "#f00",
				bgColor: "#FFFFFF", 
				bgImage: "templates/images/_alert_bar.gif", 
				closeImage: "templates/images/_alert_close.gif",
				border: "1px solid #ccc",
				width: "140px"
			}
		);
	}
}
//
//
var initFormChangePassword = function() {
	var alertXML = new fLoadXML("changepassword.xml", true);
	alertXML.fSuccess = function(){
		new validerAlert(
			"formChangePasswordAlertBox", 
			"changePassword", 
			"submitBtn", 
			this.aXml['form'][0].field, 
			{
				font: "11px/11px Tahoma",
				txtColor: "#666",
				appTxtColor: "#f00",
				bgColor: "#FFFFFF", 
				bgImage: "templates/images/_alert_bar.gif", 
				closeImage: "templates/images/_alert_close.gif",
				border: "1px solid #ccc",
				width: "116px"
			}
		);
	}
}
//
//
var initFormUpload = function() {
	var alertXML = new fLoadXML("upload.xml", true);
	alertXML.fSuccess = function(){
		new validerAlert(
			"formUploadAlertBox", 
			"uploadForm", 
			"btupload", 
			this.aXml['form'][0].field, 
			{
				font: "11px/11px Tahoma",
				txtColor: "#666",
				appTxtColor: "#f00",
				bgColor: "#FFFFFF", 
				bgImage: "templates/images/_alert_bar.gif", 
				closeImage: "templates/images/_alert_close.gif",
				border: "1px solid #ccc",
				width: "116px"
			}
		);
	}
}
//
//
var initFormForgotPassword = function() {
	var alertXML = new fLoadXML("forgotpassword.xml", true);
	alertXML.fSuccess = function(){
		new validerAlert(
			"formForgotPasswordAlertBox", 
			"forgotPassword", 
			"submitBtn", 
			this.aXml['form'][0].field, 
			{
				font: "11px/11px Tahoma",
				txtColor: "#666",
				appTxtColor: "#f00",
				bgColor: "#FFFFFF", 
				bgImage: "templates/images/_alert_bar.gif", 
				closeImage: "templates/images/_alert_close.gif",
				border: "1px solid #ccc",
				width: "196px"
			}
		);
	}
}
// creater folder
var initFormCreaterFolderForm = function() {
	var alertXML = new fLoadXML("folder.xml", true);
	alertXML.fSuccess = function(){
		new validerAlert(
			"formCreaterFolderAlertBox",
			"CreaterFolderForm",
			"creater",
			this.aXml['form'][0].field,
			{
				font: "11px/11px Tahoma",
				txtColor: "#666",
				appTxtColor: "#f00",
				bgColor: "#FFFFFF", 
				bgImage: "templates/images/_alert_bar.gif", 
				closeImage: "templates/images/_alert_close.gif",
				border: "1px solid #ccc",
				width: "115px"
			}
			
		 );
	}
};
//////////////////////////////////////

function fInitSubmenu() {
	var current;
	var tweening = false;
	if ($$(".submenu").length == 0) {
		setTimeout(fInitSubmenu, 10);
		return;
	}
	
	$$(".submenu").each(function(el){
		var link = el.getPrevious();
		link.block = el;
		link.fx = new Fx.Slide(link.block, {
			onComplete : function(){
				tweening = false;
			}
		});
		if (!link.block.hasClass("opened")) {
			link.fx.hide();
		} else {
			current = link;
		}
		link.addEvent('click', function(e){
			new Event(e).stop();
			
			var oThis = $(this);
			//
			if (tweening) {
				return;
			}
			//
			if (current && current == oThis.block) {
				current.fx.slideOut();
				return;
			}
			tweening = true;
			//
			oThis.fx.slideIn();
			if (current) {
				current.fx.slideOut();
			}
			//
			current = oThis;			
			document.location = link.block.getElements('li a')[0].href;
		});
	});
}
//



//set mask
function setMask(opaceTo) {
	try {
		var mask = $("mask") || 
		new Element("iframe").setProperties({
			id: "mask",
			frameborder: 'no'
		}).setStyles({
			width: "100%", 
			height: window.getScrollHeight(), 
			position: "absolute", 
			top: "0", 
			left: "0",
			zIndex:998
		}).injectBefore($$(".popup", ".layerNews")[0]);
		mask.setOpacity(1);
	} catch (e) {}
}
//call open popup
function opaceIt(thisObj, opaceTo , valueid ) {
	//return true;
	thisObj = $(thisObj);
	if (!thisObj) return;
	if(valueid) {
		$(thisObj).getElement('div[id=lbImage]').innerHTML = $(valueid).innerHTML;
	}
    if (opaceIt.mask == null) {
		opaceIt.mask = new Element("iframe").setProperties({
			frameborder: "no"
		}).setStyles({
			filter:'alpha(opacity=1)',
			overflow: "hidden",
			position: "absolute", 
			top: "0", 
			left: "0",
			"z-index": "998"
		});
		opaceIt.div = new Element("div").setStyles({
			overflow: "hidden",
			position: "absolute", 
			top: "0", 
			left: "0",
			"z-index": "998",
			'background' : '#000'
		}).setOpacity(0.6);
	}
	
	
	
	if (opaceTo != 0) {
		opaceIt.mask.injectInside($$('body')[0]);
		opaceIt.div.injectInside($$('body')[0]);		
		thisObj.injectInside($$('body')[0]);
		adjustPosition(thisObj);
		
	}
	else{
		opaceIt.mask.remove();
		opaceIt.div.remove();
		thisObj.setStyle('top', '-1500px');
		if(window.ie6) {
			window.onscroll = function(){
				thisObj.setStyle('top' , '-1500px');
			}
		}
	}
	/*var myEffects = new Fx.Morph(thisObj,{ 'duration': '1000' });
	myEffects.start({
		'opacity': [0.8,1]
	});*/
	var myEffects = new Fx.Style(thisObj, "opacity", {
		duration:600, 
		//transition:Fx.Transitions.quadInOut,
		onComplete: onCompleteHandler
	});
	myEffects.start(opaceTo);
	function onCompleteHandler() {
		if (opaceTo == 0) {
			try {
				opaceIt.mask.remove();
				opaceIt.div.remove();
				thisObj.setStyle('top', -1500);
			} catch (e) {};
		}
	}
	function adjustPosition(thisObj) {
		thisObj.setStyles({
			display:'block',
			left: window.getWidth()/2-thisObj.getCoordinates().width/2,
			top : screen.height/2-thisObj.getCoordinates().height/2 - 50,
			zIndex: 999,
			visibility : 'visible'
		});
		/*if(window.ie) {
			opaceIt.mask.setStyles({
				width: window.getWidth(), 
				height: window.getScrollHeight()
			});
		}	
		if(window.ie6) {
			var topLayer = window.getScrollTop()+ screen.height/4-thisObj.getCoordinates().height/3 +10;
			thisObj.setStyle('top' , topLayer+'px');
		}*/
		opaceIt.div.setStyles({
			width: window.getWidth(), 
			height: window.getScrollHeight()
		});
	}
	/*
	if(!window.ie6){
		if(opaceTo == 1) {
			var topLayer = screen.height/4-thisObj.getCoordinates().height/3 +10;
			if($$('.popupVideos')[0]) {
				topLayer=30;
			}
			thisObj.setStyles({
				'position' : 'fixed',
				'top' : topLayer+'px'
			});
			
		}
		else {
			thisObj.setStyles({'position' : 'absolute', 'top': '-5500px'});
		}	
	} else { //fixed bug for ie6 
		 window.onscroll = function(){
				var topLayer = window.getScrollTop() + 70;
				if($('conditionsLayer')){
					var topLayer = window.getScrollTop() + 20;
				}
				thisObj.setStyle('top' , topLayer+'px');
		}
	}*/
	
}

