// file input
function fileInput() {
	var file = $('div.elemBox.file input[type="file"]');
	var inputText = file.parent().next().children('input[type="text"]');

	file.change(function () {
		var fileVal = $(this).val();
		inputText.attr('value', fileVal);
	});
}

// input hint
function inputHint() {
	var $input = $('.test input[type="text"]');

	$(window).load(function () {
    	$input.attr('value', 'введите фразу...')
    });

	var $inputVal = $input.val();

	$input.focus(function () {
		if ( $(this).val() == 'введите фразу...' ) {
			$(this).attr('value', '');
		}
		$(this).removeClass('blur');
	});

	$input.blur(function () {
		if ( $(this).val() == '' ) {
			$(this).attr('value', 'введите фразу...');
			$(this).addClass('blur');
		}
	});
}

// placeholder
function placeholder() {
	if (!$.browser.webkit) {
		$('input[placeholder], textarea[placeholder]').blur(function(){

			if ($(this).val() == '') {
				$(this).val($(this).attr('placeholder'));
				$(this).addClass('m-placeholder');
			}

		}).focus(function(){

			$(this).removeClass('m-placeholder');
			if ($(this).val() == $(this).attr('placeholder')) {
				$(this).val('');
			}

		}).each(function(){

			if ( ($(this).val() == '') || ($(this).val() == $(this).attr('placeholder')) ) {
				$(this).val( $(this).attr('placeholder') );
				$(this).addClass('m-placeholder');
			}

			var form = $(this).closest('FORM');
			if (form.length) {
				form.submit(function(){
					if ($(this).val() == $(this).attr('placeholder')) {
						$(this).val('');
					}
				});
			}
		});
	}
}

// добавление класса каждому второму елементу (можно любому элементу)
function secondClass() {
	$('.test li:nth-child(2n)').addClass('testClass');
}

// add hover class
function hover() {
	$('.mainMenu > li').hover(
		function () {
			$(this).addClass('hover');
		},
		function () {
			$(this).removeClass('hover');
		}
	);
}

// add last class
function lastClass() {
	var $dropMenu = $('.dropMenuBox');
	$dropMenu.each(function () {
		$(this).find('li').last().addClass('last');
	});

	var $sidebarMenu = $('.sidebarMenu ul');
	$sidebarMenu.each(function () {
		$(this).find('li').last().addClass('last');
	});
}

// add first class
function firstClass() {
	$('.test li').first().addClass('first');
}

function phoneSwitch() {
	$('.phones a').click(function () {
		$('.phones p').removeClass('show');
		$('.phones a').removeClass('act');
		if ( $(this).hasClass('mos') ) {
			$('.phones p.mos').addClass('show');
			$(this).addClass('act');
		}

		if ( $(this).hasClass('piter') ) {
			$('.phones p.piter').addClass('show');
			$(this).addClass('act');
		}
		return false;
	});
}

function showHideSidebarMenu() {
	$('.sidebarMenu > li .arrow').click(function () {
		var $li = $(this).parents('li');
		if ( $li.hasClass('open') ) {
			$li.removeClass('open');
			$li.addClass('close');
			return false;
		}
		else if ( $li.hasClass('close') ) {
			$li.removeClass('close');
			$li.addClass('open');
			return false;
		}
	});
}

function seo() {
	$('.seoTitle .arrow').click(function () {
		$('.seoText').toggleClass('hide');
	});
}

function callMeCity() {
	$('.callMe input[type="text"]').focus(function () {
		$('.selectCity').css('display', 'block');
	});
	
	$('html').click( function(event){
		if ( $(event.target).closest('.callMe').length ) {
			return;
		}
		$('.selectCity').css('display', 'none');
		event.stopPropagation();
	});
}

