/*
    common.js
	used for the basic function
	created by Evance. 2010 .02 .09
*/
jQuery().ready(function() {
    //init function
	var catePanel = jQuery("<div id=\"catePanel\"><span class=\"arrow\">&nbsp;<\/span><\/div>");
    catePanel.appendTo("body");
    //ww-sopport
    jQuery("#ww-support").bind({
        "mouseleave":function() {
            jQuery(this).find(".bd").animate({
                "height":"hide"
            },200);
        }
    })
    jQuery("#ww-support .hd").bind({
        "click":function() {
            jQuery(this).siblings().css({
                "opacity":0.9,
                "display":"block"
            })
        }
    })
    jQuery("#ww-support #jq-close-ww").bind({
        "click":function() {
            jQuery("#ww-support .bd").hide();
        }
    })
    //select category for search
    jQuery("#site-search #jq-select-hd").bind("click",function() {
        if(jQuery("#jq-select-opt").is(":hidden")) {
            jQuery("#jq-select-opt").show();
        }
        else {
            jQuery("#jq-select-opt").hide();
        }
    });
    jQuery("#jq-select-opt ul li").bind({
        "click":function() {
            var cate = {
                "id": jQuery(this).attr("id").split("-")[1],
                "text": jQuery(this).text()
            };
            if (jQuery(this).text().indexOf("└ ") >= 0) {
            	cate.text = cate.text.substr(2);
            }
            jQuery("#site-search #jq-select-hd-text").text(cate.text);
            jQuery("#site-search #cateID").val(cate.id);
            jQuery("#jq-select-opt").hide();
        }
    }).hover(function() {
        jQuery(this).addClass("hover").siblings("li").removeClass("hover");
    },function() {
        jQuery("#jq-select-opt ul li").removeClass("hover");
    });
    if (jQuery("#cateID").length) {
        var cateID = jQuery("#cateID").val();
        if (cateID.length > 0 && cateID != "0" ) {
        	jQuery("#jq-" + cateID).trigger("click");
        };
    }
    //all cat
    var allCate = {
        "expand":function() {//all-cate show
            jQuery("#all-cate-title").addClass("title-hover")
            jQuery("#all-cat-content").show();
         },
        "collapse":function() {//all-cate hide
            jQuery("#all-cate-title").removeClass("title-hover")
            jQuery("#all-cat-content,#catePanel").hide();
         },
         "subCate":function(obj,x,y) {//open sub cate list
            catePanel.find("ul").remove();
            catePanel.append(obj)
                     .css({
                         "left":x,
                         "top":y
                     }).show();
         }
    }
    jQuery("#all-cate-title").bind({
        "click":function() {
            if(jQuery("#all-cat-content").is(":hidden")) {
                allCate.expand();
            }
            else {
                allCate.collapse();
            }
        }
    })
    jQuery("#all-cat-content").bind({
        "mouseleave":function(e) {
            var parents = jQuery(e.relatedTarget).parent();
            if (parents.index(jQuery("#catePanel")) >= 0 || jQuery(e.relatedTarget).index($("#catePanel")) >= 0 ){
                return;
            }
            allCate.collapse();
        }
    })
    jQuery("#all-cat-content #top-cat > li").bind({
        "mouseenter":function() {
            var content = jQuery(this).find("ul").clone(),
                x = jQuery(this).offset().left + 150,
                y =jQuery(this).offset().top -10;
            allCate.subCate(content,x,y);
        }
    })
    jQuery("#catePanel").bind({
        "mouseleave":function(e) {
                    if (jQuery(e.relatedTarget).parents().index($("#all-cat-content")) >= 0|| $(e.relatedTarget) == $("#all-cat-content")) {
                        return;
                    }
                    allCate.collapse();
                }
    })
    jQuery(document).bind({
        "click":function() {
            var evt=getEvent(); 
            var element=evt.srcElement || evt.target;
            if((element.parentNode.parentNode==null||element.parentNode.parentNode.id!="select-cat")&&element.id!="jq-select-hd"){
                jQuery("#jq-select-opt").hide();
            }
        }
    })
function getEvent(){ 
     if(document.all)    return window.event; 
     func=getEvent.caller; 
     while(func!=null){ 
         var arg0=func.arguments[0]; 
         if(arg0){ 
             if((arg0.constructor==Event || arg0.constructor ==MouseEvent) 
                || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){     
                return arg0; 
              } 
         } 
          func=func.caller; 
        } 
        return null; 
}

//top lead bnr by qilei 20100531
/////////////////////////////////////////
function checkBnrHide(){
    var bnr = jQuery("#top-lead-bnr");
    var close = jQuery("#top-lead-bnr a.close:first");
    var h = bnr.data('handler');
    close.click(function(){
        h && clearTimeout(h);
        handler();
        return false;
    });
    var callback = function() {
        bnr.detach();
    };
    var handler = function() {
        bnr.animate({ marginTop: -bnr.height()}, 1000 ,null,callback);
    };
    h && clearTimeout(h);
    h = setTimeout(handler, 10000); // 10s
    bnr.data('handler', h);
}
    checkBnrHide();
////////////////////////////////////////

//end ready
});
