﻿// textbox focus/blur event handling 
function InitializeTextBox(id) {
    //alert($('#' + id + ' input:text').length);
    $('#' + id + ' input:text').focus(function() {
        $(this).attr('class', 'ShopNoonTextBoxFocused');
        $(this).siblings('.spanValidation').text('');
        $('#helpword_' + $(this).attr('id')).show();
        if ($(this).attr('id').indexOf('tbPaypalID') > 0) {
            $('#spanPaypalLink').hide();
        }
    });

    $('#' + id + ' input:text').blur(function() {
        $(this).attr('class', 'ShopNoonTextBox');
        $('#helpword_' + $(this).attr('id')).hide();
        if ($(this).attr('id').indexOf('tbPaypalID') > 0) {
            $('#spanPaypalLink').show();
        }
    });

    $('#' + id + ' input:text').keypress(function() {
        $(this).attr('class', 'ShopNoonTextBox');
        $('#helpword_' + $(this).attr('id')).hide();
        //alert($(this).attr('id'));
        
    });

    $('#' + id + ' input:password').focus(function() {
        $(this).attr('class', 'ShopNoonTextBoxFocused');
        $(this).siblings('.spanValidation').text('');
        $('#helpword_' + $(this).attr('id')).show();
    });

    $('#' + id + ' input:password').blur(function() {
        $(this).attr('class', 'ShopNoonTextBox');
        $('#helpword_' + $(this).attr('id')).hide();
    });

    $('#' + id + ' input:password').keypress(function() {
        $(this).attr('class', 'ShopNoonTextBox');
        $('#helpword_' + $(this).attr('id')).hide();
    });


}

/* product event tag */
function bindEventTag() {
    $('.divEventTag').live('mouseover', function() {
        divParent = $(this).parents('.divProductImage');
        var h = $(divParent).height();
        pic = $(this).parent().find('.imgProductPicture')
        position = $(pic).position();
        d = h - $(pic).height() + position.top;
        newpos = (d - $(this).height()) + 'px';

        $(this).stop().animate({ top: newpos }, 500);
    });
    $('.divEventTag').live('mouseout', function() {
        divParent = $(this).parents('.divProductImage');
        var h = $(divParent).height();
        pic = $(this).parent().find('.imgProductPicture')
        position = $(pic).position();
        d = h - $(pic).height() + position.top;
        toppos = (d - eventTagHeadHeight) + 'px';
        $(this).stop().animate({ top: toppos }, 500);
    });
}

function initializeEventTag() {
    /* event tag */


    $('.divEventTag').each(function() {

        divParent = $(this).closest('.divProductImage'); //alert($(this).closest('.divProductImage').length);
        var h = $(divParent).height();
        var w = $(divParent).width();


        var divid = $(this).attr('id');
        iden = divid.split('_');
        pic = $(this).parent().find('.imgProductPicture')
        position = $(pic).position();
        d = h - $(pic).height() + position.top;
        toppos = d - eventTagHeadHeight;
        //alert('h:' + h + ' pic height:' + $(pic).height() + ' position top: ' + position.top + ' eventTagHeadHeight:' + eventTagHeadHeight);
        //alert(position.left + ' ' + position.top);
        if (iden.length == 3) {
            eventid = parseInt(iden[1]);
            //alert(eventid);
            if (eventid <= 0)
                $(this).hide();
            else {
                $(this).show();
                //alert(toppos);
                $(this).css({ top: toppos, opacity: 0.8 });

            }
        }
        else {
            $(this).hide();
        }


    });


}

function bind_event_loginpopup() {
    $('#aSignIn').bind('mouseout', function() {
        setTimeout('hideLoginPopup()', 50);

    });

    $('#divLoginPopup').bind('mouseout', function() {
        setTimeout('hideLoginPopup()', 50);
    });

    $('#divLoginPopup input').each(function() {
        $(this).css('border', '1px solid #ffffff');
    });

    $('#divLoginPopup input').blur(function() {
        $(this).css('border', '1px solid #ffffff');
    });

    $('#divLoginPopup input').focus(function() {
        $(this).css('border', '1px solid #7f9db9');

    });

    $('#divLoginPopup input:first').blur(function() {

        v = $(this).val();
        //alert(email);
        if (v.length <= 0) {
            //alert('empty');
            $('.spanError').text('Enter email');
        }
        else {
            b = _validateEmailAddress(v);
            if (!b)
                $('.spanError').text('Invalid email');
        }
    });
}

