Number.prototype.numberFormat = function(separator) {
	var separator = separator || ' ';
	var str = this + '';
	for (var i = str.length - 3; i > 0; i -= 3) {
		str = str.substring(0, i) + separator + str.substring(i);
	}
	return str;
}  

String.prototype.toInt = function() {
	return parseInt(this);
}

$(function() {
	$("body.root").each(function() {
		if ( $( '#root-flash' ).length ) {
			swfobject.embedSWF(
				'/i/main.swf', 'root-flash', '1000', '450', '8.0.0', '',
				{xml: 'xml/rootxml'},
				{wmode: 'transparent'}
			)
		}
		else {
			swfobject.embedSWF(
				'/i/main-en.swf', 'root-flash-en', '1000', '450', '8.0.0', '',
				{xml: 'xml/rootxml'},
				{wmode: 'transparent'}
			)
		}
	})
	$("div#extra-flash").each(function() {
		var extId   = $(this).attr("ext:id");
		var extCalc = $(this).attr("ext:calc");
		var popup   = $(this).attr("ext:popup");

		if(popup) {
			popup = popup.split(' ');
		}

		swfobject.embedSWF(
			'/i/extra.swf', 'extra-flash', '280', '117', '8.0.0', '',
			{
				path: '/xml/extra/?parent_id='+extId,
				url:  extCalc,
				popup: popup
			},
			{wmode: 'transparent'}
		)
	})

	$("div#menuflash").each(function() {


//var banner_select = Math.floor(Math.random()*11);

//if (banner_select < 5)
//{
//		swfobject.embedSWF(
//			'http://www.gulfstream.ru/5_site.swf', 'menuflash', '200', '150', '8.0.0', '',
//			{
//				targeturl: ''
//			},
//			{wmode: 'transparent'}
//		)
//} else {
//		swfobject.embedSWF(
//			'http://www.gulfstream.ru/gulfstream-ohrana_200x150.swf', 'menuflash', '200', '150', '8.0.0', '',
//			{
//				targeturl: ''
//			},
//			{wmode: 'transparent'}
//		)
//		}

		
		})


});

	

$(function() {
	var kw = $("#keyword");
	$("#searchSample").click(function() {
		kw.attr('value', $(this).html());
	});

	$("body.news #content form input[type=submit]").css("display", "none");
	$("body.news #content form select").change(function() {
		this.parentNode.parentNode.submit();
	});

	$("tr").hover(
		function() {$(this).addClass("select");},
		function() {$(this).removeClass("select");}
		);

	$("ul.collapseble li div").hide();

	$("ul.collapseble li").children("a").click(function() {
		$(this).parent().toggleClass("opened").children("div").toggle();
		return false;
	});

	var chrono_form = document.getElementById("pay_to_chrono");
	if(chrono_form)
		chrono_form.submit();

	var map = document.getElementById("addmap");
	if (map && contactsAddress) {
		var regexp = /^[\d\.,\s]+$/g;
		if ( ! regexp.test( contactsAddress ) )
		{
			// Создание экземпляра карты и его привязка к созданному контейнеру
			var map = new YMaps.Map(map);
			map.addControl(new YMaps.ToolBar());
			map.enableScrollZoom();
			// Создание объекта геокодера
			var geocoder = new YMaps.Geocoder(contactsAddress);
			// По завершению геокодирования инициализируем карту первым результатом
			  YMaps.Events.observe(geocoder, geocoder.Events.Load, function (geocoder) {
				if (geocoder.length()) {
					map.addOverlay(geocoder.get(0));
					map.setBounds(geocoder.get(0).getBounds());
				}
			});
		}
		else {
			// Создание экземпляра карты и его привязка к созданному контейнеру
			var map = new YMaps.Map(map);
			map.addControl(new YMaps.ToolBar());
			map.enableScrollZoom();
			var param = contactsAddress.split( ',' );
			param[0] = param[0].replace( ' ', '' );
			param[1] = param[1].replace( ' ', '' );
			var p = new YMaps.GeoPoint(param[0], param[1]);

			map.setCenter(p, 16);
			var options = {
				hasBalloon: true,
				balloonOptions: {
					maxWidth:270
				}
			};
         var placemark = new YMaps.Placemark(p, options);
			placemark.name = "Офис компании Гольфстрим";
			placemark.description = contactsAddr;
			map.addOverlay(placemark);
		}
	}
});

