/* News tape on main page */
var animate_news;
GoToPrevPage = function(el){
	$(el).find(".forward").click();
	animate_news = setTimeout("GoToPrevPage('#news_tape_list')", 3000);
}
function formatText(index, panel) {
	return index + "";
}
$(function () {
	$('#news_tape_list').anythingSlider({
		easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
		autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
		delay: 3000,                    // How long between slide transitions in AutoPlay mode
		startStopped: false,            // If autoPlay is on, this can force it to start stopped
		animationTime: 600,             // How long the slide transition takes
		hashTags: true,                 // Should links change the hashtag in the URL?
		buildNavigation: false,          // If true, builds and list of anchor links to link to each slide
		pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
		startText: "Play",             // Start text
		stopText: "Stop",               // Stop text
		navigationFormatter: formatText       // Details at the top of the file on this use (advanced use)
	});
});
/* */

$(document).ready(
	function(){
		/*
			COVER
		*/
		$("#content .cover .title").click(
			function(){
				$(this).parent(".cover").toggleClass("active");
			}
		);
		/**/
		/* Flash */
			var flashvars = {};
			var params = {
				wmode: "transparent"
			}
			swfobject.embedSWF("/skins/default/swf/header_3.swf", "visual_img", "1126", "260", "9.0.0", "", flashvars, params); 
			
		/* */
		/* Video */
			/* Flash Player */
			var flashvars = {
				videoURL: "/skins/default/video/build_technology.flv",
				previewURL: "/skins/default/images/photo/video.jpg"
			};
			var params = {
				wmode: "window"
			}
			swfobject.embedSWF("/skins/default/swf/videoPlayer.swf", "flash_place", "520", "370", "9.0.0", "", flashvars, params); 
			var flashvars = {
				videoURL: "/skins/default/video/build.flv",
				previewURL: "/skins/default/images/photo/video2.jpg"
			};
			var params = {
				wmode: "window"
			}
			swfobject.embedSWF("/skins/default/swf/videoPlayer.swf", "flash_place2", "520", "370", "9.0.0", "", flashvars, params); 
		/* */
		/* Calendar */
		$('input.date').each ( function() {
			$(this).attachDatepicker({
				clearText: '<span>Очистить поле</span>', // Display text for clear link
				closeText: '<span>Закрыть</span>', // Display text for close link
				currentText: '<span>сегодня</span>', // Display text for current month link
				prevText: '&#x3c;<span>пред</span>', // Display text for previous month link
				nextText: '<span>след</span>&#x3e;', // Display text for next month link
				yearRange: "2009:2015"
			});
		});
		$(".date_filter .icon_calendar").click(
			function(){
				var index = $(".date_filter .icon_calendar").index(this);
				$("input.date").eq(index).focus();
			}
		);
		/* */
		//Cufon.replace(".cufon",{hover:"true"});
		/* IE popup */
		$("#popup-content h2 span").click(function(){
			$("#iepopup").hide();
		});
		/* */
		/* Main menu */
		$("#menu_main li").hover(
			function(){
				if (!$(this).hasClass("active")){
					$(this).find("img").css("top","-28px")
				}
			},
			function(){
				if (!$(this).hasClass("active")){
					$(this).find("img").css("top","0px")
				}
			}
		);
		$("#menu_main li").click(
			function(){
				if (!$(this).hasClass("active")){
					href = $(this).find("a").attr("href");
					window.location = href;
				}
			}
		);
		/* */
		/* Sections menu */
		$("#menu_sections li").hover(
			function(){
				$("#menu_sections li").removeClass("active");
				$(this).addClass("active");
			},
			function(){
				$(this).removeClass("active");
			}
		);
		/* */
		/* Benner top */
		//var top_y = parseInt($("#animated_bnr .bnr_top_wrapp").css("top"));
		var top_y = -130;
		var top_pos = 0;
		$("#animated_bnr .bnr_top_wrapp").mouseover(
			function(){
				$("#animated_bnr .bnr_top_wrapp").stop()
				$("#animated_bnr .bnr_top_wrapp").animate({"top":top_pos}, 1000);
			}
		);
		$("#animated_bnr .bnr_top_wrapp").mouseout(
			function(){
				$("#animated_bnr .bnr_top_wrapp").stop()
				$("#animated_bnr .bnr_top_wrapp").animate({"top":top_y}, 1000);
			}
		);
		setTimeout('$("#animated_bnr .bnr_top_wrapp").animate({"top":'+top_y+'}, 0);flag_first=false', 0)
		/* */
		/* News list */
		animate_news = setTimeout("GoToPrevPage('#news_tape_list')", 1000);
		$("#news_tape_list").hover(
			function(){
				clearTimeout(animate_news);
			},
			function(){
				animate_news = setTimeout("GoToPrevPage('#news_tape_list')", 1000);
			}
		);
		/* */
		/* Cover */
			$(".elem_cover_list .elem_content").hide();
			$(".elem_cover_list h3").click(
				function(){
					$(".elem_cover_list .elem_content").removeClass("active");
					$(this).parents(".elem").find(".elem_content").toggle();
				}
			);
			$(".elem_cover_list h3 span").hover(
				function(){
					$(this).css({
						"color":"#333",
						"border-color":"#333"
					});
					$("span.black").css({
						"color":"#333"
					});
				},
				function(){
					$(this).css({
						"color":"#EE2125",
						"border-color":"#EE2125"
					});
					$("span.black").css({
						"color":"#333"
					});
				}
			);
		/* */
		/* Fancybox */
		$("a.fancy").fancybox({
			'titlePosition' : 'over'
		});
		/* */
		/* Projects */
		$(".houses_params p").hover(
			function(){
				$(".houses_params p").removeClass("hover");
				$(this).addClass("hover");
			},
			function(){
				$(".houses_params p").removeClass("hover");
			}
		);
		/* */
		/* Validation */
		$("input.only_nums").change(function() {			
			var val = $(this).val();
			val = val.replace(/[^\d]+/g,"");
			$(this).val(val);
		});
		$("input.only_cyr").change(function() {			
			var val = $(this).val();
			val = val.replace(/[^\-\,\sа-яА-Я\.]+/g,"");
			$(this).val(val);
		});

		$("input.only_nums").keyup(function() {			
			var val = $(this).val();
			val = val.replace(/[^\d]+/g,"");
			$(this).val(val);
		});
		$("input.only_cyr").keyup(function() {			
			var val = $(this).val();
			val = val.replace(/[^\-\,\sа-яА-Я\.]+/g,"");
			$(this).val(val);
		});

		$("input.only_name").keyup(function() {			
			var val = $(this).val();			
			val = val.replace(/[^\-\,\sа-яА-Я\d\.'"]+/g,"");
			$(this).val(val);
		});

		/* */
		/* Search */
		//действия при установке фокуса в поле ввода
			$("input#top_search_form").focus(function(){
				if ( $(this).attr("title") == $(this).attr("value") ){
					$(this).attr({"value": ""});
				}
			});
		//действия при убирании фокуса из поля ввода
			$("input#top_search_form").blur(function(){
				 if ( !$(this).attr("value")){
					var defText = $(this).attr("title");
					$(this).attr({"value": defText});
					}
			});
		//действия при нажатии на кнопку поиска
			$("#search_header_button").click(function(){
				if ( $("#top_search_form").attr("title") == $("#top_search_form").attr("value") ){
					return false;
				}
			});
		/* */
		/* Tooltip */
		$(".tooltip").tooltip({
			track: true,
			showURL: false,
			bodyHandler: function() { 
				return $("<img/>").attr("src", this.rel); 
			}
			
		});
		/* */
		/* Project detail page with jQuery UI */
		MAX = 100;
		floor_count = $("#floors div.floor").length - 1;
		step = Math.floor(MAX/floor_count);
		$("#floors div").eq(0).css({"opacity":1,"z-index":11});
		$("#slider").slider({
			orientation: "vertical",
			min: 0,
			max: MAX,
			range: "min",
			slide: function(event, ui) {
				opacity = ui.value/step;
				floor = Math.floor(opacity);
				if (floor == floor_count){
					$("#floor"+floor+1).css("opacity",1);
				}
				else{
					$("#floors div").css("opacity",0);
					$("#floors div").eq(floor).css("opacity", (floor+1)-opacity);
					$("#floors div").eq(floor+1).css("opacity",opacity - floor);
				}
			}
		});
		$(".floor_names li").click(
			function(){
				index = floor_count - $(".floor_names li").index(this);
				$("#floors div").css("opacity",0);
				$("#floors div").eq(index).css("opacity", 1);
				index = index*step;
				$("#slider").slider({
					value: index
				});
				
			}
		);
		/* */
		/* Target="_blank" */
		$("a[href$='pdf'], a[href$='doc']").addClass("newpage");
		$.each($("a.newpage"), function() {
			var ocd = $(this).attr("onclick") != undefined;
			var oct = ocd ? $(this).attr("onclick").toString() : "";
			if (oct.indexOf("window.open") == -1) {
				$(this).click(function() {
					window.open($(this).attr("href"));
					return false;
				});
			}
		});
		/* */
		/*
			Bug in Safari & Chrome with rotation text (price in left column)
		*/
		if ($.browser.safari) {
			$("#price_left object").hide();
			$("#price_left").append('<img class="iline1" type="image/svg+xml" src="data:image/svg+xml; charset=utf-8,<svg xmlns=\'http://www.w3.org/2000/svg\'><text x=\'67\' y=\'30\' font-family=\'Arial\' font-size=\'14\' fill=\'#fff\' transform=\'rotate(11)\' text-rendering=\'optimizeSpeed\'>СКАЧАТЬ ПРАЙС-ЛИСТ</text></svg>"><img class="iline2" type="image/svg+xml" src="data:image/svg+xml; charset=utf-8,<svg xmlns=\'http://www.w3.org/2000/svg\'><text x=\'127\' y=\'-18\' font-family=\'Arial\' font-size=\'14\' fill=\'#fff\' transform=\'rotate(11)\' text-rendering=\'optimizeSpeed\'>.xls (1.14 МБ)</text></svg>">');
		}
		/**/
		focusBlur();
	}
);

function is_email(email){
	var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
	if(result > -1){ return true; } else { return false; }
}

/*
	Focus/blur form
*/
focusBlur = function(){
	inputElem = $(".form_calc input.text");
	var rememberVal = new Array();
	inputElem.each(
		function(i){
			rememberVal[i] = inputElem.eq(i).attr('value');
		}
	);
	inputElem.focus(function(){
		index = inputElem.index(this);
		$(this).attr('value','');
		/*$(this).keypress(
			function(){
				rememberVal[index] = inputElem.eq(index).attr('value');
			}
		);*/
	});
	inputElem.keyup(function(){
		index = inputElem.index(this);
		txt = $(this).val();
		rememberVal[index] = txt;
		if (txt == '') { inputElem.eq(index).attr('value', 0); }
		else { inputElem.eq(index).attr('value', txt); }
	});
	inputElem.blur(function(){
		index = inputElem.index(this);
		$(this).attr('value',rememberVal[index]);
	});
}
