// JavaScript Document
var file="/ajax.php";
var cache=new Array();
var ratingAnswers=[, 'Ужасно', 'Плохо', 'Так себе', 'Хорошо', 'Супер']

$(document).ready(function() { 
	
	$('img[src$=.png], #window').ifixpng();
   	
   $("#loading").ajaxStart(function() {
		$(this).fadeIn('slow');
   }).ajaxStop(function() {
		$(this).fadeOut('slow');
   });
		
	$("#gallery img").click(function() {
		$("#loading2, #window, #menu").fadeOut('slow').remove();
		$id=$(this).attr('class');		
		$request="getImageInfo=" + $id;
		
		$.post(file, $request, function(html) { // load info about image
			$menu=$("<div id='menu'>"+html+"</div>");
			$close=$("<img id='closeWindow' src='/admin/i/delete.png' alt='Закрыть' title='Закрыть' />")
			$loading=$("<div align='center' id='loading2'><img src='/i/loader.jpg' alt='Загрузка изображения...' /></div>")
			$window=$("<div id='window'></div>");
			$img = $("<img src='/i/gallery/"+$id+".jpg' />"); // create image	
			$("#gallery").after($window).after($loading).after($menu);
			$menu.hide();
			$window.append($close).append($img).hide(); // append and hide image
			$loading.fadeIn('slow');
		
			if ($img[0].complete) // fix for ie, opera
				createWindow($img);
			else {
				$img.load(function() { // when image is loaded
					createWindow($img);							
				});
			}	
		});						 
	});
	
	$("#gallery img").css({"opacity": 0.7});
	
	$("#gallery img").hover(function() {
	    $(this).css({'border' : '1px solid #006dff', "opacity": 1});
	}, function () {
		$(this).css({'border' : '1px solid #CCC', "opacity":0.7});
	});
	
	$("#closeWindow").live('click', function() {
		$("#window, #menu").fadeOut('slow', function() {
			$("#loading2, #menu").remove();
			$(this).remove();
		});
	});
	
	$(".StartT img").hover(function() {
		if($(this).parent().is('a')) {		
			$link=$(this).parent().attr('href').replace(/[\\\/]/g, "");
			$(this).attr('src', "/i/buttons/"+$link+"1.jpg");
		}
	}, function () {
		if($(this).parent().is('a')) {
			$link=$(this).parent().attr('href').replace(/[\\\/]/g, "");
			$(this).attr('src', "/i/buttons/"+$link+".jpg");
		}
	});
		
	$("#topMenu a img").hover(function() {		
		$link=$(this).parent().attr('href').replace(/[\\\/]/g, "");		
		$(this).attr('src', '/i/buttons/'+$link+'3.jpg');
	}, function () {
		$link=$(this).parent().attr('href').replace(/[\\\/]/g, "");							
		$(this).attr('src', '/i/buttons/'+$link+'2.jpg');
	});
		
    $("#q").click(function() {
		var optDialog= {
				modal: true, 
				resizable:false,
				title: "Написать отзыв",
				width: 350,
				buttons: {
				"Сохранить": save,
				"Отмена": function() { $(this).dialog("destroy");}
				}
			}
		$request='getQuestion=go';
		$.post(file, $request, function(html) {
			$("#windowTalk").dialog(optDialog).dialog("open").html('<form id="form"><div>E-mail</div><div><input name="email" id="email" type="text" /></div><div>Вопрос</div><div><textarea name="question" id="question" cols="5" rows="5"></textarea></div><div id="error"></div></form>');
			$("#question").after("<br /><br />"+html);
		});	
	});
	
	// rating
	
	$("#makeRating").live("mouseover", function(event) {
		if ($(event.target).is('img')) {
			$("#titleRating").text(ratingAnswers[$(event.target).prevAll().length+1]);
			$("#makeRating img").attr('src', '/i/starOFF.png')
			$(event.target).prevAll().andSelf().attr('src', '/i/starON.png');
		}
	});
	
	$("#makeRating").live("mouseout", function(event) {
		if (event.target == this) {
			$("#titleRating").text('Проголосовать');
			$("#makeRating img").attr('src', '/i/starOFF.png');
		}
	});
	
	$("#makeRating img").live("click", function () {
		$rating=$(this).prevAll().length+1;
		$id=$("#id").val();
		$request='sendRating=' + $rating + "&id=" + $id;
		$.post(file, $request, function(html) {
			$("#menu").html(html);
			$("#titleRating").text(ratingAnswers[$("#titleRating").text()]);
		});	
	});
	// rating
	
	$("#mt").before($(".StartT"));
	
});

function save() {
	$("#error").text('');
	var pattern=/^(([\w.])+\@(([\w])+\.)[a-zA-Z0-9]{2,6})?$/gi;
	var $question=$("#question").val();
	
	pattern.test($("#email").val()) ? null : $("#error").append('<div>Неверно введен e-mail</div>'); 
	
	if($question!="")
		$question.length < 10 ? $("#error").append('<div>Cлишком короткий отзыв</div>') : null; 
	else 
		$("#error").append('<div>Введите текст отзыва</div>');

	$("#answer").val()=="" ? $("#error").append('<div>Ответьте на вопрос антиспама</div>') : null;

	if($("#error").text()!="")
		return;
		
	$.post(file, $("#form").serialize()+"&save=do", function(html) {		
		if(html=='errorAnswer') {
			$("#error").append('<div>Вы неверно ответили на вопрос антиспама</div>')
			return;
		} else	{	
			var optDialogNew= {
				modal: true, 
				resizable:false,
				title: "Задать вопрос",
				width: 350,
				buttons: {
				"OK": function() { $(this).dialog("destroy");}
				}
			}
			$("#windowTalk").dialog("destroy").dialog(optDialogNew).dialog("open").html(html);
		}
	});
}

function createWindow($img) {
	$loading.fadeOut('slow', function() {
		$("#menu, #window").fadeIn('slow');
		var position = $("#menu").position().top + 267;
		$("#menu").css({"top":position+"px"}).fadeIn('slow');
		if($("#titleRating").text()!="Проголосовать") $("#titleRating").text(ratingAnswers[$("#titleRating").text()]);
		$marginLeft=$img.width()/2;
		$marginTop=$img.height()/2;
		$img.css({"position":"relative", "left":"50%", "top":"50%", "margin-left" : "-"+$marginLeft+"px", "margin-top" : "-"+$marginTop+"px"});
	});			
}