/**
 * User: sergasd
 * Date: 25.05.11
 * Time: 15:24
 */

//tmp vars
var menu_state = false;


//fix ie7 x-index bug
$(function() {
    var zIndexNumber = 1000;
    $('div').each(function() {
        //$(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });

    //$("#specs").removeAttr('style');



    var formZIndex = 1000;
    $('.search_form :visible').each(function(){
        $(this).css('zIndex', formZIndex);
        formZIndex -= 10;
    });




});




/********
 * MENU *
 ********/
(function($){
$(document).ready(function(){
    /*MENU*/
    $("#top_menu_content>ul>li>a").hover(
            function(){
                var ul = $(this).parent().find('ul').eq(0);
                if(ul.length == 0) return;

                var offset = $(this).offset();
                ul.show();
                ul.offset({left:offset.left,top:offset.top+37});

                $(this).css('background','#ff9933');
            },
            function(){
                var ul = $(this).parent().find('ul').eq(0);
                if(ul.length == 0) return;

                var t = $(this);

                setTimeout(function(){
                    if(menu_state === false)
                    {
                        //console.log(t);
                        t.css('background','url(../../images/menu/menu_bg.png) repeat-x');
                        ul.hide();
                    }
                }, 100);
            }
    );

    $("#top_menu_content>ul>li>ul").mouseleave(function(){
        menu_state = false;
        $(this).prev().css('background','url(../../images/menu/menu_bg.png) repeat-x');
        $(this).hide();
    });

    $("#top_menu_content>ul>li>ul").mouseenter(function(){
        menu_state = true;
    });

});
})(jQuery);











(function($){
    //avatar change dialog
    $(document).ready(function(){

        /**
         * MAIN POPUP DIALOG WINDOW
         */

        /**
         * close dialog events
         * */
        $(".gray_button_left, .gray_button_right").bind('click', function(e){
            $("#splash_screen, .popup_window").hide(10);
            $("object,iframe").show();
            e.preventDefault();
        });

        /**
         * @function showAvatarDialog
         * Показывает диалог выбора файла аватара
         * @return boolean
         * */
        window.showAvatarDialog = function() {
            //hide flash
            $("object,iframe").hide();

            var ie7 = ($.browser.msie && $.browser.version == 7.0) ? true : false;

            var w = ($("#black_screen").width()) / 2 + 250;
            //var h = $(document).height();

            var scrollTop = $(document).scrollTop();
            var top = $("body").height() / 2 - $(".popup_window").height() / 2 - 100;

            $(".popup_window").css('left', w + 'px').css('top', scrollTop + top + 'px').show();

            if (!ie7)
                $("#splash_screen").fadeTo(10, 0.4).css('z-index', '1000').height($(document).height()).show();

            $(".popup_window").css('zIndex', '1100').show();
            return true;
        };



/**
 * ++++++++++++++++++++++++++++++++++++++++++++++
 * MOD USER
 * ++++++++++++++++++++++++++++++++++++++++++++++
 */
        /**
         * change avatar event
         * */
        $("#change_avatar").bind('click', function(e){
            showAvatarDialog();
            e.preventDefault();
        });








/**
 * ++++++++++++++++++++++++++++++++++++++++++++++
 * MOD CONTACTS
 * ++++++++++++++++++++++++++++++++++++++++++++++
 */


        //show employee
        $(".office_employee img, .employee img").click(function(e){
            e.preventDefault();
            User.contacts.showManager($(this).attr("eid"));
        });


/**
 * ++++++++++++++++++++++++++++++++++++++++++++++
 * MOD COUNTRIES
 * ++++++++++++++++++++++++++++++++++++++++++++++
 */

        //add comment dialog
        $("#add_comment").bind('click', function(e){
            User.dialog.setOptions({'width': 470, 'height' : 300}).setContent($('#comments_dialog').html()).show();
            window.commentsFormInit();
            e.preventDefault();
        });


        //hotel info window
        $(".hotel_link").bind('click', function(e){
            e.preventDefault();
            var iframe = '<iframe width="800" height="600" frameborder="0" src="' + $(this).attr('href') + '"></iframe>';

            User.dialog.setOptions({'width':800, 'height' : 600}).setContent(iframe).show();
        });


        //create special offer
        $("input[tourid]", ".search_result").click(function(){
            var tourId = $(this).attr("tourid");
            var tourInfo = window.g_search[tourId] || 0;


            $.ajax({
                'url' : '/countries/spec/createnew',
                'type' : 'POST',
                'dataType' : 'JSON',
                'data' : tourInfo,
                'success' : function(data){
                    if(data.status && data.status == 'ok')
                    {
                        var w = window.open('/countries/spec/edit?id=' + data.id, 'редактирование');
                    }

                }
            });


            //$(this).parent().parent().html('<input class="tour_id_input" type="text" value="' + tourId + '" />');
        });




    });

})(jQuery);
