var bookmarkurl="http://www.ukrshops.com.ua/";
var bookmarktitle="Аудио, видео, бытовая техника - цены и описания в Украине";

function addbookmark(){
	if (document.all)
		window.external.AddFavorite(bookmarkurl,bookmarktitle);
}

function loginDialog()
{
	$('#loginDialog input').css({backgroundColor:'white'});
	$('#loginDialog').dialog('destroy');
	$('#loginDialog').dialog({
		modal: true,
		resizable: false,
		show: 'fold',
		width: '350px',
		title: 'Вход в кабинет',
		buttons:{
			'Войти' : function(){
				var data = 'act=login&'+$('#loginDialog form').serialize();

				$.post('/index.php', data, function(response){
					if(response == true)
					{
						$('#loginDialog').dialog('close');
						top.location = '/';
					}
					else
					//Ошибка
						$('#loginDialog input').animate({backgroundColor:'#ffaaaa'}, 300);

				});
			},
			'Отмена' : function(){
				$('#loginDialog').dialog('close');
			}
		}
	});
}

function remindDialog()
{
	$('#remindDialog input').css({backgroundColor:'white'});
	$('#remindDialog').dialog('destroy');
	$('#remindDialog').dialog({
		modal: true,
		resizable: false,
		show: 'fold',
		width: '350px',
		title: 'Восстановление забытого пароля',
		buttons:{
			'Напомнить' : function(){
				var data = 'act=remindpassword&'+$('#remindDialog form').serialize();

				$.post('/index.php', data, function(response){
					if(response == true)
						$('#remindDialog input').animate({backgroundColor:'#aaffaa'}, 500)
					.animate({backgroundColor:'white'}, 500, function(){
						$('#remindDialog').dialog('close');
					});
					else
					//Ошибка
						$('#remindDialog input').animate({backgroundColor:'#ffaaaa'}, 500)
					.animate({backgroundColor:'white'}, 500);

				});
			},
			'Отмена' : function(){
				$('#remindDialog').dialog('close');
			}
		}
	});
}

function recoveryDialog()
{
	$('#recoveryDialog input').css({backgroundColor:'white'});
	$('#recoveryDialog').dialog('destroy');
	$('#recoveryDialog').dialog({
		modal: true,
		resizable: false,
		show: 'fold',
		width: '350px',
		title: 'Восстановление забытого пароля',
		buttons:{
			'Изменить' : function(){
				var password = $('#recovery-password').attr('value');
				var password2 = $('#recovery-password2').attr('value');
				if(password.length == 0 || (password != password2))
					$('#recoveryDialog input').animate({backgroundColor:'#ffaaaa'}, 500)
				.animate({backgroundColor:'white'}, 500);
				else
				{
					var data = 'act=recoverypassword&'+$('#recoveryDialog form').serialize();

					$.post('/index.php', data, function(response){
						if(response == true)
							$('#recoveryDialog input').animate({backgroundColor:'#aaffaa'}, 500)
						.animate({backgroundColor:'white'}, 500, function(){
							$('#recoveryDialog').dialog('close');
							top.location = '/';
						});
						else
						//Ошибка
							$('#recoveryDialog input').animate({backgroundColor:'#ffaaaa'}, 500)
						.animate({backgroundColor:'white'}, 500);
					});
				}
			}
		}
	});
}

