d = document;

$j = jQuery;
num = parseInt;
function tag(o, v) {
	return (v) ? (o.tagName.toLowerCase() == v.toLowerCase()) : o.tagName.toLowerCase();	
}

function make_slides(slides, opts) {	
	var o = this,
		def = {
			s: false,
			act: function(o2) {
				if (o.s) {
					o.s.stop().hide().css('opacity','');
					//o.s.tab.stop().animate({'marginTop':170}, 300, 'swing');
				}
				o.opts.eff(o2);
				o.s = o2.$;
				o.i = o2.i;
				return (false);
			},
			eff: function(o2) {
				o2.$.fadeIn(500, function() {o2.parentNode.style.backgroundImage = o2.style.backgroundImage; })
			},
			
			auto: true
	};
		
	
	o.auto = {
		act: function() {o.opts.act(o.itms[o.i]); o.i = (o.i < o.itms.length-1) ? o.i+1 : 0},
		start:function() {
			o.auto.act();
			clearInterval(o.auto.t1);
			o.auto.t1 = setInterval(o.auto.act, 5000);
			o.auto.paused = false;
			slides.removeClass('hov');
		},
		pause:function() {
			clearInterval(o.auto.t1);
			o.auto.paused = true;
		}		
	}

	o.itms = $('.pad', slides).each(function(i) {this.$ = $(this), this.i = i; this.style.backgroundImage = 'url('+this.getElementsByTagName('img')[0].src+')'; if (i) {this.$.hide()} else { o.s = this.$; this.parentNode.style.backgroundImage = this.style.backgroundImage; } });
	
	o.tabs = $('#menu li')
		.each(function(i) {this.$ = $(this); this.i = i; o.itms[i].$.tab = this.$; })
		.hover(
			function() {
				o.i = this.i;
				o.auto.pause();
				o.opts.act(o.itms[o.i]);
				slides.addClass('hov');
			},  
			function() {
				o.i = (o.i < o.itms.length-1) ? o.i+1 : 0;
				o.auto.t1 = setInterval(o.auto.start, 1000);
				
			}
		)
		
	o.i = 0;
	
	slides.hover(
		function() {
				clearInterval(o.auto.t1);
			},  
			function() {
				o.auto.t1 = setInterval(o.auto.start, 1000);				
			}
	);
	
	(!opts) ? opts = def : opts = $.merge({}, def);
	
	o.opts = opts;
	
	//slides.css('backgroundColor','#FFFFFF');
	o.auto.start();
	
	return o;
	
}

function make_menu () {
	var o = d.getElementById('menu');
	o.itms = [];
	o.s = false;
	
	
	var tmp = o.getElementsByTagName('li');
	
	for (var i = 0, o2; o2 = tmp[i]; i++) {	
		
		if (o2.className.indexOf('s') >= 0) o.s = o2;
		
		o2.sm = o2.getElementsByTagName('div');
		
		o.itms.push(o2);
		
		o2.onmouseover = function() {
			clearInterval(o.t1);
			if (o.hov) o.hov.className = (o.hov != o.s) ? '' : 's';
			this.className = 'hov';
			o.hov = this;
		}
		
		o2.onmouseout = function() {
			clearInterval(o.t1);
			//if (o.s != this) this.className = '';
			o.t1 = setTimeout(function() {o.hov.className = ''; o.s.className = 's'}, 500);
		}
		
		if (!o2.sm.length) continue;
		
		o2.sm[0].mouseover = function() {
			o.t1.clearTimeout();
		}
	}
};

$().ready( function() {
		Cufon.replace('#menu a span', {fontWeight:'normal', fontSize:'15px'});
		Cufon.replace('#index_abstr h3, form#cat_search a.to_cat, h1.u, div#cat_menu h2, h2, #library_search1 h3, #library #mod212 h1, h2.hdr2 a');
		Cufon.now();
		
		var pr = $('#promo'); 
		if (pr.length) pr.slides = new make_slides(pr);
	});
