﻿// 获取对象的方法，可以根据传入单个ID来返回对象，
// 也可以传入多个ID来返回一个对象数组

// 例如：var obj = $("id1");
// var arrObj = $("id1", "id2", "id3");
// arrObj[0] = Obj1;
function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string') element = document.getElementById(element);
        if (arguments.length == 1) return element;
        elements.push(element);
    }
    return elements;
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }

    return returnValue;
  }
}

var Ajax = function() {
    return Try.these(
      function() {return new XMLHttpRequest()},
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
    ) || false;
}


function Cacher() {
    this.count = 0;
}

Cacher.prototype = {
    insert: function(key, value) {
        if (!this[key]) {
            this[key] = value;
            this.count++;
        }
    }, 

    item: function(key) {
        if (this[key]) return this[key];
        else return null;
    }, 

    remove: function(key) {
        if (this[key]) {
            delete this[key];
            this.count--;
        }
    }
}

function thisMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}

function getFlashObj() {
    var flash_obj = thisMovie("videoPlayer");
    return flash_obj;
}

//image_path:图片地址 video_path：视频地址
function InitFlash(image_path,video_clear_path, video_path,insID){
	var flash_obj=getFlashObj();
    try { flash_obj.InitFlash(image_path,video_clear_path, video_path,insID);}
	catch(e) {
	    var sta = "InitFlash('" + image_path + "', '" + video_clear_path + "', '" + video_path + "'," + insID + ")";
	    window.setTimeout(sta, 200);
	}
}

function PlayVideo(){
	var flash_obj=getFlashObj();
    try {
	    flash_obj.JsPlayVideo()
	}
	catch(e) {
	    window.setTimeout("PlayVideo()", 200);
	}
}
//关闭视频
function closeVideo(){
	try {
	    var flash_obj = getFlashObj();
	    flash_obj.closeVideo()
	}
	catch(e) {
	    window.setTimeout("closeVideo()", 200);
	}
}

// 获得e的x坐标
function getX(e) {
    var x = 0;
    while(e) {
        x += e.offsetLeft;
        e = e.offsetParent;
    }
    return x;
}

// 获得e的y坐标
function getY(el) {
    var y = 0;
    for (var e = el; e; e = e.offsetParent) y += e.offsetTop;
    
    for (e = el.parentNode; e && e != document.body; e = e.parentNode)
        if (e.scrollTop) y -= e.scrollTop;

    return y;
}

function detectFlash() {
    AC_FL_RunContent(
		    "src", "swf/player",
	        "FlashVars", "",
		    "width", "326",
		    "height", "300",
		    "align", "middle",
		    "id", "videoPlayer",
		    "quality", "high",
		    "bgcolor", "#FFFFFF",
		    "name", "videoPlayer",
		    "wmode", "transparent",
		    "allowScriptAccess","sameDomain",
		    "type", "application/x-shockwave-flash",
		    'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
		    "pluginspage", "http://www.adobe.com/go/getflashplayer"
    );
}

function Tooltip() {
    this.tooltip = document.createElement("div");
    this.tooltip.style.visibility = "hidden";
    this.tooltip.className = "tooltip";

    // iframe挡住透过元素
    this.shield = document.createElement("iframe");
    this.shield.style.position = "absolute";
    this.shield.style.width = 390 + "px";
    this.shield.style.height = 190 + "px";

    this.title = document.createElement("div");
    this.title.className = "mtop";
    this.title.innerHTML = "<div class=\"mc\"><h4>企业公告，请您关注</h4><div class=\"del\"><img src=\"http://image.chinahr.com/a/video/images/del.gif\" alt=\"关闭\" onclick=\"if (tip) tip.hide();\" /></div></div>";

    this.content = document.createElement("div");
    this.content.className = "mcon";

    this.tooltip.appendChild(this.shield);
    this.tooltip.appendChild(this.title);
    this.tooltip.appendChild(this.content);
}

Tooltip.prototype.show = function(text, x, y) {
//    if ((screen.availWidth - x) < 413 ) x -= 155;
    if (x < 0) x += 158;
    y -= 128;
    y = (y < Geometry.getVerticalScroll()) ? Geometry.getVerticalScroll() : y;
    var botRange = Geometry.getViewportHeight() - (y - Geometry.getVerticalScroll());
    var shiftRange = (botRange < 208) ? (botRange - 208) : 0;
    y += shiftRange;

    this.content.innerHTML = text;
    this.tooltip.style.left = x + "px";
    this.tooltip.style.top = y + "px";
    this.tooltip.style.visibility = "visible";
    this.shield.style.top = getY(this.tooltip);
    this.shield.style.zIndex = -1;

    // 第一次显示的时候将外层div添加到body中
    if (this.tooltip.parentNode != document.body)
        document.body.appendChild(this.tooltip);
};

Tooltip.prototype.hide = function() {
    this.tooltip.style.visibility = "hidden";
    this.tooltip.style.display = 'none';
};

