
p = {
	objectTime:8000
}

jQuery(document).ready(function(){
	/*
	jQuery('#lang a').click(function(){
		if(jQuery(this).parent().parent().is('.open')){
			var t = jQuery(this).parent();
			jQuery(t).parent().unbind().children('li').removeClass('current').not(t).slideToggle(300,function(){ jQuery(this).parent().removeClass('open'); });
			jQuery(t).addClass('current');
		}else{
			jQuery(this).parent().parent().addClass('open').hover(function(){
				jQuery(this).stop(true);
			},function(){
				jQuery(this).animate({marginTop:0},500,function(){ jQuery(this).unbind().children('li').not('.current').slideToggle(300,function(){ jQuery(this).parent().removeClass('open'); }); });
			}).children('li').filter(':hidden').slideToggle(300);
		}
		return(false);
	});
	*/

	jQuery('#lang a').click(function(){
	  if(jQuery(this).parent().parent().is('.open')){
	   var t = jQuery(this).parent();
	   jQuery(t).parent().unbind().children('li').removeClass('current').not(t).slideToggle(300,function(){ jQuery(this).parent().removeClass('open'); });
	   jQuery(t).addClass('current');
	  }else{
	   jQuery(this).parent().parent().addClass('open').hover(function(){
		jQuery(this).stop(true);
	   },function(){
		jQuery(this).animate({marginTop:0},500,function(){ jQuery(this).unbind().children('li').not('.current').slideToggle(300,function(){ jQuery(this).parent().removeClass('open'); }); });
	   }).children('li').filter(':hidden').slideToggle(300);
	   
	   return(false);
	  }
	});
	
	if(jQuery.browser.msie && jQuery.browser.version == 6){
		jQuery('#logo img,#created img').pngFix();
				
		ieFixScroll();
		jQuery(window).resize(function(){ ieFixScroll(); });
		/*jQuery(window).scroll(function(){ ieFixScroll() }).resize(function(){ jQuery('#bg,#bg-inner').height(jQuery(window).height()); });
		jQuery('#bg,#bg-inner').height(jQuery(window).height());*/
	}
	
	jQuery('div.box-obj >div.img').slideImg();
	
//	jQuery('div.box-slide-obj').slideObj();
	jQuery('div.box-slide-obj').objectSlider();
});

jQuery(window).load(function(){ if( jQuery('#bg >img').length > 1 ) jQuery('#bg >img').last().slider(); });

jQuery.fn.slider = function(){
	var param = {
		speed:1000,
		timeOut:5000
	}
	var img = jQuery(this);
	
	if(jQuery(img).next().length) jQuery(img).css('z-index',1).next().css({'z-index':2,opacity:0}).delay(param.timeOut).animate({opacity:1},param.speed,function(){ jQuery(img).css('z-index',0).next().slider(); });
	else jQuery(img).css('z-index',1).parent().children('img').first().css({'z-index':2,opacity:0}).delay(param.timeOut).animate({opacity:1},param.speed,function(){ jQuery(img).css('z-index',0); jQuery(this).slider(); });
}

function ieFixScroll(){
	if( jQuery('#main').height() > jQuery('#wrap').height() ) jQuery('#top').css('width',jQuery('#wrap').width()-17);
	else jQuery('#top').css('width','100%');
   /* jQuery('#top').stop(true).animate({paddingTop:1},1).animate({paddingTop:0},1);
    jQuery('#top,#bg,#bg-inner').css('top',jQuery(window).scrollTop());*/
}


jQuery.fn.slideImg = function(){
	this.each(function(){
		var img = jQuery('>div',this).children();
		var block = jQuery(this);
		
		if( jQuery(img).length > 1){
			jQuery(img).not(':first').hide();
			jQuery(img).filter(':visible').addClass('current');
			jQuery(this).append('<a href="#prev" class="prev"><img src="img/prev-2.gif" alt="prev" title="Предыдущая" /></a><a href="#next" class="next"><img src="img/next-2.gif" alt="next" title="Следующая" /></a>');
			jQuery('>a',this).css('opacity',0).click(function(){
				jQuery(this).css('opacity',1).stop(true).animate({opacity:0.75},100);
				var cls = (jQuery(this).attr('class') == 'prev')?1:0;
				var current = jQuery(img).stop('true').hide().css({'opacity':1,'z-index':0}).filter('.current').show().removeClass('current');
				var goTo = (cls)?jQuery(current).prev():jQuery(current).next();
				if( !jQuery(goTo).length ) goTo = (cls)?jQuery(img).last():jQuery(img).first();
				jQuery(goTo).addClass('current').css({'z-index':5,'opacity':0}).show().animate({opacity:1},500,function(){ jQuery(current).css('opacity',1).hide(); jQuery(this).css('z-index',0); });
				return(false);
			}).hover(function(){
				jQuery(this).stop(true).animate({opacity:0.75},100);
			},function(){
				jQuery(this).stop(true).animate({opacity:1},100);
			});
			
			jQuery(this).hover(function(){
				jQuery('>a',this).stop(true).animate({opacity:1},300);
			},function(){
				jQuery('>a',this).stop(true).animate({opacity:0},300);
			});
		}
	});
}

