﻿/*
author:grayguo
time:2011/9/9
<item>
1:动态设定内容高
1.1:对比设定最大高
2：JS选项卡
3:移除点击边框
4:鼠标移动到对象上交换图片
5:获取全局键盘事件
6:获取元素相对位置
7：Js淡入淡出新闻效果
8.移除指定元素最后一个指定样式
9.调整指定样式背景宽度为窗口可见区域的宽度
10.设置对象只读属性
11.设定table简单边框样式
12:htmlEncode&HtmlDeCode
13:禁止点击右键
13.1：禁止复制粘贴
</item>
*/

/*
1:
设定页面某一内容固定高（防止页面内容高度在回发事随内容而变化）
rclass：依据的tag 从中找出最大高的值
tclass:需要设定的tag(unique)
offset:高的偏移值
*/
function setFixedHeightByGray(rclass, tclass, offsetValue) {
    var $rclassList = $(rclass);
    var $tclassList = $(tclass);
    var $MaxValue = 0;
    var $DefaulHeight = $tclassList.height();
    if ($rclassList.length == 0 || $tclassList.length == 0) {
        return false;
    }
    $MaxValue = $($rclassList[0]).height();
    for (var i = 0; i < $rclassList.length; i++) {
        if ($($rclassList[i]).height() > $MaxValue) {
            $MaxValue = $($rclassList[i]).height();
        }
    }
    if ($MaxValue > $DefaulHeight) {
        $tclassList.css({ "height": $MaxValue + offsetValue + "px" });
    }
}
/*
1.1:对比对象最大高进行设定*父元素的高*
rclass：对象1
tclass:对象2
offset:高的偏移值
*/
function setMaxHeightByGray(rclassg, tclassg, offsetValueg) {

    var $h1 = $(rclassg).height();
    var $h2 = $(tclassg).height();
    if ($h1 > $h2) {
        $($(rclassg).parent()).css("height", $h1 );
        $($(tclassg).parent()).css("height", $h1 + offsetValueg);
    }
    else {
        $($(rclassg).parent()).css("height", $h2 );
        $($(tclassg).parent()).css("height", $h2 + offsetValueg);
    }
}
/*
2:
JS选项卡效果
cClass:控制按钮
cdClass:被控内容
*/
function tabCarByGray(cClass, cdContent,Defaultum) {
    var $cClassList = $(cClass);
    var $cdContentList = $(cdContent);
    var $last = $cClassList.length;
    $cdContentList.css("display", "none");
    $($cdContentList[Defaultum]).css({ "display": "block" });
    for (var i = 0; i < $cClassList.length; i++) {
        $($cClassList[i]).attr("Numgray", i).click(function () {
           
            $cdContentList.css({ "display": "none" });
            $($cdContentList[$(this).attr("Numgray")]).css("display", "block");

        });

    }
}


/*
3:
设置按钮取消点击时的边框
classname:需要移除的tag
*/
function removeClickBorderByGray(classname) {
    $(classname).focus(function () {
        $(this).blur();
    })
}
/*
4:
交换ImageSrc
*/
function switchImageMouseOverByGray(objectName,imageOne,imageTwo) {
    $(objectName).mouseover(function () {
        $(this).attr({ "src": imageTwo });
    }).mouseout(function () {
        $(this).attr({ "src": imageOne });
    });
}

function switchBackgrundImageMouseOverByGray(objectName, imageOne, imageTwo) {
    $(objectName).mouseover(function () {
        $(this).css({ "background-image": "url("+imageTwo+")"});
    }).mouseout(function () {
        $(this).css({ "background-image":"url("+ imageOne+")" });
    });
}
/*
5：
获取全局键盘事件进行处理
*/
/*1)设置回车按钮激发的事件对象*/
function SetSubmitButtonOnEnterByGray(controlIdg) {
    $("body").keyup(function (eveng) {
        if (eveng.keyCode == "13") {
            eveng.preventDefault();
            __doPostBack(controlIdg, "jsPostbackByGray");

        }
    });
}
/*
6:
获取元素绝对位置（Left,Top）
*/
function getElementLeft(element) {
    var actualLeft = element.offsetLeft;
    var current = element.offsetParent;
    while (current !== null) {
        actualLeft += current.offsetLeft;
        current = current.offsetParent;
    }
    return actualLeft;
}
function getElementTop(element) {
    var actualTop = element.offsetTop;
    var current = element.offsetParent;
    while (current !== null) {
        actualTop += current.offsetTop;
        current = current.offsetParent;
    }
    return actualTop;
}

