Shadowbox.init({
	skipSetup: false,
	adapter: 'base',
	language: 'en',
	overlayColor: '#000000',
	overlayOpacity: '0.8',
	handleOversize: 'resize',
	players: ["img", "swf", "html", "iframe", "flv"]
});

function fixpng(){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5) && (document.body.filters) && (version < 7)){
		var png_images = $$('.pngfix');	
		png_images.each(function(element) {
			if(!$(element).hasClass('pngfixed')){
				$(element).addClass('pngfixed');
				var img = $(element);
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.getProperty('width') + "px; height:" + img.getProperty('height') + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML					
			}
		});
	} else {
		$$('.pngfix').addClass('pngfixed');
	}
}

function init(){
	fix();
	fixpng();
	
	if($$('#container.acc').length){
		acc_init();
		acc_open(1);	
	}

	menu_init();

}

function loadDiv(in_url, in_div){
    new Request.HTML({   
        method: 'get',   
        url: in_url, 
        evalScripts: true, 
        headers: {'X-REQUEST': 'AJAX', 'X-DIV': in_div},
        update: in_div,
        onComplete: function(){ 
			pageTracker._trackPageview(in_url);
        	window.fireEvent('ajaxload');
            window.removeEvents('ajaxload');
		}
	}).send();
}

var levels = new Array();
var togglers = new Array();
var min_height = 0;
function acc_init(){
	levels = $$('.level');
	togglers = $$('.toggler');
	if(levels.length > 1){
		$('footer').setStyle('marginTop', '50px');
	} else {
		min_height = Math.max(330, $('container1').getScrollSize().y, $('side1').getScrollSize().y, $('seealso1').getScrollSize().y);
	}
	togglers.each(function(el, i){
		el.addEvent('click', function(){
			menu_close_all();
			level = el.getParent('.level').getProperty('level');
			if(!$('level'+level).hasClass('animating')){
				if(!$('level'+level).hasClass('open')){
					acc_open(level);
				} else {
					acc_close(level);
				}
			}
		});
	});
}
		
function acc_open(in_level){
	height = Math.max(min_height, $('container'+in_level).getScrollSize().y, $('side'+in_level).getScrollSize().y, $('seealso'+in_level).getScrollSize().y);
	$('level'+in_level).addClass('animating');
	new Fx.Tween($('level'+in_level), {duration: 750, onComplete: function(){
		$('level'+in_level).removeClass('animating');
		$('level'+in_level).addClass('open');
		fix();
	}}).start('height', $('level'+in_level).getSize().y, 64+height);
	new Fx.Tween($('stretcher'+in_level), {duration: 750}).start('height', $('level'+in_level).getSize().y, 64+height);
}

function acc_close(in_level){
	height = Math.max(min_height, $('container'+in_level).getScrollSize().y, $('side'+in_level).getScrollSize().y, $('seealso'+in_level).getScrollSize().y);
	$('level'+in_level).addClass('animating');
	new Fx.Tween($('level'+in_level), {duration: 750, onComplete: function(){
		$('level'+in_level).removeClass('animating');
		$('level'+in_level).removeClass('open');
		fix();
	}}).start('height', $('level'+in_level).getSize().y, 57);
	new Fx.Tween($('stretcher'+in_level), {duration: 750}).start('height', $('level'+in_level).getSize().y, 57);
}

var menuFx = new Array();

function menu_init(){
	$$('.has_sub').each(function(el, i){
		el.addEvent('mouseenter', function(){
			sub = el.getProperty('sub');
			if(!$('sub_'+sub).hasClass('animating')){
				menu_open(sub);	
			}
		});	
	});
	$$('.no_sub').each(function(el, i){
		el.addEvent('mouseenter', function(){
			menu_close_all();
		});	
	});
	$$('.submenu').each(function(el, i){
		menuFx[el.getProperty('main')] = new Fx;
		el.addEvent('mouseleave', function(){
			if(!$(el.id).hasClass('animating')){
				menu_close(el.id);
			}
		});
		el.setStyle('marginLeft', $('main_'+el.getProperty('main')).getPosition().x-12+"px");
	});
	
	$('container').addEvent('click', menu_close_all);
}

function menu_open(sub){
	height = $('sub_'+sub).getScrollSize().y;
	 $('sub_'+sub).addClass('animating');
	 menu_close_all(sub);
	 menuFx[sub] = new Fx.Tween($('sub_'+sub), {duration: 300, onComplete: function(){
		$('sub_'+sub).addClass('open');
		$('sub_'+sub).removeClass('animating');
	}}).start('height', 20, height);
}

function menu_close_all(sub){
	$$('.submenu').each(function(el, i){
		if(sub){
			if(el.id != 'sub_'+sub && (el.hasClass('open') || el.hasClass('animating'))){
				menuFx[el.getProperty('main')].cancel();
				menu_close(el.id);
			}
		} else {
			if(el.hasClass('open') || el.hasClass('animating')){
				menuFx[el.getProperty('main')].cancel();
				menu_close(el.id);
			}
		}
	});
}

function menu_close(sub){
	 $(sub).addClass('animating');
	 menuFx[$(sub).getProperty('main')].cancel();
	 menuFx[$(sub).getProperty('main')] = new Fx.Tween($(sub), {duration: 200, onComplete: function(){
		$(sub).removeClass('open');
		$(sub).removeClass('animating');
		$(sub).setStyle('height', '0px');
	}}).start('height', $(sub).getSize().y, 20);
}

function fix(){
	if($('body').getSize().y < $('body').getScrollSize().y){
		$('backtotop').setStyle('display', 'block');
	} else {
		$('backtotop').setStyle('display', 'none');
	}
}

window.addEvent('load', init);
window.addEvent('resize', fix);
