/*
7ya.lv js library
version 10 nov 2010
*/
var lib7ya = "7ya-services.20101110.php";
$(document).ready(function(){
	$("#Moods").css("display", "none");
// BEGIN: Toggle moods block
	$("a.buttonMoods").click(function(){
		if($("#Moods").is(":empty")) {
			$.ajax({
				url: lib7ya,
				type: "POST",
				data: ({
					type : "moods",
					action: "load"
				}),
				dataType: "html",
				success: function(data){
					if(data){
						$("#Moods").html(data).slideDown("slow");
					}
				}
			});
		} else {
			if ($("#Moods").is(":hidden")) {
				$("#Moods").slideDown("slow");
			} else {
				$("#Moods").slideUp("slow");
			}
		}
		return false;
	});
// END: Toggle moods block

// BEGIN: Set mood
	$("ul.listMoods li a").live('click', function(){
		var mood_id = $(this).attr("mood-id");
		var mood_image = $(this).css("backgroundImage").replace(/^url|[\(\)]/g, '').replace(/"/g,'');
		$.ajax({
			url: lib7ya,
			type: "POST",
			data: ({
				type : "moods",
				action: "set",
				mood_id: mood_id
			}),
			dataType: "html",
			success: function(user_id){
				if ($("td.moodimg" + user_id + " img").length) {
					$("td.moodimg" + user_id + " img").attr("src", mood_image);
				} else {
					$("td.moodimg" + user_id).append('<img src="' + mood_image + '" />');
				}
				$("#Moods").slideUp("slow");
			}
		});
		return false;
	});
// END: Set mood

// BEGIN: Reset mood
	$("div.resetMoods input").live('click', function(){
		$.ajax({
			url: lib7ya,
			type: "POST",
			data: ({
				type : "moods",
				action: "set",
				mood_id: 0
			}),
			dataType: "html",
			success: function(user_id){
				$("td.moodimg" + user_id + " img").remove();
				$("#Moods").slideUp("slow");
			}
		});
		return false;
	});
// END: Reset mood

// BEGIN: Mood status
	$("a.buttonMoods").bind('mouseenter mouseleave', function(e){
		var offset = $(this).offset();
		if (e.type=='mouseenter'){
			$.ajax({
				url: lib7ya,
				type: "POST",
				data: ({
					type : "moods",
					action: "status"
				}),
				dataType: "html",
				success: function(img){
						var obj = $("#MoodStatus");
						var html = '';
						if(img!='') {
							html = '<div style="text-align: center"><strong>Моё настроение</strong><br /><img src="images/moods/' + img + '" /></div>';
						} else {
							html = '<div style="text-align: center"><strong>Моё настроение</strong><br />Не в настроении</div>';
						}
						obj.html(html);
						obj.css({
							"top": offset.top+30,
							"left": offset.left+3,
							"backgroundColor":"#ffffff",
							"width": "150px",
							"zIndex":"10000",
							"position":"absolute",
							"overflow":"visible",
							"borderWidth":"1px",
							"borderStyle":"solid",
							"borderColor":"#5A8C35"
						}).show();
				}
			});
		} else if (e.type=='mouseleave'){
			$("#MoodStatus").empty().hide();
		}
	});
// END: Mood status

// BEGIN: Feedback
	var name = $("#name"),
		email = $("#email"),
		message = $("#message"),
		allFields = $([]).add(name).add(email).add(message),
		tips = $(".validateTips");

	function updateTips(t) {
		tips
			.text(t)
			.addClass('ui-state-highlight');
		setTimeout(function() {
			tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			// updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			updateTips("Длина текста должна быть между "+min+" и "+max+" символами.");
			return false;
		} else {
			return true;
		}

	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}
	
	$("#feedback-form").dialog({
		autoOpen: false,
		height: 360,
		width: 350,
		modal: true,
		buttons: {
			'Отправить': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"username",3,16);
				bValid = bValid && checkLength(email,"email",6,100);
				bValid = bValid && checkLength(message,"message",5,255);

				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
				if (bValid) {
					$.post("feedback.php", $("#feedback-form form").serialize(), function(data) {
							alert(data);
					});
					$(this).dialog('close');
				}
			},
			'Закрыть': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	$('a.feedback-button').click(function() {
		$('#feedback-form').dialog('open');
	});
// END: Feedback
});
