
var ua = navigator.userAgent;
var isiPad = /iPad/i.test(ua) || /iPhone OS 3_1_2/i.test(ua) || /iPhone OS 3_2_2/i.test(ua);

$(document).ready(function () {
    var canClose = false;
    var closed = true;
    var clickEvent = (isiPad == true) ? 'touchend' : 'click';
    $(document).bind(clickEvent, function (event) {

        if (closed == false &&
			 canClose == true &&
			 $(event.target).closest('.curtain').length == 0 &&
			 $(event.target).hasClass('curtain') == false
		) {

            $('.curtain').hide();
            $('.curtainActivator').removeClass('activeLi');
            closed = true;
            canClose = false;
        }
        else {
            if (isiPad && closed == true && $(event.target).hasClass('curtainActivator')) {
                closed = false;

                $('.curtain').slideDown('slow', function () {
                    canClose = true;
                 });
                $('.curtain').offset($(this).offset().left - 40, $(this).offset().top);
            }
        }
    });

    $('.curtainActivator').hover(function () {
        if (closed && !isiPad) {
            closed = false;
            canClose = false;
            $(this).addClass('activeLi');
            $('.curtain').slideDown('slow', function () { canClose = true; });
            $('.curtain').offset($(this).offset().left - 40, $(this).offset().top);
        }
    })

    $('.curtainActivator').mouseleave(function () {
        if (canClose) {
            $('.curtain').hide();
            $('.curtainActivator').removeClass('activeLi');
            closed = true;
        }
    });
    $('.curtain').mouseleave(function () {
        if (canClose) {
            $('.curtain').hide();
            $('.curtainActivator').removeClass('activeLi');
            closed = true;
        }
    })
    .mouseenter(function () {
        $('.curtainActivator').addClass('activeLi');
        $(this).show();
    });

    $('.curtain a').each(function () {
        var url = $(this).attr('href');
        $(this).click(function (event) {
            if (!$(event.target).hasClass('first') && !$(event.target).parent().hasClass('first')) {
                $('.curtain').slideUp('slow', function () {
             
                    location.href = url;
                });
            }
            return false;
        });
    });

});