$(function() {
	$("body.faq #navigation ul.faq li").each(function() {
/*
		var ItemId = $(this).attr("id");

		if (ItemId != "allqw" && $("body.faq #content .content ul#"+ItemId+"ans").length == 0) {
//				$(this).hide();
		}

		if ($(this).hasClass("current")) {
			$("body.faq #content .content ul#"+ItemId+"ans").show();
		}

		$(this).click(function() {
			$("body.faq #content .content ul li").removeClass("opened").children("div").hide();
			$(this).addClass("current").siblings("li").removeClass("current");

			if (ItemId == "allqw") {
				$("body.faq #content .content ul").show();
			} else {
				$("body.faq #content .content ul").hide();
				$("body.faq #content .content ul#"+ItemId+"ans").show();
			}
		})
*/
	})

	if ($("body.faq #navigation ul li#allqw").hasClass("current")) {
		$("body.faq #content .content ul").show();
	}
})

$(function() {
	if($("dl.serv-block dt").length == 1) {
		$("dl.serv-block dt").next("dd").children("img.topcorn").attr("src", "/i/bg/serv-block-top.gif");
	}

	$('dl.serv-block dt').click(function(){
		$(this).siblings().removeClass('selected').end().next('dd').andSelf().addClass('selected');

		if ($(this).next("dd").hasClass("opt")) {
			$("dd img.topcorn").attr("src", "/i/bg/serv-block-top-1.gif");
		} else {
			$("dd img.topcorn").attr("src", "/i/bg/serv-block-top.gif");
		}
	});
});

$(function() {
	function hideExtra(node) {
		$('#content').removeClass('hide_extra_equipment');

		if($(node).hasClass('hide_extra_equipment')) {
			$('#content').addClass('hide_extra_equipment');
		}
	}

	$('dl.serv-block2 dt').click(function(){
		hideExtra(this);
		$(this).siblings().removeClass('selected').end().next('dd').andSelf().addClass('selected');

		$('.limitation a').each(function(){
			var id = $('dl.serv-block2 dt.selected')[0].className.match(new RegExp('(tab\\d*)'));
			if(id) {
				var href = $(this).attr('href').replace(new RegExp('#(.)*', 'g'), '') + '#' + id[1];
				$(this).attr('href', href);
			}
		});

	}).eq(0).trigger('click');	// click on first tab

	// if hash was passed click on desired tab
	if(location.hash) {
		var curLocation = location.hash.split('#')[1];
		$('dl.serv-block2 dt.' + curLocation).trigger('click');
	}

});



$(function() {
	$("ul.equipment").each(function() {
		var count = $(this).children("li").length;
		var width = Math.floor(715/count);
		$(this).children("li").css("width", width + "px");
	})
})

$(function() {
	$(".archive .current-month").parents("li.by-year").addClass("opened");

	$(".archive .by-year span.year").click(function() {
		$(this).parents("li").toggleClass("opened");
	})
})

$(function() {
	$("form.calc table tr.eq:nth-child(even)").addClass("even");
})

$(function() {
	$("#orderform").addClass("hide");

	$("input#makeorder").click(function() {
		$(this).addClass("hide");
		$("#orderform").removeClass("hide");
		$("#orderform").addClass("show");

	})
})