var ajax, tip;
var openedId;
var cacher = new Cacher();
var bltShowTimer, bltHideTimer;

function displayBulletin(companyId, img, toHide) {
    if (toHide) {
        if (bltShowTimer) window.clearTimeout(bltShowTimer);
        //if (bltHideTimer) window.clearTimeout(bltHideTimer);
        hideTip();
        return;
    }
    else {
        // 在弹出公告层上挪动时，清除关闭层的动作并阻止重新打开层
        if (openedId == companyId) {
            if (bltHideTimer) {
                window.clearTimeout(bltHideTimer);
                bltHideTimer = null;
                if (tip && tip.tooltip.style.visibility == "visible") return;
            }
        }
        // 从一个公告上快速挪动到另一个时清除打开第一个公告的动作
        else {
            if (bltShowTimer) window.clearTimeout(bltShowTimer);
        }
        openedId = companyId;
    }

    bltShowTimer = window.setTimeout(showTip, 500);

    function showTip() {
        tip = new Tooltip();
        tip.tooltip.onmouseover = function() { displayBulletin(companyId, img, 0); };
        tip.tooltip.onmouseout = function() { displayBulletin(companyId, img, 1); };
        var msg = cacher.item(companyId);
        if (msg == 'undefined' || msg == null) {
            var url = 'MemNoticeInfo.aspx?adInsID=' + companyId;
            ajax = new Ajax();
            ajax.onreadystatechange = function() { callback(companyId, img); };
            ajax.open('GET', url);
            ajax.send(null);
        }
        if (msg != 'undefined' && msg != null) {
            tip.show(msg, (getX(img) - 415), getY(img));
        }
    }

    function hideTip() {
        bltHideTimer = window.setTimeout(function() { if (tip) tip.hide(); }, 500);
    }
}

function callback(companyId, img) {
    if (ajax.readyState == 4) {
        cacher.insert(companyId, ajax.responseText);
        displayBulletin(companyId, img);
    }
}

// prefetch g1, g3 for cache
(new Image()).src = "http://st.mychinahr.com/a/video/images/g1.gif";
(new Image()).src = "http://st.mychinahr.com/a/video/images/g3.gif";
function LoadBulletinImg(imgObj,onlineTools, onlineTime, notice, noticeCreateDate, companyId) {
    var msg = "<div class=\"mbody\"><div class=\"con\">{0}</div><div class=\"notes\"><b>联系方式：</b>{1}<br /><b>交谈时间：</b>{2}</div></div><div class=\"mbm\"><div class=\"mc\"><b>提示：</b>将招聘方的MSN/QQ添加为好友，可在指定的时间交谈！</div><div class=\"mr\"></div></div>";
    if (notice == "") {
        msg = msg.replace("{0}", "该公司目前没有公告信息。").replace("{1}", onlineTools).replace("{2}", onlineTime);
    }
    else {
        msg = msg.replace("{0}", notice).replace("{1}", onlineTools).replace("{2}", onlineTime);
    }
    cacher.insert(companyId, msg);
    if( noticeCreateDate.length > 0 ) {
        img = $(imgObj);
        img.style.cursor = 'hand';
        today = new Date();
        dates = noticeCreateDate.split('-');
        var rangeDate = getNDaysAfterDate(3,new Date(dates[0], dates[1], dates[2]))

        if (onlineTools == '' && onlineTime == '' && notice == '') {
            img.src = "http://st.mychinahr.com/a/video/images/g3.gif";
        }
        else {
            img.src="http://st.mychinahr.com/a/video/images/g1.gif";
        }

//        else if (today < rangeDate)
//        {
//            img.src="/images/g1.gif";
//        }
//        else
//        {
//            img.src="/images/g2.gif";
//        }
    }
}

//取得指定日期的后N天的日期
function getNDaysAfterDate(n,today_date) {
	var oneDayMilliseconds = 86400000;
	var stepMilliseconds = oneDayMilliseconds*n;
	var nowMilliseconds = Date.UTC(today_date.getFullYear(), today_date.getMonth(), today_date.getDate());
	var startDate = new Date(nowMilliseconds + stepMilliseconds);
    startDate = new Date(startDate.getFullYear(),(startDate.getMonth() - 1),startDate.getDate());

	return startDate;
}

//隐现效果
function showlayer(ccname,bbname) {
    var cc = document.getElementById(ccname);
	if(cc.style.display=="") {
		cc.style.display="none";
		document.getElementById(bbname).className="mtop up";
	}
	else {
		cc.style.display=""
		document.getElementById(bbname).className="mtop down";
	}
}

function onKeyDown() {
    if (event.keyCode == 116) {
        event.keyCode = 0;
        event.returnValue = false;
    }
}

//取地址栏参数

var request = { 
    QueryString : function(val) {
        var uri = window.location.search; 
        var re = new RegExp("" +val+ "=([^&?]*)", "ig"); 
        return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):""); 
    } 
};