/*
7:
Js淡入淡出新闻效果
cbtng:控制按钮
ridg:按钮顺序(定义属性 eg:bid=1,bid=2...)
cimgg:被控图片
gotimeg：自动跳转时间
*/
function ImagefadeShowByGray(cbtng, ridg, cimgg, gotimeg) {
    $controlBtn = $(cbtng);
    $Images = $(cimgg);
    $controlBtn.css({ "cursor": "pointer" });
    $Images.css({ "display": "none", "position": "absolute" });
    $($Images[0]).attr("IsPlay", "true").css({ "display": "block" });
    if ($($controlBtn[0]).attr("runningg") != "true") {
        var timeid = window.setTimeout("GoImageByGray('" + cbtng + "','" + ridg + "','" + cimgg + "','" + gotimeg + "')", gotimeg);
        $($controlBtn[0]).attr({ "timeID": timeid, "runningg": "true" });
    }

    $controlBtn.mouseenter(function () {
        if ($($controlBtn[0]).attr("timeID") != null && $($controlBtn[0]).attr("runningg") == "true") {
            window.clearTimeout($($controlBtn[0]).attr("timeID"));
            $($controlBtn[0]).attr("runningg", "false");
        }

        for (var i = 0; i < $Images.length; i++) {
            if ($($Images[i]).attr("IsPlay") == "true") {
                if (i == $(this).attr(ridg)) {
                    return;
                }
                $($Images[i]).attr("IsPlay", "false").fadeOut(400);
            }
        }
        $($Images[$(this).attr(ridg)]).fadeIn(500);
        $($Images[$(this).attr(ridg)]).attr("IsPlay", "true")


    }).mouseout(function () {

        if ($($controlBtn[0]).attr("runningg") != "true") {
            var timeid = window.setTimeout("GoImageByGray('" + cbtng + "','" + ridg + "','" + cimgg + "','" + gotimeg + "')", gotimeg);
            $($controlBtn[0]).attr({ "timeID": timeid, "runningg": "true" });
        }
    });

}
function GoImageByGray(cbtng, ridg, cimgg, gotimeg) {

    $controlBtn = $(cbtng);
    $Images = $(cimgg);

    for (var i = 0; i < $Images.length; i++) {
        if ($($Images[i]).attr("IsPlay") == "true") {
            $($Images[i]).attr("IsPlay", "false").fadeOut(600);
            var j = i + 1;
            if (j <= $Images.length - 1) {
                $($Images[j]).attr("IsPlay", "true").fadeIn(600);
            }
            else {
                $($Images[0]).attr("IsPlay", "true").fadeIn(600);
            }
            break;
        }
    }
    var timeid = window.setTimeout("GoImageByGray('" + cbtng + "','" + ridg + "','" + cimgg + "','" + gotimeg + "')", gotimeg);
    $($controlBtn[0]).attr({ "timeID": timeid, "runningg": "true" });
}
/*
8:移除指定元素序列最后一个元素的指定样式
targetg:指定元素
classg：移除的类型
flagg:对象属性标示
*/
function RemoveBK(targetg, classg,flagg) {
    $(targetg).removeClass(classg).addClass(classg);

    $Alllist = $(targetg);
    $lastOne = $($Alllist[0]);
    for (var i = 0; i < $Alllist.length; i++) {
        if ($($Alllist[i]).attr(flagg) == 'true') {
            $lastOne = $($(targetg)[i]);
        }
    }
    $lastOne.removeClass(classg);
}
/*
9:调整对象的宽度为可见区域的100%
*/
function SetWidthAllScreen(tObjectg) {

    $(window).bind("scroll", function () {
        $(tObjectg).css({ "width": document.documentElement.clientWidth + $(window).scrollLeft() });
    }).bind("resize", function () {
        $(tObjectg).css({ "width": document.documentElement.clientWidth + $(window).scrollLeft() });
    });
}
/*
10:设置对象只读属性
*/
function SetObjectReadOnely(tobjectg) {
    $(tobjectg).focus(function () {
        $(tobjectg).blur();
    });
}
/*
11:设定table边框样式（简单）
Tclassg:table的标记
Colorg：颜色
*/
function SetTableBorderSimpleStyleByGray(Tclassg, Colorg) {
    $table = $(Tclassg).find("table");
    $table.attr({ "cellpadding": "0", "cellspacing": "0" });
    $table.find("tr td").css({ "border": "1px solid " + Colorg });
    $tableTr = $table.find("tr");
    for (var i = 0; i < $tableTr.length; i++) {
        $currenttd = $($tableTr[i]).find("td");
        $currenttd.css({ "border": "1px solid " + Colorg });
        for (var j = 0; j < $currenttd.length; j++) {
            $($currenttd[j]).css({ "border": "1px solid " + Colorg });
            if (j != $currenttd.length - 1) {
                $($currenttd[j]).css("border-right", "none");
            }
        }
        if (i != $tableTr.length - 1) {
            $currenttd.css("border-bottom", "none");
        }
    }
}
/*
11.HtmlEncode和HtmlDecode
*/
function htmlEncode(str) {
    var div = document.createElement("div"); var text = document.createTextNode(str); div.appendChild(text); return div.innerHTML;
 }
 function htmlDecode(str) {
     var div = document.createElement("div"); div.innerHTML = str; return div.innerHTML;
 }
 /*
 13:禁止用户点击右键
 */
 function disableRightClickByGray() {
     var message = "Disabled!";
     function clickIE4() {
         if (event.button == 2) {
             alert(message); return false;
          }
      }
      function clickNS4(e) {
          if (document.layers || document.getElementById && !document.all) {
              if (e.which == 2 || e.which == 3) {
                  alert(message); return false;
              } 
          } 
      }
      if (document.layers) {
          document.captureEvents(Event.MOUSEDOWN); document.onmousedown = clickNS4;
      } 
      else if (document.all && !document.getElementById) {
          document.onmousedown = clickIE4;
      }
      // document.oncontextmenu = new Function("alert('123');return false")
       document.oncontextmenu = new Function("return false")

   }
/*
13.1：禁止复制粘贴
*/
   function disableCopyPaseByGray() {
       document.onselectstart = new Function('return false');
       document.ondragstart = new Function('return false');
       
   }
