Asset.images([
	'images/moognify.gif',
	'images/down.gif',
	'images/up.gif',
	'images/b_show.gif',
	'images/b_show2.gif'
]);

MooWaiter = {
  // the script to wait this amount of msecs until it shows the loading element
  timeUntilShow: 250,

  // opacity of loading element
  opacity: 0.8,

  // Start waiting status - show loading element
  startWaiting: function(className, timeUntilShow, opacity) {
    element = this;
    className = className || 'waiting';
    timeUntilShow = timeUntilShow || MooWaiter.timeUntilShow;
    opacity = opacity || MooWaiter.opacity;
    element._waiting = true;
    if (!element._loading) {
      var e = element._loading = new Element('div', {
      	'styles': {
      		'position': 'absolute',
      		'opacity': opacity
      	}
      }).injectInside(/*element.offsetParent*/$(element).getParent() || document.body);
    }
    element._loading.className = className;
    (function() {
      if (this._waiting) {
        $(this._loading).setStyles({
          'left': this.getLeft(),
          'top': this.getTop(),
          'width': this.getSize().size.x,
          'height': this.getSize().size.y
        });
      }
    }).bind(element).delay(timeUntilShow);
    return this;
  },

  // Stop waiting status - hide loading element
  stopWaiting: function() {
    element = this;
    if (element._waiting) {
      element._waiting = false;
      //element._loading.parentNode.removeChild(element._loading);
      $(element._loading).remove();
      element._loading = null;
    }
    return this;
  }
};

if (MooTools) {
  Element.extend(MooWaiter);

  /* Extends functionality from <Waiter> into <Ajax>.

     Additional Options:
     useWaiter - (boolean) if true will automatically apply a <Waiter> to the update target; defaults to false. Note: if you do not specify a value for update option this is ignored.
     waiterOptions - (object) options value passed on to <Waiter> class.
     waiterTarget - (element) if specified, the Waiter will overlay this element, otherwise it uses the update target specified in the ajax options.
  */
  if (typeof Ajax != "undefined") {
     var Ajax = Ajax.extend({
         options: {
           useWaiter: false,
           waiterOptions: {
             className: 'waiting',
             timeUntilShow: MooWaiter.timeUntilShow,
             opacity: MooWaiter.opacity
           },
           waiterTarget: false
         },
         initialize: function(url, options) {
           this.parent(url, options);
           if (this.options.useWaiter && ($(this.options.waiterTarget) || $(this.options.update))) {
             var stop = function() {
               ($(this.options.waiterTarget) || $(this.options.update)).stopWaiting();
             }.bind(this);
             this.addEvent('onComplete', stop);
             this.addEvent('onFailure', stop);
           }
         },
         request: function(data) {
           if (this.options.useWaiter) {
             try {
               ($(this.options.waiterTarget) || $(this.options.update)).startWaiting(this.options.waiterOptions.className, this.options.waiterOptions.timeUntilShow, this.options.waiterOptions.opacity);
             } catch(e) {}
           }
           this.parent(data);
           return this;
         }
     });
  }
}