$(document).ready(function() {
    function autocompleteCallback(request, responseCallback)
    {

        var url = "/glossary/ajax/";
        $.getJSON(url, request, function(data)
        {
            responseCallback(data);
            var projects = [];

            $.each(data, function(ind, arr){
                projects.push({
                    value: arr.id,
                    label: arr.name
                });
            });
            responseCallback(projects);
        });

        //window.console.debug(request, responseCallback);
    }

    function selectCallback(request, selected)
    {
        $("input#search_glossary").val(selected.item.label);
        $("input#search_glossary_hidden").val(selected.item.value);
        location.replace('/glossary/word/' + selected.item.label + '/');
        setGetValue(selected.item.label);
        //window.console.debug(request,selected);
    }

    var selectedGlobal = '';
    function setGetValue(newValue)
    {
        if(newValue!='get')
        {
            selectedGlobal = newValue;
        }
        $("input#search_glossary").val(selectedGlobal);
        return selectedGlobal;

    }


    $("#search_glossary").autocomplete({
            delay:10,
            source: autocompleteCallback,
            select: selectCallback
            //close: function(event, ui) {setGetValue('get');}
        }
    );












    function autocompleteCallbackFefco(request, responseCallback)
    {

        var url = "/fefco/ajax/";
        $.getJSON(url, request, function(data)
        {
            responseCallback(data);
            var projects = [];

            $.each(data, function(ind, arr){
                projects.push({
                    value:  arr.name,
                    label:  arr.name,
                    dir:    arr.dir
                });
            });
            responseCallback(projects);
        });
    }
    function replaceGo( url ) {
            location.replace(url);
    }
    $("#search_fefco").autocomplete({
            delay:10,
            source: autocompleteCallbackFefco,
            select: function( event, ui ) {
				replaceGo( ui.item.dir );
			}
        }
    );




});

function search_glossary()
{
        location.replace('/glossary/search/' + $('#search_glossary').val() + '/');
}

function search_fefco()
{
        location.replace('/fefco/search/' + $('#search_fefco').val() + '/');
}

function getPhone()
{
    if($('#phone').val().length>0 && $('#phone').val()!=$('#phone').attr('placeholder'))
    {
        var office = $("input:radio[name=phone_office]:checked").val();
        var url = "/ajax/";
        var request = 'phone=' + $('#phone').val() + '&office=' + office;
        $.getJSON(url, request, function(data)
        {
            if(data.responsephone == 'ok')
            {
                $('#phone').val('');
				$('.selectCity').css('display', 'none');
                alert('Спасибо, ваш звонок принят. В ближайшее время с Вами свяжется наш менеджер.')
            }
        });
    } else
    {
        alert('Необходимо ввести Ваш номер телефона.');
        $('#phone').focus();
    }
}

function add_question_name()
{
    if(!$('#add_question_name').val())
    {
        alert('Необходимо ввести, ваше имя.');
        $('#add_question_name').focus();

    } else if(!$('#add_question_question').val())
    {
        alert('Вы не задали вопрос.');
        $('#add_question_question').focus();

    } else
    {
        var url = "/ajax/";
        var request = 'name=' + $('#add_question_name').val() + '&question=' + $('#add_question_question').val();
        $.getJSON(url, request, function(data)
        {
            if(data.responsephone == 'ok')
            {
                $('#add_question_name').val('');
                $('#add_question_question').val('');
                alert('Спасибо, ваш звонок принят. В ближайшее время с Вами свяжется наш менеджер.')
            }
        });
    }
}


function quickgo()
{
    location.replace($("#quick_go").val());
}


