﻿// JavaScript Document
//香伟健 20091130
jQuery.fn.extend({
    vkSelect: function(optionsId, valueEle, textEle){
        //init
        var $optionList = $("#" + optionsId);
        var $options = $optionList.children();
        var active = false;
        //select click event
        $(textEle).click(function(){
            if ($optionList.get(0).style.display == "none") 
                $optionList.show();
            else 
                $optionList.hide();
        });
        //autoClose
        this.mouseout(function(event){
            autoClose();
        });
        var autoClose = function(){
            setTimeout(function(){
                if ($optionList.get(0).style.display != "none") 
                    $optionList.hide();
                clearTimeout(autoClose);
            }, 4000)
        }
        //option event
        $options.each(function(i){
            var e = $(this);
            //Style
            e.hover(function(){
                e.addClass("on");
                clearTimeout(autoClose);
            }, function(){
                e.removeClass("on");
            })
            //Logic
            e.click(function(){
                textEle.innerHTML = this.innerHTML;
                valueEle.value = $(this).attr("svalue");
                $optionList.hide();
                
            });
        });
        
    },
    vksuggestion: function(listEle){
        var Ele = this.get(0);
        var content = listEle.get(0);
        var LengthUpper = 8;
        this.keyup(function(ev){
            var keyword = this.value;
            var soType = $("#soType").get(0).value;
            //alert(ev.keyCode);
            //Esc
            if (!this.value || !this.value.length || ev.keyCode == '27') {
                listEle.hide();
                return;
            }
            
            else {
                if (ev.keyCode == '38' || ev.keyCode == '40') {
                    var ObjSelected = $(".on", listEle);
                    if (content.style.display == "none") {
                        return;
                    }
                    //ev.keyCode == '38' 上翻
                    if (ev.keyCode == '38') {
                        //已有选中
                        if (ObjSelected.length > 0) {
                            //如果不是第一个数据
                            if (ObjSelected.prev().text() != "") {
                                ObjSelected.removeClass("on");
                                ObjSelected.prev().addClass("on");
                                Ele.value = $(".text", ObjSelected.prev()).get(0).innerHTML;
                                return;
                            }
                            //第一个数据
                            else {
                                ObjSelected.removeClass("on");
                                //Ele.focus();
                                return;
                            }
                        }
                        //没有选中,让最后一个选中
                        else {
                            listEle.find("tr:last").prev().addClass("on");
                            Ele.value = listEle.find("tr:last").prev().find(".text").get(0).innerHTML;
                            return;
                        }
                    }
                    //ev.keyCode == '40' 下翻
                    else {
                        //已有选中
                        if (ObjSelected.length > 0) {
                            //如果是最后一个数据
                            if (ObjSelected.next().hasClass("last")) {
                                ObjSelected.removeClass("on");
                                return;
                            }
                            //不是最后一个数据
                            else {
                                ObjSelected.removeClass("on");
                                ObjSelected.next().addClass("on");
                                Ele.value = $(".text", ObjSelected.next()).get(0).innerHTML;
                                return;
                            }
                        }
                        //没有选中,让第一个选中
                        else {
                            listEle.find("tr:first").addClass("on");
                            Ele.value = listEle.find("tr:first .text").get(0).innerHTML;
                            return;
                        }
                    }
                }
                //if(ev.keyCode != '32'&& keyword.length <= LengthUpper) {
                if (keyword.length <= LengthUpper) {
                    $.getJSON(wwwURL + "/test/Suggestions.rails?jsoncallback=?", {
                        keyword: keyword,
                        type: soType
                    }, function(json){
                        listEle.empty();
                        //listEle.innerHTML = "";
                        if (json.count != 0) {
                            $.each(json.data, function(i, data){
                                $('<tr><td class="text">' + data.text + '</td><td class="count">' + data.count + '结果</td></tr>').appendTo(listEle).hover(function(){
                                    $(this).addClass("on");
                                }, function(){
                                    $(this).removeClass("on");
                                }).click(function(){
                                    Ele.value = $(".text", $(this)).get(0).innerHTML;
                                    listEle.hide();
                                });
                            });
                            //
                            $('<tr class="last" ><td class="close" colspan="2" ><span id="closeBtn">关闭</span></td></tr>').appendTo(listEle).click(function(){
                                listEle.hide();
                            });
                            listEle.show();
                        }
                    });
                }
            }
        });
    }
})

//Update Userbar
var updateUserbar = function(Ele, userid, username){
    var tmpHTML = '';
    if (userid != 0) {
        tmpHTML = '<a href="' + wwwURL + '/userpanel">' + username + '</a>，您好！<a class="link" href="' + wwwURL + '/userpanel">用户中心</a><a class="link" href="' + wwwURL + '/upload">上传视频</a><a class="link" href="' + wwwURL + '/help">查看帮助</a><a class="link" href="http://album' + cookieDomain + '">专辑</a><a class="link" href="http://group' + cookieDomain + '">群组</a><a class="link last" href="' + wwwURL + '/user/logout">退出</a>'
        Ele.innerHTML = tmpHTML;
    }
    
}
// runcode
$(document).ready(function(){
    //bind SearchType select
    $("#sel").vkSelect("option", $("#soType").get(0), $("#searchType").get(0));
    //bind suggestion
    $("#soKeyword").vksuggestion($("#soResult"));
    $("#searchForm").submit(function(){
        if ($("#soKeyword").get(0).value.length < 1) 
            return false;
        else 
            return true;
    });
    //根据User信息更新Userbar
    //var oUser = new user();
    updateUserbar($("#userbar").get(0), oUser.id, oUser.name);
    //添加到专辑按钮
    addToAlbumInit();
    //fix IE6 maxHeight maxWidth
    if ($.isIE6) {
        $('.poster_pre').each(function(){
            var maxHeight = parseInt($(this).css('max-height'));
            var maxWidth = parseInt($(this).css('max-width'));
            var iHeight = $(this).height();
            var iWidth = $(this).width();
            if (iWidth / iHeight >= maxWidth / maxHeight) {
                if (iWidth > maxWidth) {
                    $(this).width(maxWidth);
                    $(this).height((maxWidth * iHeight) / iWidth);
                }
            }
            else {
                if (iHeight > maxHeight) {
                    $(this).height(maxHeight);
                    $(this).width((iWidth * maxHeight) / iHeight);
                }
            }
        });
    }
});
try {
    document.execCommand("BackgroundImageCache", false, true);
} 
catch (e) {
}




