/**
 *      Основной скриптовый файл.
 *      
 *      @author scailer
 *
 */


function open_form(name) {
	if ($('#'+name).length==0){
		$.ajax({
			url:"/ajax/"+name+"/", 
			success:function(data){$('body').append(data); $('#name_'+name).focus();}, 
			error:function(){alert('К сожелению, произошел сбой при запросе. Пожалуйста, повторите попытку позднее или воспользуйтесь другими средствами связи')}
		});
	} else {
		close_form(name);
	}
}

function close_form(name) {
	$('#'+name).remove()
}

function validate_form(name) {
	if (($('#name_'+name).val() != '')&&($('#phone_'+name).val() != '')&&($('#text_'+name).val() != '')||(($('#phone_'+name).val() != '')&&(name=='certificate'))){
		$('#send_'+name).removeClass('disable');
		return true;
	} else {
		$('#send_'+name).addClass('disable');
		return false;
	}
}

function send_form(name) {
	$.ajax({
		url:"/ajax/"+name+"/",
		data:'name='+$('#name_'+name).val()+'&phone='+$('#phone_'+name).val()+'&text='+$('#text_'+name).val(), 
		type:"POST", 
		success:function(data){$('#'+name+'_block').html(data)}, 
		error:function(){alert('К сожелению, произошел сбой при отправке данных. Пожалуйста, повторите попытку позднее или воспользуйтесь другими средствами связи')}
	})
}

fix_procedure = 0;

function procedure_up_call(id) {
	fix_procedure = id
	$.ajax({
		url:"/ajax/proceduredetails/"+id+"/",
		success:function(data){
			$('#popup').remove(); 
			$('#details_'+id).after(data);
			$('#popup').css('top', $('#details_'+id).parent().position().top-$('#popup').height()+10)
			$('#popup').css('left', $('#details_'+id).parent().position().left+$('#details_'+id).parent().width()/2-$('#popup').width()/2)
		}
	})
}

function procedure_up() {
	id = $(this).attr("id").split('_')[1];
	if (id != fix_procedure) {
		setTimeout("procedure_up_call(id)", 1000);
	}
}

function procedure_down(obj) {
	$('#popup').remove();
	fix_procedure = 0;
}

function procedure_add(id) {
	$('#procedure_'+id).toggleClass('active');
	res = '';
	$('.hover.active').each(function(i,x){res+='&choice='+$(x).attr('value')});
	$.ajax({
		url:"/ajax/choice/",
		data:res,
		success:function(data){
			$('#choice').html(data);
		}
	});
	validate_singup();
}

function validate_singup(){
	if (($('.hover.active').length != 0)&&($('#singup_name').val() != '')&&($('#singup_phone').val() != '')&&($('#singup_date').val() != '')){
		$('#singup_button').removeClass('disable');
		return true
	} else {
		$('#singup_button').addClass('disable');
		return false
	}
}

function singup() {
	if (validate_singup()){
		res = '';
		$('.hover.active').each(function(i,x){res+='&choice='+$(x).attr('value')});
		$.ajax({
			url:"/singup/",
			data:'name='+$('#singup_name').val()+'&phone='+$('#singup_phone').val()+'&date='+$('#singup_date').val()+res,
			type:"POST",
			success:function(data){$('#singup_button').html(data)},
			error:function(){alert('К сожелению, произошел сбой при отправке данных. Пожалуйста, повторите попытку позднее или воспользуйтесь другими средствами связи')}
		})
	}
}

photos = {};
cur_photo = '';

function display_photo_next() {
	here = $('#photo_'+cur_photo);
	if (here.next().length != 0) { $('.next').show(); $('.next').addClass('active');}
	else { $('.next').hide(); $('.next').removeClass('active');};
}

function display_photo_prev() {
	here = $('#photo_'+cur_photo);
	if (here.prev().length != 0) { $('.prev').show(); $('.next').addClass('active');}
	else { $('.prev').hide(); $('.next').removeClass('active');};
}

function hide_photo_arrows() {
	$('.nav a').removeClass('active');
}

function show_photo(id) {
	here = $('#photo_'+id);
	$('.list .active').removeClass('active');
	here.addClass('active');
	$('#main_img').attr('src', photos[id]);
	cur_photo = id;
}

function show_prev_photo() {
	here = $('#photo_'+cur_photo);
	id = here.prev().attr('id').split('_')[1];
	show_photo(id);
}

function show_next_photo() {
	here = $('#photo_'+cur_photo);
	id = here.next().attr('id').split('_')[1];
	show_photo(id);
}

function open_veer() {
	$('#mainveer').attr('src','/layout/img/veer/'+$(this).attr('name'));
}

function close_veer() {
	$('#mainveer').attr('src','/layout/img/veer.jpg');
}