jQuery.fn.slideObj = function(){
	var c = 0;
	this.each(function(){
		var obj = jQuery('>div.obj',this);
		var block = jQuery(this);
		
		if( jQuery(obj).length > 1){
			var blockHeight = 0;
			jQuery(obj).each(function(){
				if(jQuery(this).height() > blockHeight) blockHeight = jQuery(this).height();
			});
			jQuery(this).height(blockHeight);
			jQuery(obj).not(':first').hide();
			/**/
			if(c){
				jQuery(obj).first().animate({marginTop:0},4000,function(){ jQuery(this).slider2(); });
				c = 0;
			}else{
				jQuery(obj).first().slider2();
				c++;
			}
		}
	});
}

jQuery.fn.slider2 = function(){
	var param = {
		speed:1000,
		timeOut:7000
	}
	var i = jQuery(this);
	
	if(jQuery(i).next().length) jQuery(i).delay(param.timeOut).slideToggle(param.speed).next().delay(param.timeOut).slideToggle(param.speed,function(){ jQuery(this).slider2(); });
	else jQuery(i).delay(param.timeOut).slideToggle(param.speed).parent().children('div.obj').first().delay(param.timeOut).slideToggle(param.speed,function(){ jQuery(this).slider2(); });
}

jQuery.fn.pngFix = function(){
	this.each(function(){
		var param = {
			width:jQuery(this).width(),
			height:jQuery(this).height(),
			src:'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+jQuery(this).attr('src')+'",sizingMethod="scale")'
		}
		jQuery(this).attr('src','/img/clear.gif').css({'width':param.width,'height':param.height,'filter':param.src});
	});
}


jQuery.fn.objectSlider = function(){
	this.each(function(){
		var param = {
			parent:$(this),
			block:$('>div.block >div',this),
			box:$('>div.block >div >div.obj',this)
		}
		param.count = $(param.box).length;
		
		if(param.count > 1){
			$(param.block).height($(param.block).height());
			$(param.box).not($(param.box).first()).hide();
			
			var sld = '';
			for (var i = 0; i < param.count; i++) {
				var h = $(param.box).eq(i).children('h4').text();
				sld += '<li><a href="#'+i+'" title="'+h+'">'+h+'</a></li>';
			}
			var html = '<a href="#prev" class="prev" title="Prev">Prev</a><a href="#next" class="next" title="Next">Next</a><ul>'+sld+'</ul>';
			$(this).append(html);
			
			param.li = $('>ul >li',this);
			param.list = $('>a',param.li);
			param.slider = $('>a',this);
			
			$(param.li).first().addClass('current');
			
			$(this).autoslide(param);
			
			$(param.list).click(function(){
				if(!$(this).parent().is('.current')){
					$(param.parent).stop(true).autoslide(param);
					
					var old = $(param.li).filter('.current').removeClass().stop(false, true).index();
					var index = $(this).parent().addClass('current').index();
					
					$(param.box).eq(index).css('left',(old < index)?658:-610).addClass('move').show().animate({left:24},300,function(){ $(this).removeClass('move'); });
					$(param.box).eq(old).css('left',24).addClass('move').animate({left:(old < index)?-610:658},300,function(){ $(this).hide().removeClass('move'); });
				}
				
				return(false);
			});
			
			$(param.slider).click(function(){
				$(param.parent).stop(true).autoslide(param);
				
				var old = $(param.li).filter('.current').removeClass().index();
				
				if($(this).is('.next')){
					var index = (old+2 > param.count)?0:old+1;
					var h = 0;
				}else{
					var index = (old-1 < 0)?param.count-1:old-1;
					var h = 1;
				}
				
				$(param.li).eq(index).addClass('current');
				
				$(param.box).eq(index).css('left',(h)?-610:658).addClass('move').show().animate({left:24},300,function(){ $(this).removeClass('move'); });
				$(param.box).eq(old).css('left',24).addClass('move').animate({left:(h)?658:-610},300,function(){ $(this).hide().removeClass('move'); });
				
				return(false);
			});
		}
	});
}

jQuery.fn.autoslide = function(param){
	$(this).animate({marginTop:0},p.objectTime,function(){
		var old = $(param.li).filter('.current').removeClass().index();
		var index = (old+2 > param.count)?0:old+1;
		
		$(param.box).eq(index).css('left',658).addClass('move').show().animate({left:24},300,function(){ $(this).removeClass('move'); });
		$(param.box).eq(old).css('left',24).addClass('move').animate({left:-610},300,function(){ $(this).hide().removeClass('move'); });
		
		$(param.li).eq(index).addClass('current');
		
		$(this).autoslide(param);
	});
}