var hasFlash = function(){
  var nRequiredVersion = 9;

  if (window.ie && navigator.appVersion.indexOf("Windows") > -1) {
    document.write('<script language="VBScript"\> \non error resume next \nhasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & ' + nRequiredVersion + '))) \n</script\> \n');

    if (window.hasFlash != null) {
      return window.hasFlash;
    };
  };

  if (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"] && navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin) {
     var flashDescription = (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description;
     var ver = flashDescription.match(/\d{1,2}\.\d/);
     return ver ? (ver[0].toInt() >= nRequiredVersion) : false;
  };

  return false;
}();

function menuElement(id, name, caption, icon, url, parent, level) {
	this.id = id;
	this.name = name;
	this.caption = caption;
	this.url = url;
	this.parent = parent;
	this.level = level;

	if (level==1)
		this.icon = '<img src="images/0.gif" width="9" height="1" align="absmiddle">';
	 else
		this.icon = '<img src="images/li.gif" width="2" height="2" align="absmiddle">';

	if(level <= 1) this.visible = true;
	else this.visible = true;
	this.children = new Array();
	return this;
}

function addNode(name, caption, icon, url, parent) {
	id = parent.id*100 + parent.children.length + 1;
	elem = new menuElement(id, name, caption, icon, url, parent, parent.level+1);
	parent.children[parent.children.length] = elem;
	return elem;
}

function switchMenu(id) {
	var obj = getById(menu, id);
	for(var i=0; i<obj.children.length; i++) {
		obj.children[i].visible = !obj.children[i].visible;
	}
//	autoClose(menu, obj)
	showMenu();
}

function getById(obj, id) {
	if(obj.id == id) { return obj; }
	for(var i=0; i<obj.children.length; i++) {
	var obj1 = getById(obj.children[i], id);
		if(obj1.id == id) { return obj1; }
	}
	return obj;
}

function getByName(obj, name) {
  if(obj.name == name) {
    return obj;
  }
  for(var i=0; i<obj.children.length; i++) {
    var obj1 = getByName(obj.children[i], name);
	if(obj1.name == name) {
      return obj1;
    }
  }
  return obj;
}

function setMenu(name) {
  var obj = getByName(menu, name);
  if(obj.level > 0) {
  	for(var i=0; i<obj.parent.children.length; i++) {
  	  obj.parent.children[i].visible = true;
  	}
  }

  for(var i=0; i<obj.children.length; i++) {
    obj.children[i].visible = true;
  }

  if(obj.level > 0) {
  	var level = obj.parent.level;
  	while(obj.level > 1) {
    	obj.visible = true;
		obj = obj.parent;
  	}
  	obj.visible = true;
  	setVisible(obj, level);
  }

  showMenu();
}

function setVisible(obj, level) {
	if(obj.level < level) {
		for(var i=0; i<obj.children.length; i++) {
			obj.children[i].visible = true;
			setVisible(obj.children[i], level);
		}
	}
}

function autoClose(top, obj) {
	var strId1 = top.id + "";
	var strId2 = obj.id + "";
	if(!((top.level <= obj.level)||(strId1.substring(0,strId2.length) == strId2))) {
		top.visible = false;
	}
	for(var i=0; i<top.children.length; i++) {
		autoClose(top.children[i], obj);
	}
}

function buildMenu(obj, text) {

	for(var i=0; i<obj.children.length; i++) {
		child = obj.children[i];

		if(child.visible) {

			if(child.children.length > 0) {
				if (child.parent.id==0) {
					var line = startNodeLine.replace("#ID#",child.id);
				} else {
					var line = startNodeLine2.replace("#ID#",child.id);
					line = line.replace("#WD#", (child.level-1)*10);
				}

				var line2 = endLine;

			} else {
				line = startLeafLine;
				line2 = endLine;
			}

			if (((child.level-1)*10) > 10) {
				line = line.replace("#CL#",'#FFFFFF');
				line2 = line2.replace("#CL#",'#FFFFFF');
			} else if (((child.level-1)*10) > 0) {
				line = line.replace("#CL#",'#F1F6F9');
				line2 = line2.replace("#CL#",'#F1F6F9');
			} else {
				line = line.replace("#CL#",'#EFEFEF');
				line2 = line2.replace("#CL#",'#EFEFEF');
			}

			line = line.replace("#WD#", (child.level-1)*28);
			text += line.replace("#LV#", child.level);
			text += spacer;

			if(child.children.length > 0) {
				if(child.children[0].visible) {
					child.icon = openedLine.replace("#URL#", "javascript:switchMenu('"+child.id+"')");;
				} else {
					child.icon = closedLine.replace("#URL#", "javascript:switchMenu('"+child.id+"')");;
				}
			}

			text += child.icon + '&nbsp;&nbsp;';

			if(child.name != currentId) {
				url_text = startLink.replace("#URL#", child.url);

				if (child.parent.id!=0 && child.children.length==0) {
					url_text = url_text.replace("#CL#", 'class="lnk"');
				}
				//
				if (child.url == '#') {
					url_text = startLink.replace("#URL#", "javascript:switchMenu('"+child.id+"')");
					url_text = url_text.replace('target="main"', 'target="_self"');
				}
				//
				text += url_text;
			}

			text += child.caption;
			if(child.id != currentId) text += endLink;
			text += line2;
			text = buildMenu(child, text);
		}
	}

	return text;
}

function showMenu() {
	var text = buildMenu(menu, '');
	text = startBlock + text + endBlock;
	document.getElementById('wpmmenu').innerHTML = text;
}

function showError(text) {
	errordiv = new Element('div', {'id': 'errordiv'}).injectInside(document.body);
	errordiv.innerHTML = text;

	var c = errordiv.getCoordinates();

	errordiv.setStyles({
		'top': (document.body.clientHeight - c.height)/2,
		'left': (document.body.clientWidth - c.width)/2
	});

	errordiv.addEvent('click',function(){
        $('errordiv').setStyles({'display':'none'});
    });
}

function formDownload(elem, id) {
	var c = $(elem).getCoordinates();
	$('file_id').value = id;
	$('formdiv').setStyles({
		'display':'block',
		'top': c.bottom
	});

	return false;
}

function hideDownload() {
	$('formdiv').setStyles({
		'display': 'none'
	});
}

function showForm() {
	new Ajax('feedback.php', {
		evalScripts: true,
		update: 'contactdiv',
		method: 'get'
	}).request();
}

function activateToggler(status, usereverse) {
	window.addEvent('domready', function() {
		$('toggler1').opened = status;
		$('toggler2').opened = status;

		var mySlide1 = new Fx.Slide('info1');

		if (!status)
		{
			mySlide1.hide();
		}

		$('toggle1').addEvent('click', function(e){
			$('toggler1').src = 'images/down.gif';
			e = new Event(e);
			mySlide1.toggle();

			if ($('toggler1').opened)
			{
				$('toggler1').src = usereverse ? 'images/up.gif' : 'images/down.gif';
				$('toggler1').opened = false;
			} else {
				$('toggler1').src = usereverse ? 'images/down.gif' : 'images/up.gif';
				$('toggler1').opened = true;
			}

			e.stop();
		});

		var mySlide2 = new Fx.Slide('info2');

		if (!status)
		{
			mySlide2.hide();
		}

		$('toggle2').addEvent('click', function(e){
			e = new Event(e);
			t2 = mySlide2.toggle();

			if ($('toggler2').opened)
			{
				$('toggler2').src = 'images/down.gif';
				$('toggler2').opened = false;
			} else {
				$('toggler2').src = 'images/up.gif';
				$('toggler2').opened = true;
			}

			e.stop();
		});
	});
}

function preparefolio() {
	$('foliofilter').send({
		useWaiter: true,
		evalScripts: true,
		update: 'tempfolio',
		onComplete: function() {
			$('objects').innerHTML = objcnt;
			if (objcnt>0) {
				document.images['btnfolio'].src = 'images/b_show2.gif';
				showme = true;
			} else {
				document.images['btnfolio'].src = 'images/b_show.gif';
				showme = false;
			}
		}
	});
}

function showtempfolio() {
	if(objcnt>0 && showme) {
		$('listfolio').empty().adopt($('tempfolio').getFirst());
		$('tempfolio').empty();
	}
}

function showblock(id) {
	var elem = $('block' + id);
	if (!elem.opened) {
		elem.className = 'shown';
		elem.opened = true;
	} else {
		elem.className = 'hided';
		elem.opened = false;
	}
}

