var timerMenu=0, menuShowSpeed = 200, menuHideTimeout = 250, menuHideSpeed = 200;

$(document).ready(function() {
	// resize images for device independance
	var imgs = $('img');
	imgSizer.collate(imgs);
	
	$(document.body).removeClass('noscript');
	$(window).bind('resize', windowResize).resize();
	
	// main menu
	$('#menu > div.wrapper > ul > li').has('div.sub').children('a').addClass('more').next('div.sub').hide();
	$('#menu a.more').bind({ mouseover: function() {
		if ($('#menu > .wrapper > ul').css('display')=='inline')
			$(this).addClass('over').removeClass('subHide').next('div.sub:hidden').fadeIn(menuShowSpeed);
	}}).bind({ mouseout: function() {
		if ($('#menu > .wrapper > ul').css('display')=='inline') {
			$(this).addClass('subHide');
			if (timerMenu==0) timer=setTimeout(menuDraw, menuHideTimeout);
		}
	}});
	$('#menu div.sub').bind({ mouseover: function() { $(this).prev('a').mouseover(); }}).bind({ mouseout: function() { $(this).prev('a').mouseout(); }});
	
	// slideshow
	if ($('.slideshow').length>0) {
		$('.slideshow').each(function() {
			$(this).css({ overflow: 'hidden', position: 'relative'});
			var ctrl=$(this).find('.controls');
			var sliders=ctrl.parent().find('.slider').length;
			ctrl.find('span').text('1/'+sliders);
			$(ctrl).children('a.prev').click(function(event) {
				event.preventDefault();
				changeSlide(this, 'prev');
			});
			$(ctrl).children('a.next').click(function(event) {
				event.preventDefault();
				changeSlide(this, 'next');
			});
		});
	}
	
	$('#mBottom a').click(function(event) { event.preventDefault(); $(document).scrollTop(0); });
	
});

function menuDraw() {
	$('#menu a.subHide').next('div.sub').fadeOut(menuHideSpeed).end().removeClass('over');
	clearTimeout(timer);
	timer=0;
}

function changeSlide(controlLink, direction) {
	var animationDuration=500;
	var sliderCurrent=$(controlLink).parent().parent().find('.slider:visible');
	var ind=sliderCurrent.prevAll().length;
	var indMax=sliderCurrent.siblings('.slider').length;
	var slideWidth=sliderCurrent.width();
	var slideHeight=sliderCurrent.height();
	if (direction=='prev') {
		if (ind==0) return;
	} else { // direction == 'next'
		if (ind>=indMax) return;
		ind+=2;
	}
	$(controlLink).parent().children('span').text(''+ind+'/'+(++indMax));
	sliderCurrent.parent().css({width: slideWidth, height: slideHeight});
	if (direction=='prev') {
		sliderCurrent.css({position: 'absolute', marginLeft: 0, width: slideWidth}).prev().css({position: 'absolute', marginLeft: -slideWidth, width: slideWidth}).show();
		mLeft=slideWidth;
		slideNew=sliderCurrent.prev();
	} else { // direction == 'next'
		sliderCurrent.next().css({position: 'absolute', marginLeft: slideWidth, width: slideWidth}).show().end().css({position: 'absolute', marginLeft: 0, width: slideWidth});
		mLeft=-slideWidth;
		slideNew=sliderCurrent.next();
	}
	sliderCurrent.animate({marginLeft: mLeft}, animationDuration, function() {$(this).hide().css({width: 'auto'});});
	slideNew.animate({marginLeft:0}, animationDuration, function() {$(this).css({position: 'relative', margin: 0}).parent().css({width: 'auto', height: 'auto'});});
}

function windowResize() {
	var winWidth = $(window).width();
	var docWidth = $('#container').width();
}