// calculator form
$(function() {
  	if (!$('form.calc').size()) {
		return false;
	}
  	// base cost
  	var portingBase = ($('input[name=porting]').val()).toInt(),
		priceBase   = ($('input[name=price]').val()).toInt(),
		portingMore = 0,
		priceMore   = 0;

	// helper
	function getCheckboxCounter($checkbox) {
		return $('.am' + $checkbox.attr('id').replace(/eq|service/g, '') + '-count');
	}

	$( '#calcform input[type=text]' )
		.click( function(){
				$( this ).closest( 'tr' ).find( 'input[type=checkbox]' ).attr( 'checked', 'checked' );
			} )
		.blur( function(){
				if ( ! this.value || ! this.value.toInt() ) {
					$( this ).closest( 'tr' ).find( 'input[type=checkbox]' ).removeAttr( 'checked' );
				}
			} );
	// auto-fix values in boolean columns
	$( '#calcform input[type=text].bool' )
			.keyup( function( Evt ){
				if ( $( this ).val() == '' )
				{
					$( this ).val( 0 );
				}
				else if ( $( this ).val().length )
				{
					$( this ).val( 1 );
				}
			} )
			.change( function( Evt ){
				if ( $( this ).val() == '' )
				{
					$( this ).val( 0 );
				}
				else if ( $( this ).val().length )
				{
					$( this ).val( 1 );
				}
			} )

	// switch inside/outside object location
	if ($('input[name=porting]').attr('outside-value') && $('input[name=price]').attr('outside-value')) {
		$('input.outside').change(function(){
			var key = this.checked ? 'outside-value' : 'value';
			portingBase = ($('input[name=porting]').attr(key)).toInt();
			priceBase = ($('input[name=price]').attr(key)).toInt();
			// update labels
			$('.porting_main').text(portingBase.numberFormat());
			$('.tariff_price .price_main').text(priceBase.numberFormat());

			if ($('input[name=servprice]').attr('outside-value')) {
				servBase = ($('input[name=servprice]').attr(key)).toInt();
				$('.price.price_serv').text(servBase.numberFormat());
				$('.add-more.price_serv').attr('price-value', servBase.numberFormat());
			}
			
		});
	}

	// set default count if service enabled/disabled
	$('.add-more').change(function(){
		getCheckboxCounter($(this)).val(this.checked ? 1 : 0);
	});

	// calc additions + update total labels
	var calcTotalPrices = function() {
		portingMore = priceMore = 0;
		$('.add-more:checked').each(function(){
			var $el = $(this),
				$counter = getCheckboxCounter($el),
				elCount = ($counter.size()) ? $counter.val().toInt() : 1;
			if (isNaN(elCount)) {
				elCount = 0;
			}
			portingMore += $el.attr('porting-value').toInt() * elCount;
			priceMore += $el.attr('price-value').toInt() * elCount;
		});

		$('.price-total').text((priceBase + priceMore).numberFormat());
		$('.porting-total').text((portingBase + portingMore).numberFormat());
	};
	
	// update total, if any checkbox change
	$('.calc input:checkbox').change(calcTotalPrices);
	$('.am-count').keyup(calcTotalPrices);

	calcTotalPrices();
})

$(function() {
	openVeil = function() {
		$('body').addClass('veil');
		window.scrollTo(0,0);
		return false;
	}

	$('a.demo-veil').click(function() {
		$('select').hide();
		return openVeil();
	});

	$("a.closeVeil").click(function(){
		$('select').show();
		$("body").removeClass("veil");
		$("div.popupwrapper").removeClass("hide");
		return false;
	});
});


$(function() {
	var addlist = $(".news .additional .other li");
	if (addlist.length == 1) {
		addlist.removeClass();
	}
	if (addlist.length == 0) {
		$("div.other").hide();
	}

	$('.remove-phone').live('click', function(){
		var id = $(this).attr('rel');
		$("#span-divider-" + id).remove();
		return false;
	});

	$('.add-phone').live('click', function() {
		var id = parseInt($("#default-id").val()) + 1;
		$("#addPhone").append('<span class="wrap clear" id="span-divider-' + id + '"><span><span class="wrap left" id="span-left-' + id + '"><label>Удалить: </label></span><span class="wrap right" id="span-right-' + id + '"><input type="text" class="textfield" name="uPhone[]" id="input-' + id + '"><br /><a href="#" class="remove-phone" rel="'+id+'">Удалить</a></div>');
		$("#default-id").val(id);
		return false;
	});
});

$(function() {
	if($.browser.msie) {
		$(".home-choose li.active a span").each(function() {
			$(this).wrapInner("<span style='position: relative; z-index: 20'></span>")
			.css({"position" : "relative"}).children("span").clone().prependTo($(this)).css({"position":"absolute", "top":"-1px", "color":"#424b63", "z-index": "10"});
		})
	}
});

$(function() {
	$(".region-select select").select_skin();
	$(".region-select select").change(function(){$(this).closest('form').submit()});
});

