var sUserAgent = navigator.userAgent;
var sortFlag = 0;
var currentSortChild;
var isOpera = sUserAgent.indexOf("Opera") > -1;
var isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
$(document).ready(function() {
    $(".productSort").find("div[class=popWrap]").each(function() {
        $(this).hide();
        //alert($(this).parent().find("a").width());
        $(this).parent().find("a").bind("mouseover", function() {
            sortFlag = 1;
            var mObj = $(this).parent().find("div[class=popWrap]");
            showFunction(mObj);
        });
        $(this).parent().find("a").mouseout(function() {
            var mObj = $(this).parent().find("div[class=popWrap]");
            sortFlag = 0;
            currentSortChild = mObj;

            setTimeout(function() { hideFunction(); }, 1000);
        });
        $(this).find("a").unbind("mouseover").unbind("mouseout");
        $(this).bind("mouseover", function() { sortFlag = 1;  });
        $(this).bind("mouseout", function() { sortFlag = 0; currentSortChild = $(this); setTimeout(function() { hideFunction(); }, 1000); });
    })
})
//------------------------------------------------------------------------------------------------
function hideFunction() {

    if (sortFlag == 0) {
        //currentSortChild.hide("slow");
        currentSortChild.hide();  
        //alert(obj.parent());
        //obj.hide("slow");
    }
}
function showFunction(obj) {
    setPostion(obj);
    hideAllSort(obj);
    //obj.show("slow");
    obj.show();
}
function hideAllSort(obj) {
    //alert(obj.html());
    $(".productSort").find("div[class=popWrap]").each(function() {
        if($(this)!=obj){
            $(this).hide();
        }
    })
}
//------------------------------------------------------------------------------------------------------
function getShowPos(obj) {
    var x = obj.offsetLeft;
    while (obj = obj.offsetParent) {
        x += obj.offsetLeft;
    }
    if (x * 2 < document.body.offsetWidth) {
        return "right";
    } else {
        return "left";
    }
}
//----------------------------------------------------------------------------------------------------------
function setPostion(mObj) {
    mObj.css("position", "absolute");
    var pop_pos = getShowPos(mObj.parent().find("a")[0]);
    var objMenu = mObj[0];
    
    if (pop_pos != "left") {
        //right pop

        var pid = mObj.parent().find("a");
        var pd = pid[0];
        var m_width = pid.width();
        var m_height = pid.height();
        var m_x = pd.offsetLeft;
        var m_y = pd.offsetTop;
        while (pd = pd.offsetParent) {
            
            m_x += pd.offsetLeft;
            m_y += pd.offsetTop;
            
        }
        //Coupled with the migration picture
        m_x += m_width + 10;
        m_y -= 4;
        
        if (isIE) {
            
            objMenu.style.left = m_x;
            objMenu.style.top = m_y;
            
        } else {
            objMenu.style.left = m_x + "px";   
            objMenu.style.top = m_y + "px";
        }
       // alert(objMenu.style.left);
        
    } else {
        //Left pop

    var pid = mObj.parent().find("a");
    var pd = pid[0];
    var m_width = pid.width();
    var m_height = pid.height();
    var m_x = pd.offsetLeft;
    var m_y = pd.offsetTop;

    while (pd = pd.offsetParent) {
        m_x += pd.offsetLeft;
        m_y += pd.offsetTop;
    }
    //Minus the width of the pop-up box
    m_x += m_width + 10;
    m_y -= 4;
        if (isIE) {
            objMenu.style.left = m_x;
            objMenu.style.top = m_y;
        } else {
            objMenu.style.left = m_x + "px";
            objMenu.style.top = m_y + "px";
        }
        
    }
}