function orderForm($one)
{

    $(document).ready(function () {

        if($one)
        {
            var selectOneMenu = $one;

            $("#rightScroll li").each(function () {
                $(this).css({'display' : 'none'});
            });
            $('.' + selectOneMenu).css({'display' : 'block'});
        }

        $("#leftScroll li").click( function () {

            $("#leftScroll li").each(function () {
                $(this).removeAttr('class');
            });
            $(this).attr({'class' : 'select'});

            $("#rightScroll li").each(function () {
                $(this).css({'background-position' : 'right 3px', 'font-weight' : 'normal', 'padding' : '0 14px 0 0'});
            });

            var selectOneMenu = $(this).attr('id');
            var cuttext="content_";
            $("input#content_one").val(selectOneMenu.substring(cuttext.length));
            $("input#content_tvo").val("");

            $("#rightScroll li").each(function () {
                $(this).css({'display' : 'none'});
            });
            $('.' + selectOneMenu).css({'display' : 'block'});
        });


        $("#rightScroll li").click( function () {

            //тут ajax
            var id = $(this).attr('id');
            $("input#content_tvo").val(id);

            $.ajax({
                type: 'GET',
                url: '/ajax/orderajax/',
                data: {'id':id},
                success: function (data) {

                        $(".productInfo").empty().append(data);
                }
            });


            $("#rightScroll li").each(function () {
                $(this).css({'background-position' : 'right 3px', 'font-weight' : 'normal', 'padding' : '0 14px 0 0'});
            });
            $(this).css({'background-position' : 'right 3px', 'font-weight' : 'bold', 'padding' : '0 14px 0 0'});
        });

        var c = 0;
        $('#close_1').change(function () {
            c = c+1;
            if(c%2==0)
            {
                $('.multiSelect').css({'display' : 'block'});
                $('.productInfo').css({'display' : 'block'});
            } else
            {
                $('.multiSelect').css({'display' : 'none'});
                $('.productInfo').css({'display' : 'none'});
            }
        });

        var p = 0;
        $('#close_2').change(function () {
            p = p+1;
            if(p%2==0)
            {
                $('.block2').css({'display' : 'block'});
            } else
            {
                $('.block2').css({'display' : 'none'});
            }
        });

    });
}

function submit_order()
{
    $(document).ready(function () {
        var fio = $("#fio").val();
        var email = $("#email").val();
        var company = $("#company").val();
        var phone_number = $("#phone_number").val();

        if(!fio)
        {
            alert('Не заполнено поле "ФИО"');
            $("#fio").focus();

            return fio;
        }
        if(!email)
        {
            alert('Не заполнено поле "email"');
            $("#email").focus();

            return fio;
        }
        if(!phone_number)
        {
            alert('Не заполнено поле "телефон"');
            $("#phone_number").focus();

            return fio;
        }

        var content_one = $("input#content_one").val();
        var content_tvo = $("input#content_tvo").val();

        var office = $("input:radio[name=office]:checked").val();


        var close_1 = Number($("#close_1").is(':checked'));
        var close_2 = Number($("#close_2").is(':checked'));

        var width = $("#width").val();
        var height = $("#height").val();
        var long = $("#long").val();
        var material_id = $("#material_id").val();

        var mm = $("#mm").val();
        var tirag = $("#tirag").val();

        var url = "/ajax/";
        var request = 'order=1&fio=' + fio + '&company=' + company + '&email=' + email + '&phone_number=' + phone_number + '&content_one=' + content_one + '&content_tvo=' + content_tvo + '&office=' + office + '&width=' + width + '&height=' + height + '&long=' + long + '&mm=' + mm + '&tirag=' + tirag + '&material_id=' + material_id + '&close_1=' + close_1 + '&close_2=' + close_2;
        $.getJSON(url, request, function(data)
        {
            if(data.responsephone == 'ok')
            {
                alert('Спасибо, Ваш заказ принят. В ближайшее время с Вами свяжется наш менеджер.');
                location.replace('/order/');

            }
        });

    });
}
orderForm('onemenu_526');


function oneSelect(oneMneu, teoMenu)
{
    $("#onemenu_" + oneMneu).attr({'class' : 'select'});
     $.ajax({
         type: 'GET',
         url: '/ajax/orderajax/',
         data: {'id':teoMenu},
         success: function (data) {

                 $(".productInfo").empty().append(data);
         }
     });


     $("#rightScroll li").each(function () {
         $('#' + teoMenu).css({'background-position' : 'right 3px', 'font-weight' : 'normal', 'padding' : '0 14px 0 0'});
     });
     $('#' + teoMenu).css({'background-position' : 'right 3px', 'font-weight' : 'bold', 'padding' : '0 14px 0 0'});

}

 $(document).ready(function () {
    oneSelect(526,527);
     $("input#content_one").val(526);
     $("input#content_tvo").val(527);
 });



