var fade_speed = 500; //скорость затухания/появления
var fade_time = 7000; //время задержки между циклами затухания/появления

var prod_slide_time = 500; //время задержки между циклами затухания/появления

var cur_elem;

jQuery(document).ready(function(){
	jQuery('.captcha_reset').click(function(){
		var d = new Date();
		jQuery(this).parent().parent().find('img#captcha_img').attr('src', '/captcha.php?reset&' + d.getTime());
	}); 
	if (jQuery('#slider div') && jQuery('#slider_controls'))
	{
		ashki = "";
		for (i=0; i<jQuery('#slider').children('div').length; i++)
		{
			ashki +="<a href='#"+i+"' class='"+((i==0)?'active':'')+"'></a>";
		}
		jQuery('#slider_controls').html(ashki);
		jQuery('#slider_controls a').click(function(){
			go_to = parseInt(jQuery(this).attr('href').replace('#', ''));
			go_to_slide(go_to);
			return false;
		});
		setTimeout(fade_my_slider, fade_time);
	}
	jQuery('#flashka .next a').click(function(){ go_next(); });
	jQuery('#flashka .prev a').click(function(){ go_prev(); });
	jQuery('#main_content .content_field h1').each(function(){
		txt = jQuery(this).text();
		txt = "<div class=\"header_w_bg dinamic\"><div class=\"header_w_l\"> <div class=\"header_w_r\">"+txt+"</div></div></div>";
		jQuery(txt).insertAfter(this);
		jQuery(this).remove();
	});
	
	jQuery('#main_content .content_field img , #main_content .news_cont img , #articles .article_text img ,  #main_content .videos .video_item img').each(function(){
		tmp_style = this.style.cssText;
		if (tmp_style != "")
			tmp_style = this.getAttribute('style');
		tmp_src = this.getAttribute('src');
		tmp_w = this.getAttribute('width');
		tmp_h = this.getAttribute('height');
		tmp_title = this.getAttribute('title');
		tmp_alt = this.getAttribute('alt');
		if(tmp_style)
			txt = "<div class='foto_wrapper' style='"+tmp_style+"'>";
		else
			txt = "<div class='foto_wrapper' style='float:left;'>";
		txt += "<div class='foto_lt'><div class='foto_rt'><div class='foto_t'></div></div></div>";
		txt += "<div class='foto_l'><div class='foto_r'><div class='foto_pad'>";
		txt += "<img ";
		txt += "src='"+tmp_src+"' ";
		txt += (tmp_w!="" && tmp_w!='null' && tmp_w!=null)?("width='"+tmp_w+"' "):"";
		txt += (tmp_h!="" && tmp_h!='null' && tmp_h!=null)?("height='"+tmp_h+"' "):"";
		txt += "title='"+tmp_title+"' ";
		txt += "alt='"+tmp_alt+"' ";
		txt += "/>";
		txt += "</div></div></div>";
		txt += "<div class='foto_lb'><div class='foto_rb'><div class='foto_b'></div></div></div>";
		txt += "</div>";
		jQuery(txt).insertAfter(this);
		jQuery(this).remove();
	});
});

var next_el = false;
var next_el_count = 1;
var stop_slide = 0;
function fade_my_slider()
{
	if (stop_slide == 0)
	{
		if (!next_el)
			next_el = jQuery('#slider div.flash_item.show').next();
		if (jQuery(next_el).next().length==0)
		{
			next_el = jQuery('#slider div.flash_item:eq(0)');
			next_el_count = 0;
		}
		else
			next_el = jQuery('#slider div.flash_item.show').next();
			
		setTimeout(function(){ 
			slide_operation();
			}
		, fade_speed);
	}
	else
		stop_slide = 0;
	setTimeout(fade_my_slider, fade_time);
}
function slide_operation()
{ 
	jQuery('#slider .show').animate({opacity: 0}, fade_speed);
	jQuery(next_el).animate({opacity: 1}, fade_speed);
	setTimeout(function(){ 
		jQuery('#slider .show').removeClass('show'); 
		jQuery(next_el).addClass('show');
	}, fade_speed);
	
	//next_el_count++;
}
function go_next()
{
	sstop_slide=1;
	el = jQuery('#slider div.flash_item.show');
	next_el = (jQuery(el).next().length!=0)?jQuery(el).next():jQuery('#slider div.flash_item:eq(0)');
	next_el_count = (jQuery(el).next().length!=0)?(next_el_count+1):0;
	slide_operation();
}
function go_prev()
{
	stop_slide=1;
	el = jQuery('#slider div.flash_item.show');
	next_el = (jQuery(el).prev().length!=0)?jQuery(el).prev():jQuery('#slider div.flash_item:last');
	next_el_count = (jQuery(el).prev().length!=0)?(next_el_count-1):(jQuery('#slider div.flash_item').length-1);
	slide_operation();
}
function go_to_slide(i)
{
	next_el = jQuery('#slider div.flash_item:eq('+i+')');
	next_el_count = i;
	
	slide_operation();
}
jQuery(function() {
	//jQuery('.lightbox').lightBox({fixedNavigation:true});
	jQuery('.lightbox').fancybox({
		'zoomSpeedIn' :	500,
		'zoomSpeedOut' : 500
	});
	jQuery(".mapopen a").fancybox({
		'zoomSpeedIn' :	500,
		'zoomSpeedOut' : 500
	});
	if (jQuery('#GMapContainer').length > 0)
	{
		GMapInit();
	}
})
jQuery(document).ready(function(){
	if (jQuery('#add_form_sert'))
	{
		jQuery('#add_form_sert').click(function(){
			//id=5;
			id = parseInt(this.className.replace('form', ''));
			jQuery.ajax({
				//url: '/upage//' + id + '?transform=modules/catalog/popup-add-options.xsl',
				url: '/udata//webforms/add/' + id + '?transform=modules/webforms/sert_form.xsl',
				dataType: 'html',
				success: function (data) {
					jQuery.fancybox(data);
					jQuery('#fancybox-content').css('background-color', '#F7F2D5').css('border-color', '#F7F2D5');
				}
			});
		});
	}
});
