function slideShow()
{
	//Set the opacity of all images to 0
	$('#latest_articles a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#latest_articles a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#latest_articles .caption').css({opacity: 0.7});

	//Resize the width of the caption according to the image width
	$('#latest_articles .caption').css({width: $('#latest_articles a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	$('#latest_articles .content').html($('#latest_articles a:first').find('img').attr('rel'))
	.animate({opacity: 0.7}, 400);
	
	//Call the latest_articles function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',5000);
	
}
function gallery()
{
	//if no IMGs have the show class, grab the first image
	var current = ($('#latest_articles a.show') ? $('#latest_articles a.show') : $('#latest_articles a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#latest_articles a:first') :current.next()) : $('#latest_articles a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#latest_articles .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#latest_articles .caption').animate({opacity: 0.7}, 100).animate({height: '46px'},500 );
	
	//Display the content
	$('#latest_articles .content').html(caption);
}
function changeSection()
{
	var section_link=$(this).val();
	$("#subsections_list option:not(:first)").remove();

	if(section_link!=-1)
	{
		var txt=$("#subsections_list option:first").text();
		$("#subsections_list").attr("disabled", "disabled").find("option:first").text("Èä¸ò çàãðóçêà");

		$.post("http://directstroy.ru/php/search.php", {target: "subsections_list", value: section_link}, function(data){
			$("#subsections_list").removeAttr("disabled").find("option:last").text(txt);
			data=data.split("\n");
			for(i=0; i<data.length-1; i++)
			{
				data[i]=data[i].split(":");
				$('<option value="'+data[i][0]+'">'+data[i][1]+'</option>').appendTo("#subsections_list");
			}
		});
	}
}
function changeCountry()
{
	var country_link=$(this).val();
	$("#regions_list option:not(:first)").remove();
	$("#cities_list option:not(:first)").remove();

	if(country_link!=-1)
	{
		var txt=$("#regions_list option:first").text();
		$("#countries_list").attr("disabled", "disabled");
		$("#regions_list").attr("disabled", "disabled").find("option:first").text("Èä¸ò çàãðóçêà");

		$.post("http://directstroy.ru/php/search.php", {target: "countries_list", value: country_link}, function(data){
			$("#regions_list").removeAttr("disabled").find("option:last").text(txt);
			data=data.split("\n");
			for(i=0; i<data.length-1; i++)
			{
				data[i]=data[i].split(":");
				$('<option value="'+data[i][0]+'">'+data[i][1]+'</option>').appendTo("#regions_list");
			}
			$("#countries_list").removeAttr("disabled", "disabled");
		});
	}
}
function changeRegion()
{
	var region_link=$(this).val();
	$("#cities_list option:not(:first)").remove();

	if(region_link!=-1)
	{
		var txt=$("#cities_list option:first").text();
		$("#cities_list").attr("disabled", "disabled").find("option:first").text("Èä¸ò çàãðóçêà");
	
		$.post("http://directstroy.ru/php/search.php", {target: "regions_list", value: region_link}, function(data){
			$("#cities_list").removeAttr("disabled").find("option:last").text(txt);
			data=data.split("\n");
			for(i=0; i<data.length-1; i++)
			{
				data[i]=data[i].split(":");
				$('<option value="'+data[i][0]+'">'+data[i][1]+'</option>').appendTo("#cities_list");
			}
		});
	}
}
function startSearch()
{
	var section_link = $("#sections_list").val();
	var subsection_link = $("#subsections_list").val();
	var country_link = $("#countries_list").val();
	var region_link = $("#regions_list").val();
	var city_link = $("#cities_list").val();

	var location = "/";

	if(city_link!=-1)
		location+=city_link;
	else if(region_link!=-1)
		location+=region_link;
	else if(country_link!=-1)
		location+=country_link;

	if(subsection_link!=-1)
		location+="/"+subsection_link;
	else if(section_link!=-1)
		location+="/"+section_link;

	if(location!="/")
		window.location = location.replace("//", "/")+".html";
	else
		window.location = "/";

	return false;
}
$(document).ready(function(){

	$('#runline').crawlLine({
		speed:2,
		crawElement:'#move',
		textElement:'p',
		hoverClass:'viewText'
	});

	$("#sections_list").change(changeSection);
	$("#countries_list").change(changeCountry);
	$("#regions_list").change(changeRegion);
	$("#fast_search form").submit(startSearch);

	$("#login").colorbox({transition:"none", iframe:true, width:"360", height:"302", opacity:"0.75", title:true, fixed:true});
	$("#registration").colorbox({transition:"none", iframe:true, width:"360", height:"382", opacity:"0.75", title:true, fixed:true});
	$("#tender").colorbox({transition:"none", iframe:true, width:"360", height:"342", opacity:"0.75", title:true, fixed:true});
	$("#contact_form").colorbox({transition:"none", iframe:true, width:"360", height:"492", opacity:"0.75", title:true, fixed:true});
	$("#b2b_context span[class^='text_']").css("display", "block").css("margin-bottom", "-18px");

	if($(".tab-content").length)
	{
		$(".tab-content>div:not(:first)").hide();
		$(".tab-buttons a").click(function(){
			$(".tab-buttons a").parent().removeClass("selected");
			$(this).parent().addClass("selected");
			class_name = $(this).attr("class");
			$(".tab-content>div").hide();
			$(".tab-content ."+class_name).show();
			return false;
		});
	}

	if($("#latest_articles").length>0)
		slideShow();
});