function bind_event_linkmyaccount() {
    $('.LinkMyAccount').bind('mouseover', function() {
        $('#divMyAccountMessageBox').show();
        pos = $('.LinkMyAccount:first').position();
        $('#divMyAccountMessageBox').css('left', pos.left - 15);
        $('#divMyAccountMessageBox').css('top', pos.top + 12);

        showMyAccount($(this));
        if ($('#aSignIn').length <= 0) {
            showMessageBox($(this));
            if ($(this).attr('id').indexOf('imgMessage1') >= 0)
                $('#divmyaccountmessage').show();
        }

        //hflogin = $get('<%=hfLoginPopup.ClientID %>');
        //hflogin.value = '1';

        height1 = $('#divMyAccount').height();
        height2 = $('#divMessageBox').height();

        //alert(height1 + ' ' + height2);

        if (height1 > height2) {
            $('#divMessageBox').css('z-index', 1000);
            $('#divMyAccount').css('z-index', 999);
            $('#divMessageBox').css('border-left', '0px');
        }
        else {
            $('#divMessageBox').css('z-index', 1000);
            $('#divMyAccount').css('z-index', 999);
            $('#divMyAccount').css('border-right', '0px');
        }

    });

    $('.LinkMyAccount').bind('mouseout', function() {
        setTimeout('hideMyAccount()', 50);

    });

    $('#divMyAccountMessageBox').bind('mouseout', function() {
        setTimeout('hideMyAccount()', 50);
    });

    if ($('#aSignIn').length <= 0) {
        // user logined
        $('.front_myaccountmenu').each(function(i) {
            $(this).bind('mouseover', function() {

                $('#divMessageBox div').hide();
                var idx = i + 1;
                //var sel = '#divMessageBox #div' + idx;
                //alert($(sel).length);
                //$(sel).show();
                ids = $(this).attr('id').split('_');
                $('#div' + ids[1]).show();
            });
        });
    }
}

function bind_event_statusbar() {
    if ($('#aSignIn').length <= 0) {


        $('#divStatusBar').show();
        AdjustStatusBar();
        $('#tdFooter').css('padding-bottom', 30);

        // statusbar favorite
        $('.aStatusBarFavorite').live('click', function() {
            ids = $(this).attr('id').split('_');
            toggleFavorite(ids[1]);
        });

        $('.imgFavorite').live('mouseover', function() {
            $(this).css('position', 'relative');
            $(this).stop().animate({ top: -5 }, 200);
        });

        $('.imgFavorite').live('mouseout', function() {
            $(this).stop().animate({ top: 0 }, 200);
            $(this).css('position', 'static');
        });

        $('.divChatRoomActive .txtMessage').live('keypress', function(event) {
            if (event.keyCode == '13') {

                if (typeof (WebService) != "undefined") {
                    id = $(this).attr('id');
                    //alert(id.replace('txtMessage_', ''));
                    WebService.WriteChatMessage(id.replace('txtMessage_', ''), $(this).val(), OnChatSucceeded);
                    $(this).val('');
                }
            }
        });

        $('.imgHideChat').live('click', function() {
            $(this).parents('.divChatRoomActive').hide('slide', { direction: 'down' }, 150); ;
            $(this).parents('.divChatRoomInactive').hide('slide', { direction: 'down' }, 150); ;

            adjustChatRoomContainer();
        });

        $('.imgLeaveChat').live('click', function() {

            var bActiveRoom = true;
            var classname = "divChatRoomActive";

            if ($(this).parents('.divChatRoomActive').length <= 0) {
                bActiveRoom = false;
                classname = "divChatRoomInactive";
            }

            var div = $(this).parents('.' + classname);
            ids = $(div).attr('id').split('_');
            guid = ids[1];

            //alert(guid);
            // below should be actually "purge"
            $('#iconChatRoom_' + guid).hide();
            $(div).hide();


            adjustChatRoomContainer();

            if (typeof (WebService) != "undefined") {
                WebService.LeaveChat(guid);
            }
        });

        $('.iconChatRoomActive').live('click', function() {
            ids = $(this).attr('id').split('_');
            $('#divChatRoom_' + ids[1]).show('slide', { direction: 'up' }, 200); ;
            adjustChatRoomContainer();
        });

        $('.iconChatRoomInActive').live('click', function() {
            ids = $(this).attr('id').split('_');
            $('#divChatRoom_' + ids[1]).show('slide', { direction: 'up' }, 200);
            adjustChatRoomContainer();
        });

        $('#divActivityFeed .txtComment').live('keypress', function(event) {
            if (event.keyCode == '13') {

                if (typeof (WebService) != "undefined") {
                    trackingid = parseInt($('#divActivityFeed #spanTrackingID').text());
                    WebService.WriteActivityComment(trackingid, $(this).val());
                    $(this).val('');
                }

                return false;
            }

        });

        //start_activity_feed();
        
    }
}

