var gflag = 0;
var to = null;
var to2 = null;
var to3 = null;
var prev = 0;
var curID = 0;
var txtflag = false;

function fadeOutMenu(m) {
    $("#" + m).fadeOut('100');
    $("#" + m).prev("a.act").removeClass('act');
    if (m == 'cityTab') $("#chooseCity a").removeClass('act');
    clearTimeout(to);
}


function showDD(m) {
    if (prev != 0) tmp = prev.substring(0, 4);
    $("#" + tmp).removeClass('hover');
    if ($("#" + m).length == 0) { return true; }
    hideDD(prev);
    prev = m;
    $("#" + curID).addClass('hover');
    $("#" + m).fadeIn(150);
    gflag = 1;
}

function hideDD(m) {
    if ($("#" + m).length == 0) { return true; }
    if (gflag == 0) {
        $("#" + m).fadeOut(150);
        $("#" + curID).removeClass('hover');
        clearTimeout(to2);
        gflag = 0;
    }
}

function hideDDTimed(m) {
    gflag = 0;
    to2 = setTimeout(function () {
        hideDD(m);
    }, 1000);
}

function hideDDTimed2(m) {
    gflag = 0;
    to2 = setTimeout(function () {
        hideDD(m);
    }, 600);
}

$(document).ready(function () {

    /*  Change Your Location  */
    $('#chooseCity a').click(function (e) {
        e.preventDefault();
        clearTimeout(to);
        if ($(this).hasClass('act')) {
            $(this).removeClass('act');
            $("#cities").fadeOut('100');
        } else {
            $(this).addClass('act');
            $("#cities").fadeIn('100');
        }

    });

    $("#cities, #chooseCity a.act").click(function () {
        clearTimeout(to);
    });

    $("#cities, #chooseCity a.act").hover(
			function () { clearTimeout(to); },
			function () {
			    if (txtflag) return;
			    to = setTimeout(function () { fadeOutMenu('cities') }, 1000);
			}
		);

    $("#chooseCity a").hover(
			function () { },
			function () {
			    if (txtflag) return;
			    if ($(this).hasClass('act')) to = setTimeout(function () { fadeOutMenu('cities') }, 1000);
			}
		);


    /*  Change Your Location  */
    $('#tsForm .inpt').click(function () {
        $(this).children('input').focus();
    });

    $('#tsForm #inpt').click(function () {
        if ($(this).attr("value") == "e.g. Italian Restaurant in Dubai Marina") $(this).attr("value", "").removeClass('default');
    });

    $('#tsForm #inpt').focus(function () {
        if ($(this).attr("value") == "e.g. Italian Restaurant in Dubai Marina") $(this).attr("value", "").removeClass('default');
    });

    $(function () {
        $('#tsForm #inpt').focus(function () {
            if (this.setSelectionRange) /* DOM */{
                setTimeout(function (t) { /* hack for select delay */
                    t.setSelectionRange(t.value.length, t.value.length);
                }, 0, this);
            } else if (this.createTextRange) /* IE */{
                r = this.createTextRange();
                r.collapse(false);
                r.select();
            }
        });
    });

    $('#tsForm #inpt').blur(function () {
        if ($(this).attr("value") == "") $(this).attr("value", "e.g. Italian Restaurant in Dubai Marina").addClass('default');
    });



    /* --- Admin Stuff --- */

    $('#toolbar a[id^="tb_"]').click(function (e) {
        e.preventDefault();

        curID = $(this).attr("id");
        curPos = $(this).position();
        tmp = curID + '_dd';

        console.log(curID);

        if ((curID != 'tb_user') && (curID != 'tb_notice_dd')) {
            $("#" + tmp).css("left", curPos.left + "px");
        }

        if (curID == 'tb_user') {
            $("#" + tmp).css("right", "5px");
        }

        if (curID == 'tb_notice') {
            tmp2 = parseInt(curPos.left + $(this).outerWidth() / 2 - $("#" + tmp).outerWidth() / 2);
            $("#" + tmp).css("left", tmp2 + "px");
        }

        if (!$(this).hasClass('hover')) {
            $('#toolbar .hover').removeClass('hover');
            showDD(tmp);
        } else {
            //hideDDTimed(tmp);
            $('#toolbar .hover').removeClass('hover');
            $('#toolbar .toolbar_dd:visible').fadeOut(150);
            clearTimeout(to2);
            gflag = 0;
        }
    });

    $(".toolbar_dd, .notice_dd").hover(
			function () { clearTimeout(to2); },
			function () { hideDDTimed(curID + '_dd'); }
		);

    $('#toolbar a[id^="tb_"]').hover(
			function () { if ($(this).hasClass('hover')) clearTimeout(to2); },
			function () { if ($(this).hasClass('hover')) hideDDTimed(curID + '_dd'); }
		);

});
