function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
	{
	return unescape(y);
	}
  }
}

function rateit(rate, session, attr) {
	if (!getCookie('attraction-'+attr)) {
		$.ajax({
			type: "GET",
			url: "/input.xml",
			data:"_current_submit_rate=rate"+
				"&_session_id_rate="+session+
				"&attraction_id="+attr+
				"&attraction_lastrate="+rate+
				"&_forward",

			success: function(html) {
				document.cookie="attraction-"+attr+"="+rate+"; expires=Mon, 01-Jan-2200 00:00:00 GMT";
				$(".rayt span").removeClass('active');
				$(".rayt span").first().addClass( (rate >= 1)? 'active':'' ).next().addClass( (rate >= 2)? 'active':'' ).next().addClass( (rate >= 3)? 'active':'' ).next().addClass( (rate >= 4)? 'active':'' ).next().addClass( (rate >= 5)? 'active':'' );
				//$(".rayt b").html(rate);
				$("#rate_output").html(html);
			}
		});
	} else $("#rate_output").html('Вы уже голосовали');
}

function emailme() {
		if ($('#email').val()==''|$('#message').val()=='') {
			alert('Заполните поля отмеченные звездочкой');
			return;
		}
		$.ajax({
			type: "POST",
			url: "/mailme.xml",
			data:
				"&name="+$('#name').val()+
				"&email="+$('#email').val()+
				"&phone="+$('#phone').val()+
				"&message="+$('#message').val()+
				"&iscopy="+$('#iscopy').attr('checked')+
				"&_forward",

			success: function(html) {
				alert(html.split('<body>').join('').split('</body>').join(''));
			}
		});
}

$.fn.showDarkBg = function(e, timeout){
	$(this).width($(window).width()).height($(window).height());
	if (!timeout) timeout=0;
	if (e=="show") $(this).show(timeout);
	if (e=="hide") $(this).hide(timeout);
}
$.fn.closePopup = function(popupId){
	$("#darkBg, .close").click(function(){
		if ($(popupId).css("display") == "block") {
			$("#darkBg").showDarkBg("hide", 0);
			$(popupId).hide();
		}
	});
}

$.fn.calculatePositionCenter = function(){
	curWindowWidth  = $(window).width();
	curWindowHeight = $(window).height();
	curBlockWidth = $(this).outerWidth();
	curBlockHeight = $(this).outerHeight();
	$(this).css({
		"top"  : (curWindowHeight - curBlockHeight)/2 + $(document).scrollTop(),
		"left" : (curWindowWidth - curBlockWidth)/2
	});
}


$(document).ready(function () {
	$(".phone").click(function(){
		$("#darkBg").showDarkBg("show", 0);
		$("#popup").show();
		$("#popup").calculatePositionCenter();
	});
	$.fn.closePopup(".popup");
	$(".close").click(function(){
		$(this).parent().hide();
		$("#darkBg").showDarkBg("hide", 0);
	});

	$(".rayt span").hover(
		function(){
			qwe = $(".rayt span").index(this) + 1;
			$(".rayt span").removeClass("active1");
			$(".rayt span:lt("+qwe+")").addClass("active1");
		},
		function(){
			$(".rayt span").removeClass("active1");
		});
	
	$("#emailme").click(function(){
		emailme();
	});
});

$(window).resize(function(){
	if ($("#darkBg").length) {	$("#darkBg").width($(window).width()).height($(window).height() + $(window).scrollTop()); }		
	$("#popup").calculatePositionCenter();
});
$(window).scroll(function(){
	if ($("#darkBg").length) {	$("#darkBg").width($(window).width()).height($(window).height() + $(window).scrollTop()); }
	$("#popup").calculatePositionCenter();
});