function bind_event_topmenu() {
    $('#tableTopMenu img').bind('mouseover', function() {
        src = $(this).attr('src');
        $(this).attr('src', src.replace('.gif', '_over.gif'));
    });

    $('#tableTopMenu img').bind('mouseout', function() {
        src = $(this).attr('src');
        $(this).attr('src', src.replace('_over', ''));
    });

    $('.imgHasSubMenu').bind('mouseover', function() {
        /*
        src = $(this).attr('src');
        $(this).attr('src', src.replace('.gif', '_over.gif'));
        */
        imgid = $(this).attr('id');
        submenu = $('#divTopSub' + imgid);
        $(submenu).show();
        pos = $(this).position();
        h = $(this).height();
        $(submenu).css('top', pos.top + h);
        $(submenu).css('left', pos.left);

    });

    $('.imgHasSubMenu').bind('mouseout', function() {
        /*
        src = $(this).attr('src');
        $(this).attr('src', src.replace('_over', ''));
        */
        setTimeout("hideSubMenu('" + $(this).attr('id') + "')", 50);
    });

}

function bind_event_banner() {
    // banner type Sapphire rolling around effect

    $('#testbutton').click(function() {
        $('#divBannerSapphire_0').slideToggle(2000);
    });

    $('#testbutton2').click(function() {
        $('#divBannerSapphire_0').hide('slide', { direction: 'up' }, 1000);
    });


    sapphireCount = $('.divBannerSapphire').length;
    if (sapphireCount > 0) {
        $('#divBannerSapphire_0').slideToggle(10);
        if (sapphireCount > 1) {
            setTimeout("run_sapphire(0)", sapphireChangeInterval);
        }

    }

}

// category menu
function bind_event_catmenu(hfcatvalue, urlroot) {
    $('#imgCatMenu').bind('click', function() {
        //HideAllCategory();

        if ($('.divCat:first').is(':visible')) {
            //alert('will hide category menu');
            // hide it
            HideCategoryMenu();
        }
        else {
            //alert('cat menu will show');
            //$('.divCat:first').show();
            ShowCategoryMenu();


        }


    });

    //


    //alert(hfcatvalue);
    if (hfcatvalue == '0') {
        $('.divCat:first').hide();
        $('#imgCategory').attr('src', urlroot + 'Btn_main_viewall.gif');
        $('#imgCatMenu').attr('src', urlroot + 'Btn_main_viewall_arrow.gif');
    }
    else {
        //alert('divcat will be shown');
        $('#imgCategory').attr('src', urlroot + 'Btn_main_viewall_over.gif');
        $('#imgCatMenu').attr('src', urlroot + 'Btn_main_viewall_arrow_over.gif');
        /* not used
        $('.divCat:first').show();
        if ($('.divCat').length <= 1) {
            position = $('#imgCategory').position();
            height = $('#imgCategory').height();
            $('.divCat:first').css('top', position.top + height);
            $('.divCat:first').css('left', position.left);
        }
        */
        $('#divCatMain').appendTo('#tdcatmenucontainer');
        $('#divCatMain').show();
        
    }

    $('.divCat:first').bind('mouseout', function() {
        HideSubCat();
    });

    $('#divCatMain').bind('mouseout', function() {
        HideSubCat();
    });
}

// bind mouse-over event to cart link and image
/* do not show order items when mouse over
function bind_event_carticon() {
    $('#img_cart_indicator').bind('mouseover', function() {
        if (typeof (WebService) != "undefined") {
            WebService.GetCartItem(OnGetCartItemSuccess);
        }
    });
}
*/

// mouse-over event to imgProductThumbShowInfo
function bind_event_productThumbInfo() {
    $('.imgProductThumbShowInfo').bind('mouseover', function() {
        id = $(this).attr('id');
        ids = id.split('_');
        //showProductThumbInfo(ids[1]);
        setTimeout("showProductThumbInfo('" + ids[1] + "','" + ids[2] +"')", 500);
    });

    $('.imgProductThumbShowInfo').bind('mouseout', function() {
        $('#divProductInfoPopup').hide();
    });
}
