(function ( $ ) {

var setSearchOptions = function (  ) {
var args = {}, source = window.location.search.substr(1).split('&');
    for( k in source )
        if( typeof(source[k]) == 'string' ) {
        var s = source[k].split('=');
            args[s[0]] = decodeURIComponent(s[1].replace(/\+/g, ' '));
        }
var item = '#header_all';
    if( args.S )
        item = 'a[scls=' + args.S + ']';
    item = $(item);
    item.click();
var input = $('#' + item.attr('input'));
    if( args.kw )
        input.find('input[name=kw]').val(args.kw).change();

    if( args.adkw )
        input.find('input[name=adkw]').val(args.adkw).change();
}

var now = new Date().getHours(), greetings = '晚';
    if( now >= 3 && now < 11 )
        greetings = '早';
    else if( now >= 11 && now < 17 )
        greetings = '午';
    $('.header_greeting').text(greetings);
    $('#hdsrh_type li a').click(function ( e ) {
            e.preventDefault();
        var me = $(this);
            $('#hdsrh_type li a.select').removeClass('select');
            me.addClass('select');
            $('.hdsrh_area').hide();
        var searchArea = $('#' + me.attr('input')).show(),
            searchInput = searchArea.find('input:first'),
            s_class_id = me.attr('scls');
            searchInput
                .flushCache()
                .setOptions({ extraParams: { S: s_class_id }})
                .focus();
            searchArea.find('span.button_target').text(me.text());
            searchArea.find('input.srh_class_id').val(s_class_id);
        var note = me.attr('note'), itemName = me.attr('item');
            if( note ) {
                $('.hdsrh_note').show().find('span').hide();
                $('#' + note).show();
            } else
                $('.hdsrh_note').hide();
            if( itemName )
                searchInput.changeEditBoxPrompt('- 關鍵字 或 ' + itemName + '名 -');
        });
    $('#fdoorway a.doorway').each(function (  ) {
            $(this).attr('href', '/home/' + $.cookie('uhome') + $(this).attr('href'));
        });
    $('#doorway_close').click(function ( e ) {
            e.preventDefault();
            $('#fdoorway').slideUp('fast');
        });
    $('#header_gateway').click(function ( e ) {
            e.preventDefault();
        var o = $(this).offset();
            $('#fdoorway').css({
                    'left': o.left + 'px',
                    'top':  (o.top + $(this).height() + 2) + 'px'
                }).slideToggle('fast');
        });
    $('.hdsrh_send').click(function ( e ) {
            e.preventDefault();
            $(this).parent().submit();
        });
    $('#header_favorite').click(function ( e ) {
            e.preventDefault();
        var title = 'iPeen愛評網', url = 'http://www.ipeen.com.tw';
            if( document.all )
                window.external.AddFavorite(url, title);
            else if( window.sidebar )
                window.sidebar.addPanel(title, url, '');
        });
    $('#header_homepage').click(function ( e ) {
            e.preventDefault();
            this.style.behavior='url(#default#homepage)';
            this.setHomePage('http://www.ipeen.com.tw/');
        });
    $('#search_addr input:first, #search_noaddr input:first')
        .promptedEditBox('- 關鍵字 或 店名 -')
        .autocomplete('/suggest/keywords.php', { selectFirst: false });
    $('.srh_add')
        .promptedEditBox(' - 輸入地點，例如：台北市羅斯福路 -');
    $('#search_map input:first')
        .promptedEditBox(' - 輸入地圖定位點，例：台北市羅斯福路 -');
    if( $.cookie('unick') ) {
        $('#header_nick').text($.cookie('unick'));
        $('#header_msg').attr('href', '/home/' + $.cookie('uhome') + '/mail');
        $('#header_msgnum').text($.cookie('umsgno'));
        $('#header_area_nologin').remove();
        $('#header_area_login').show();
    } else {
        $('#header_area_nologin').show();
        $('#header_area_login').remove();
        if( window.location.pathname == '/' )
            $('#header_login').attr('href', '/signin/signin.php');
        else
            $('#header_login').click(function ( e ) {
                    e.preventDefault();
                    dialogLogin();
                });
    }
    if( isIE6() ) {
        fixPNGforIE('.hdsrh_send');
        fixPNGforIE('#header_logo h1');
    }
    if( window.location.pathname.indexOf('/search/') == 0 )
        setSearchOptions();
})(jQuery);
