/**
 * @author Alexey
 */

var statusMouse_select_glamur_1 = false;
var statusMouse_select_glamur_2 = false;
var statusMouse_select_glamur_3 = false;

function fun_check_1(obj) {
    $(obj).toggleClass('checked').parent('.checked_line').children('input[type="checkbox"]').click();
    $(obj).parent().toggleClass('active');
}

function fun_check_2(obj) {
    $(obj).toggleClass('checked').parent('.checked_line').children('input[type="checkbox"]').click();
    $(obj).parent().toggleClass('active');
}

$(function () {
    $(document).click(function () {
        // sel 1
        if (statusMouse_select_glamur_1) {
            $('ul.list_select_1').show();
        }
        else {
            $('ul.list_select_1').hide();
        }
        // sel 2
        if (statusMouse_select_glamur_2) {
            $('ul.list_select_2').show();
            $("#icon_calendar_1").hide();
            $("#icon_calendar_2").hide();
        }
        else {
            $('ul.list_select_2').hide();
        }
        // sel 3
        if (statusMouse_select_glamur_3) {
            $('ul.list_select_3').show();
            $("#icon_calendar_1").hide();
            $("#icon_calendar_2").hide();
        }
        else {
            $('ul.list_select_3').hide();
        }
    });
    // Select
//    $('.text-select_1').hover(function () {
//        statusMouse_select_glamur_1 = true;
//    }, function () {
//        statusMouse_select_glamur_1 = false;
//    });
    $('.list_select_1 a[class*="li_"]').bind('click', function () {
        var text = $(this).text();
        var clss = $(this).attr('class');
        $('.text-select_1').text(text).attr('id', clss);
        $('#select_1').val(text);
        $(this).parent('.list_select_1').hide();
    });
    $('.arrow_bottom_1').bind('click', function () {
        var id = $('.text-select_1').attr('id');
        if ($(".text-select_1 a." + id).parent().next().size() == 0) {
            var nextElem = $($(".text-select_1 a." + id).parent().parent().children('li')[0]).children('a');
        }
        else {
            var nextElem = $(".text-select_1 a." + id).parent().next().children('a');
        }
        var nextId = $(nextElem).attr('class');
        var text = $(nextElem).text();
        $('.text-select_1').text(text);
        $('#select_1').val(text);
        $('.text-select_1').attr('id', nextId);
    });
    $('.arrow_top_1').bind('click', function () {
        var id = $('.text-select_1').attr('id');
        if ($(".text-select_1 a." + id).parent().prev().size() == 0) {
            var size = $(".text-select_1 a." + id).parent().parent().children('li').size() - 1;
            var nextElem = $($(".text-select_1 a." + id).parent().parent().children('li')[size]).children('a');
        }
        else {
            var nextElem = $(".text-select_1 a." + id).parent().prev().children('a');
        }
        var nextId = $(nextElem).attr('class');
        var text = $(nextElem).text();
        $('.text-select_1').text(text);
        $('#select_1').val(text);
        $('.text-select_1').attr('id', nextId);
    });
    // Select 2
//    $('.text-select_2').hover(function () {
//        statusMouse_select_glamur_2 = true;
//    }, function () {
//        statusMouse_select_glamur_2 = false;
//    });
    $('.list_select_2 a[class*="li_"]').bind('click', function () {
        $("#icon_calendar_1").hide();
        $("#icon_calendar_2").hide();
        var text = $(this).text();
        var clss = $(this).attr('class');
        $('.text-select_2').text(text).attr('id', clss);
        $('#select_2').val(text);
        $(this).parent('.list_select_2').hide();
    });
    $('.arrow_bottom_2').bind('click', function () {
        $("#icon_calendar_1").hide();
        $("#icon_calendar_2").hide();
        var id = $('.text-select_2').attr('id');
        if ($(".list_select_2 a." + id).parent().next().size() == 0) {
            var nextElem = $($(".list_select_2 a." + id).parent().parent().children('li')[0]).children('a');
        }
        else {
            var nextElem = $(".list_select_2 a." + id).parent().next().children('a');
        }
        var nextId = $(nextElem).attr('class');
        var text = $(nextElem).text();
        $('.text-select_2').text(text);
        $('#select_2').val(text);
        $('.text-select_2').attr('id', nextId);
    });
    $('.arrow_top_2').bind('click', function () {
        $("#icon_calendar_1").hide();
        $("#icon_calendar_2").hide();
        var id = $('.text-select_2').attr('id');
        if ($(".list_select_2 a." + id).parent().prev().size() == 0) {
            var size = $(".list_select_2 a." + id).parent().parent().children('li').size() - 1;
            var nextElem = $($(".list_select_2 a." + id).parent().parent().children('li')[size]).children('a');
        }
        else {
            var nextElem = $(".list_select_2 a." + id).parent().prev().children('a');
        }
        var nextId = $(nextElem).attr('class');
        var text = $(nextElem).text();
        $('.text-select_2').text(text);
        $('#select_2').val(text);
        $('.text-select_2').attr('id', nextId);
    });

    // Select 3
//    $('.text-select_3').hover(function () {
//        statusMouse_select_glamur_3 = true;
//    }, function () {
//        statusMouse_select_glamur_3 = false;
//    });
    $('.list_select_3 a[class*="li_"]').bind('click', function () {
        $("#icon_calendar_1").hide();
        $("#icon_calendar_2").hide();
        var text = $(this).text();
        var clss = $(this).attr('class');
        $('.text-select_3').text(text).attr('id', clss);
        $('#select_3').val(text);
        $(this).parent('.list_select_3').hide();
    });
    $('.arrow_bottom_3').bind('click', function () {
        $("#icon_calendar_1").hide();
        $("#icon_calendar_2").hide();
        var id = $('.text-select_3').attr('id');
        if ($(".list_select_3 a." + id).parent().next().size() == 0) {
            var nextElem = $($(".list_select_3 a." + id).parent().parent().children('li')[0]).children('a');
        }
        else {
            var nextElem = $(".list_select_3 a." + id).parent().next().children('a');
        }
        var nextId = $(nextElem).attr('class');
        var text = $(nextElem).text();
        $('.text-select_3').text(text);
        $('#select_3').val(text);
        $('.text-select_3').attr('id', nextId);
    });
    $('.arrow_top_3').bind('click', function () {
        $("#icon_calendar_1").hide();
        $("#icon_calendar_2").hide();
        var id = $('.text-select_3').attr('id');
        if ($(".list_select_3 a." + id).parent().prev().size() == 0) {
            var size = $(".list_select_3 a." + id).parent().parent().children('li').size() - 1;
            var nextElem = $($(".list_select_3 a." + id).parent().parent().children('li')[size]).children('a');
        }
        else {
            var nextElem = $(".list_select_3 a." + id).parent().prev().children('a');
        }
        var nextId = $(nextElem).attr('class');
        var text = $(nextElem).text();
        $('.text-select_3').text(text);
        $('#select_3').val(text);
        $('.text-select_3').attr('id', nextId);
    });

    $('a.icon_calendar_1').bind('click', function () {
        $("#icon_calendar_2").hide();
        $("#icon_calendar_1").toggle();
    });
    $('a.icon_calendar_2').bind('click', function () {
        $("#icon_calendar_1").hide();
        $("#icon_calendar_2").toggle();
    });
});

$(document).ready(function(){
    if ($("#icon_calendar_1").datepicker) {
        $("#icon_calendar_1").datepicker({
            onSelect: function (dateText, inst) {
                var value = inst.selectedDay;
                $('#select_2').val(value);
                $('.text-select_2').text($("#icon_calendar_1").datepicker('getDate').format('dd MMMM yyyy'));
                $("#icon_calendar_1").hide();
            }
        });
    }
    if ($("#icon_calendar_2").datepicker) {
        $("#icon_calendar_2").datepicker({
            onSelect: function (dateText, inst) {
                var value = inst.selectedDay;
                $('#select_3').val(value);
                $('.text-select_3').text($("#icon_calendar_2").datepicker('getDate').format('dd MMMM yyyy'));
                $("#icon_calendar_2").hide();
            }
        });
    }
});

function detectIE6(){
	var IE = '\v' == 'v';
	if (IE) {
		var temp = navigator.appVersion.match(/\((.+)\)/i);
		var params = temp[1].split(/;\s*/);
		var params1 = params[1].split(/\s/);
		if (params1[1] <= 6) {
			return true;
		}
		else {
			return false;
		}
	}
}
