var sliderTimer;
$().ready( function() {
if ( $('.search-clear').length ) {
$('.search-clear:first').click( function() {
$('.search-field:first').val('');
});
$('.search-field:first').focus( function() {
if ( $(this).val() == "поиск" ) $(this).val('');
});
$('.search-field:first').blur( function() {
if ( $(this).val() == "" ) $(this).val('поиск');
});
}
if ( $('#citiesSelect').length ) {
	var selectedCity = $('#citiesSelect option:selected').text();
	if ( selectedCity != "" ) {
		var tmpStr = "<h2><strong>" + selectedCity + "</strong></h2>";
		var j = 0;
		for ( var i = 0; i<salons_data.length; i++ ) {
			if ( salons_data[i]['city'] == selectedCity ) {
				var curphoto = "<span class='salon-photo'></span>";
				if ( typeof salons_data[i]['photo'] != "undefined" ) {
					curphoto = "<a class='salon-photo fancy' href='" + salons_data[i]['photo'] + "'><img src='/imgs/salon_photo.gif' alt='' /></a>";
				}
				tmpStr += "<div class='salons-item'>" + curphoto + "<div class='salon-info'><p class='salon-name'><strong>" + salons_data[i]['name'] + "</strong></p><div class='salon-address'>" + salons_data[i]['address'] + "</div></div></div>";
				j++;
				if ( j == 2 ) {
					j = 0;
					tmpStr += "<div class='spacer'></div>";
				}
			}
		}
		tmpStr += "<div class='spacer'></div>";
		$('#salonsContent').html(tmpStr);

		$('#citiesSelect').change( function() {
			var selectedCity = $('#citiesSelect option:selected').text();
			var tmpStr = "<h2><strong>" + selectedCity + "</strong></h2>";
			var j = 0;
			for ( var i = 0; i<salons_data.length; i++ ) {
				if ( salons_data[i]['city'] == selectedCity ) {
					var curphoto = "<span class='salon-photo'></span>";
					if ( typeof salons_data[i]['photo'] != "undefined" ) {
						curphoto = "<a class='salon-photo fancy' href='" + salons_data[i]['photo'] + "'><img src='/imgs/salon_photo.gif' alt='' /></a>";
					}
					tmpStr += "<div class='salons-item'>" + curphoto + "<div class='salon-info'><p class='salon-name'><strong>" + salons_data[i]['name'] + "</strong></p><div class='salon-address'>" + salons_data[i]['address'] + "</div></div></div>";
					j++;
					if ( j == 2 ) {
						j = 0;
						tmpStr += "<div class='spacer'></div>";
					}
				}
			}
			tmpStr += "<div class='spacer'></div>";
			$('#salonsContent').html(tmpStr);
			$("a.fancy").fancybox();
		});
	} else {
		$('#citiesSelect').remove();
	}
}
/*if($('.catalog-main').length) {
$($('.catalog-main li')[$('.catalog-main li').length-1]).after($($('.catalog-main li')[$('.catalog-main li').length-2]));
}*/
/*630x540*/
$('#mapflash').flash({
swf: '/flash/map.swf',
width: 800,
height: 550
});
if ($('.root-item-selected').length) {
$('.root-item-selected').next('ul:first').css('display','block');
}
	if ( $('.eq-header').length > 0 ) {
		$('.eq-header').each( function(i) {
			$(this).click( function() {
				if ( !$(this).hasClass('eq-header-active') ) {
					$(this).addClass('eq-header-active');
					$($('.modules-content')[i]).css('display', 'block');
				} else {
					$(this).removeClass('eq-header-active');
					$($('.modules-content')[i]).css('display', 'none');
				}
			});
		});
	}
	$('.fragment-point').each( function(i) {
		$(this).click( function() {
			if ( $($('.fragment-image')[i]).css('display') != 'block' ) {
				$(this).addClass('active-point');
				$($('.fragment-image')[i]).css('display', 'block');
			} else {
				$(this).removeClass('active-point');
				$($('.fragment-image')[i]).css('display', 'none');
			}
		});
	});
	$("a.fancy").fancybox({
		/*'padding': 0,
		'margin': 0*/
	});
	$('.sub-menu>li>a').click( function(event) {
		if ( $(this).parents('li').find('ul').length == 1 ) {
			if ( $(this).parents('li').find('ul').css('display') == 'block' ) {
				$(this).parents('li').find('ul').css('display', 'none');
			}
			else {
				$('.sub-menu ul').css('display', 'none');
				$(this).parents('li').find('ul').css('display', 'block');
			}
			event.preventDefault();
		}
	});
	//alert(randoms.length);
	var prevRandomSection = 0;
	var prevRandomPhoto = 0;
	var curRandomSection = 1;
	var curRandomPhoto = 0;
if ( typeof randoms != 'undefined' ) {
	/*$('.model-photo').each( function(i) {
		$($(this).find('img')[parseInt( Math.random() * randoms[i] )]).css('zIndex', 2);
	});*/
	if ( randoms.length > 1 ) {
		var test = 0;
		for ( var k = 0; k < randoms.length; k++ ) {
			test += randoms[k];
		}
		if ( (test - 1) > randoms.length ) {
			setInterval( function() {
				while( curRandomSection == prevRandomSection || randoms[curRandomSection] == 0 ) {// && randoms[curRandomSection] < 2
					curRandomSection = parseInt ( Math.random() * (randoms.length - 1) ) + 1;
				}
				/*$( $($('.model-photo')[curRandomSection]).find('img') ).each( function(i) {
					if( $(this).css('zIndex') == 2 ) {
						prevRandomPhoto = i;
					}
				});*/
				var loop_count = 0;
				while( curRandomPhoto == prevRandomPhoto && loop_count < 10 ) {
					loop_count++;
					curRandomPhoto = parseInt( Math.random() * randoms[curRandomSection] );
				}
				/* $( $($('.model-photo')[curRandomSection]).find('img:first') ).attr('src', images[curRandomSection][curRandomPhoto]);*/
				$( $($('.model-photo')[curRandomSection]).find('a:first') ).append("<img src=\""+images[curRandomSection][curRandomPhoto]+"\" style=\"opacity:0;\"  width=\"260\" height=\"195\">");
				$( $($('.model-photo')[curRandomSection]).find('img:last') ).animate({opacity:1}, 2000,
				function()
				{
					$( $($('.model-photo')[curRandomSection]).find('img:first') ).remove();
				});

				/*$( $($('.model-photo')[curRandomSection]).find('img') ).each( function(i) {
					if( i == curRandomPhoto ) {
						$(this).css('zIndex', 2);
					}
					else {
						$(this).css('zIndex', 1);
					}
				});*/
				prevRandomSection = curRandomSection;
				prevRandomPhoto = curRandomPhoto;
			}, 3000);
		}
	}
}
	if ( $('#slider img').length > 1 ) {
		sliderTimer = setInterval( function() {
		}, 0);
		$(document).mousemove( function() {
			clearInterval(sliderTimer);
			sliderTimer = setInterval( function() {
				$('.model-photos div').each( function(j) {
					if ( $(this).css('zIndex') == 2 ) {
						$('.model-photos div').css('zIndex', 1);
						if ( ( j + 1 ) == $('.model-photos div').length ) {
							$($('.model-photos div')[0]).css('zIndex', 2);
						} else {
							$($('.model-photos div')[j + 1]).css('zIndex', 2);
						}
						return false;
					}
				});
			}, 7000);
		});
		$('#slider img').each( function(i) {
			$(this).click( function() {
				$('.model-photos div').css('zIndex', 1);
				$($('.model-photos div')[i]).css('zIndex', 2);
				clearInterval(sliderTimer);
				sliderTimer = setInterval( function() {
					$('.model-photos div').each( function(j) {
						if ( $(this).css('zIndex') == 2 ) {
							$('.model-photos div').css('zIndex', 1);
							if ( ( j + 1 ) == $('.model-photos div').length ) {
								$($('.model-photos div')[0]).css('zIndex', 2);
							} else {
								$($('.model-photos div')[j + 1]).css('zIndex', 2);
							}
							return false;
						}
					});
				}, 7000);
			});
		});
		sliderTimer = setInterval( function() {
			$('.model-photos div').each( function(i) {
				if ( $(this).css('zIndex') == 2 ) {
					$('.model-photos div').css('zIndex', 1);
					if ( ( i + 1 ) == $('.model-photos div').length ) {
						$($('.model-photos div')[0]).css('zIndex', 2);
					} else {
						$($('.model-photos div')[i + 1]).css('zIndex', 2);
					}
					return false;
				}
			});
		}, 7000);
	}
});

$(function(){
	$('#slider').jScrollPane({showArrows: true});
});