function regDialog()
{
	var tipNickName = $('#regDialog #descr-nickname').html();
	var tipEmail = $('#regDialog #descr-email').html();
	var tipPassword = $('#regDialog #descr-password').html();

	$('#regDialog td').css({color:'#444444'});
	$('#regDialog #descr-nickname').html(tipNickName);
	$('#regDialog #descr-email').html(tipEmail);
	$('#regDialog #descr-password').html(tipPassword);

	$('#regDialog').dialog('destroy');
	$('#regDialog').dialog({
		modal: true,
		resizable: false,
		show: 'fold',
		width: '700px',
		title: 'Регистрация',
		buttons:{
			'Зарегистрироваться' : function(){
				$('#regDialog td').css({color:'#444444'});
				$('#descr-nickname').html(tipNickName);
				$('#descr-email').html(tipEmail);
				$('#descr-password').html(tipPassword);

				if($('#reg-password').attr('value') != $('#reg-password2').attr('value'))
				{
					//Пароли не совпадают
					$('#reg-password').animate({backgroundColor:'#ffaaaa'}, 500)
					.animate({backgroundColor:'white'}, 500);
					$('#reg-password2').animate({backgroundColor:'#ffaaaa'}, 500)
					.animate({backgroundColor:'white'}, 500);
					$('#descr-password').animate({opacity:'0'}, 100)
					.css({color:'#ff3333'})
					.text('Пароли не совпадают.')
					.animate({opacity:'1'}, 500);
					return;
				}
				var data = 'act=reg&'+$('#regDialog form').serialize();

				$.post('/index.php', data, function(response){
					if(response == 0)
					{
						//Автологин
						data = 'act=login&'+$('#regDialog form').serialize();
						$.post('/index.php', data, function(){
							$('#regDialog').dialog('close');
							top.location.reload(true);
						});
					}
					if(response == 1)
					{
						//Email уже используется
						$('#reg-email').animate({backgroundColor:'#ffaaaa'}, 500)
						.animate({backgroundColor:'white'}, 500);

						prev_text = $('#descr-email').text();
						$('#descr-email').animate({opacity:'0'}, 100)
						.css({color:'#ff3333'})
						.text('Указанный адрес электронной почты уже используется.')
						.animate({opacity:'1'}, 500);
					}
					if(response == 2)
					{
						//Email неправельный
						$('#reg-email').animate({backgroundColor:'#ffaaaa'}, 500)
						.animate({backgroundColor:'white'}, 500);

						$('#descr-email').animate({opacity:'0'}, 100)
						.css({color:'#ff3333'})
						.text('Указанный адрес электронной почты неправильный.')
						.animate({opacity:'1'}, 500);
					}
					if(response == 3)
					{
						//Nickname короткий
						$('#reg-nickname').animate({backgroundColor:'#ffaaaa'}, 500)
						.animate({backgroundColor:'white'}, 500);
						$('#descr-nickname').animate({opacity:'0'}, 100)
						.css({color:'#ff3333'})
						.text('Длина имени должна составлять от 3 до 30 символов.')
						.animate({opacity:'1'}, 500);
					}
					if(response == 4)
					{
						//Пароль короткий
						$('#reg-password').animate({backgroundColor:'#ffaaaa'}, 500)
						.animate({backgroundColor:'white'}, 500);
						$('#reg-password2').animate({backgroundColor:'#ffaaaa'}, 500)
						.animate({backgroundColor:'white'}, 500);
						$('#descr-password').animate({opacity:'0'}, 100)
						.css({color:'#ff3333'})
						.text('Длина пароля должна составлять от 5 до 16 символов.')
						.animate({opacity:'1'}, 500);
					}
				});
			},
			'Отмена' : function(){
				$('#regDialog').dialog('close');
			}
		}
	});
}

function settingsDialog()
{
	$("#tabs").tabs();
	$('#settingsDialog').dialog('destroy');
	$.post('/index.php','act=getnickname',function(response){
		$('#nickname-new').attr('value', response);
	});
	$('#settingsDialog').dialog({
		modal: true,
		resizable: false,
		show: 'fold',
		width: '350px',
		title: 'Настройки профиля',
		buttons:{
			'Закрыть' : function(){
				$.post('/index.php', 'act=subscribenews&subscribe_news='+$('#subscribe-news:checked').attr('value'), function()
				{
					$('#settingsDialog').dialog('close');
				});
			}
		}
	});
}

function dialogFastChoice()
{
	$('#fastChoice').css({display:'block'}).animate({opacity:1}, 300);
}
function closeFastChoice()
{
	$('#fastChoice').animate({opacity:0}, 100, 'linear', function(){
		$('#fastChoice').css({display:'none'});
	});
}
var cls;
var ven;

$(document).ready(function() {
	$("#accordion").accordion({
		autoHeight: false,
		icons: {
    			header: "ui-icon-plus",
   				headerSelected: "ui-icon-minus"
		},
		animated: false
	});
	$(".selectable1").selectable({selected: function(){
			cls = $('.ui-selected', this).attr('value');
			$.post('/fastchoice-ajax.php', 'act=vendor&class_id='+cls, function(data){
				$('#selectable3').html('');
				$('#selectable2').stop().css({opacity:0}).html(data).css({opacity:1});
			});
		}});

	$("#selectable2").selectable({selected: function(){
			ven = $("#selectable2 li.ui-selected").attr('value');
			$.post('/fastchoice-ajax.php', 'act=model&class_id='+cls+'&vendor_id='+ven, function(data){
				$('#selectable3').stop().css({opacity:0}).html(data).css({opacity:1});
			});
		}});
	$("#selectable3").selectable({selected: function(){
			top.location = '/fastchoice-ajax.php?act=redir&good_id='+$("#selectable3 li.ui-selected").attr('value');
		}});
});

//Настройки
function passwordChange()
{
	var password_old = $('#password-old').attr('value');
	var password_new = $('#password-new').attr('value');
	var password_new2 = $('#password-new2').attr('value');

	if(password_new != password_new2 || password_new.length < 4)
	{
		$('#password-new').animate({backgroundColor:'#ffaaaa'}, 500)
		.animate({backgroundColor:'white'}, 500);
		$('#password-new2').animate({backgroundColor:'#ffaaaa'}, 500)
		.animate({backgroundColor:'white'}, 500);
		return;
	}

	$.post('/index.php','act=changepass&password_old='+password_old+'&password_new='+password_new, function(response){
		if(response)
		{
			$('#password-old').animate({backgroundColor:'#aaffaa'}, 500)
			.animate({backgroundColor:'white'}, 500);
			$('#password-new').animate({backgroundColor:'#aaffaa'}, 500)
			.animate({backgroundColor:'white'}, 500);
			$('#password-new2').animate({backgroundColor:'#aaffaa'}, 500)
			.animate({backgroundColor:'white'}, 500);
			$('#password-old').attr('value','');
			$('#password-new').attr('value','');
			$('#password-new2').attr('value','');
		}
		else
		{
			$('#password-old').animate({backgroundColor:'#ffaaaa'}, 500)
			.animate({backgroundColor:'white'}, 500);
		}
	});
}

function nicknameChange()
{
	var nickname = $('#nickname-new').attr('value');
	$.post('/index.php','act=changenickname&nickname='+nickname, function(response){
		if(response)
		{
			$('#nickname-new').animate({backgroundColor:'#aaffaa'}, 500)
			.animate({backgroundColor:'white'}, 500);
		}
		else
		{
			$('#nickname-new').animate({backgroundColor:'#aaffaa'}, 500)
			.animate({backgroundColor:'white'}, 500);
		}
	});
}

function setRegion()
{
	var region = $('input[name=region]:checked').attr('value');
	var delivery_ua = $('input[name=delivery_ua]').attr('checked') ? '1' : '0';

	$.post('/index.php', 'act=setregion&region='+region+'&delivery_ua='+delivery_ua, function(){
		$('#region').animate({backgroundColor:'green'}, 500).animate({backgroundColor:'#f5f8fb'}, 500)
		.animate({opacity:0}, 200, function(){
			$('#region').css({display:'none'});
			window.location.reload(true);
		});
	});
}

var monitorAppend = false;



function monitor()
{
	var price = $('#wish-price').attr('value');
	if(price.length == 0)
		$('#monitor').animate({backgroundColor:'red'}, 500).animate({backgroundColor:'#f5f8fb'}, 500);
	else
	{
		$.post('/index.php', 'act=monitoring&id=<?=$id?>&wish='+price, function()
		{
			$('#monitor').animate({backgroundColor:'green'}, 500).animate({backgroundColor:'#f5f8fb'}, 500)
			.animate({opacity:0}, 200, function(){
				$('#monitor').css({display:'none'});
			});
		});
	}
}

function fc_openclose(id){
	if($('#fcimg'+id).attr('src') == '/images/plus.gif'){
		$('li[param='+id+']').css({display:'block'});
		$('#fcimg'+id).attr({src:'/images/minus.gif'});
	}
	else{
		$('li[param='+id+']').css({display:'none'});
		$('#fcimg'+id).attr({src:'/images/plus.gif'});
	}
}