﻿//  页面加载完成时执行的初始化
$(function(){
    //  搜索关键字不能为空
    $('#formSearch').bind('submit', function(){
        if ($('#Keyword').val()==''){
            alert('关键字不能为空');
            return false;
        }
        return true;
    });
    //  禁止页面选取事件
    $("#Keyword").focus(function(){
        $(this).parent().attr("class", "focus");
    }); 
    $("#Keyword").blur(function(){
        $(this).parent().attr("class", "blur");
    }); 
    //  如果有验证码对象则初始化值
    var _VerityId = $("#VerityId");
    if (_VerityId!=null && _VerityId.attr("tagName")!=null && _VerityId.attr("tagName").toUpperCase()=="INPUT" && _VerityId.attr("type")!=null && _VerityId.attr("type").toUpperCase()=="HIDDEN"){
        _VerityId.val(__VerityId);
    }
});
//  扩展单选框/复选框选中方法
;(function($){
	$.fn.radioChecked = function(value){
	    $(this).each(function(){
	        if ($(this).val() == value){
	            $(this).attr("checked", true);
	        }else{
	            $(this).attr("checked", false);
	        }
	    });
	};
	$.fn.goLocation = function(url, msg){
	    $(this).click(function(){
	        if (msg==null || confirm(msg)){
	            window.location = url;
	        }
	    });
	};
})(jQuery);
//  页面提示效果
;(function($){
	$.fn.sweetTips = function(){
	    var _self = this;
		$(_self).mouseover(function(e){
			this.myTitle = this.title;
			this.title = "";
			if ($('#tooltip').size()<=0){
			    var tooltip = "<div id='tooltip' style=' white-space: nowrap; '><p>"+this.myTitle+"</p></div>";
			    $('body').append(tooltip);
			}
		    var clientX = $(window).scrollLeft();
		    var clientY = $(window).scrollTop();
		    var clientWidth = $(window).width();
		    var clientHeight = $(window).height();
		    var offsetWidth = $('#tooltip').width();
		    var offsetHeight = $('#tooltip').height();
			$('#tooltip')
				.css({
					"opacity":"0.8",
					"top":((((clientHeight+clientY)-e.pageY)>(offsetHeight+20)?e.pageY+20:e.pageY-(offsetHeight+20)))+"px",
					"left":((((clientWidth+clientX)-e.pageX)>(offsetWidth+10)?e.pageX+10:e.pageX-(offsetWidth+10)))+"px"
				}).show('fast');
		}).mousemove(function(e){
		    var clientX = $(window).scrollLeft();
		    var clientY = $(window).scrollTop();
		    var clientWidth = $(window).width();
		    var clientHeight = $(window).height();
		    var offsetWidth = $('#tooltip').width();
		    var offsetHeight = $('#tooltip').height();
			$('#tooltip')
			.css({
				"top":((((clientHeight+clientY)-e.pageY)>(offsetHeight+20)?e.pageY+20:e.pageY-(offsetHeight+20)))+"px",
				"left":((((clientWidth+clientX)-e.pageX)>(offsetWidth+10)?e.pageX+10:e.pageX-(offsetWidth+10)))+"px"
			});
		}).mouseout(function(){
			this.title = this.myTitle;
			$('#tooltip').remove();
		}).mousedown(function(){
			this.title = this.myTitle;
			$('#tooltip').remove();
		});
	};
})(jQuery);
//  页面 Cookie 访问
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {  //  如果指定了 name 和 value ，则写入 cookie 
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // 指定过期时间属性, 最大值不支持 IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // 如果仅指定了 name 则读取 cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // 如果这个 cookie 字符串与我们提供的相同则此 cookie 为我们需要的 cookie
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
//  预览文件
jQuery.filePreview = function(fileId) {
    var wnd = $(window), doc = $(document);
    if ($("#overlayFilePreview").size()>0){
        $("#overlayFilePreview").show();
        $("#iframeFilePreview").attr("src", __Local + "Main/Broswer/FilePreview.aspx?FileId=" + fileId + "&d=" + Date.UTC(new Date()))
            .css({
					top:  ((wnd.height() - 505) / 2) + doc.scrollTop() + 'px',
					left: ((wnd.width() - 640) / 2) + doc.scrollLeft() + 'px'
			}).show();
    }else{
		// handle IE 6
		var myWidth, myHeight;
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			var scrollHeight = Math.max(
				document.documentElement.scrollHeight,
				document.body.scrollHeight
			);
			var offsetHeight = Math.max(
				document.documentElement.offsetHeight,
				document.body.offsetHeight
			);
			var scrollWidth = Math.max(
				document.documentElement.scrollWidth,
				document.body.scrollWidth
			);
			var offsetWidth = Math.max(
				document.documentElement.offsetWidth,
				document.body.offsetWidth
			);

			if (scrollHeight < offsetHeight) {
				myHeight = $(window).height() + 'px';
			} else {
				myHeight = scrollHeight + 'px';
			}

			if (scrollWidth < offsetWidth) {
				myWidth = $(window).width() + 'px';
			} else {
				myWidth = scrollWidth + 'px';
			}
		// handle "good" browsers
		} else {
			myHeight = $(document).height() + 'px';
			myWidth = $(document).width() + 'px';
		}
		$("<div></div>")
            .appendTo(document.body)
			.attr("id", "overlayFilePreview")
			.addClass('my-overlay').css({
				width: myWidth,
				height: myHeight,
				zIndex: 100
			});
        $("<iframe/>")
            .appendTo(document.body)
            .attr("id", "iframeFilePreview").attr("iframeborder", 0).attr("scrolling", "no").attr("width", 640).attr("height", 505).attr("src", __Local + "Main/Broswer/FilePreview.aspx?FileId=" + fileId + "&d=" + Date.UTC(new Date()))
            .css({
					position: 'absolute',
					top:  ((wnd.height() - 505) / 2) + doc.scrollTop() + 'px',
					left: ((wnd.width() - 640) / 2) + doc.scrollLeft() + 'px',
					zIndex: 200
				});
    }
};
//  输出评论表情选单
;(function($){
	$.fn.sweetSmiles = function(textarea){
	    var _self = this;
	    var _smiles = [["001","方向"],["002","咧嘴笑"],["003","迷惑"],["004","酷"],["005","难过"],["006","惊讶"],["007","邪恶"],["008","困惑"],["009","有主意了"],["010","大笑"],["011","疯了"],["012","菜鸟"],["013","中立"],["014","不解"],["015","吐舌头"],["016","窘迫"],["017","眼珠转动"],["018","悲伤"],["019","微笑"],["020","震惊"],["021","反常"],["022","眨眼"]];
	    var _smileHTML = "";
	    for(i=0;i<_smiles.length;i++){
	        _smileHTML += "<a title='" + _smiles[i][1] + "' smile='" + _smiles[i][0] + "'><img src='" + __Local + "Attach/Smiles/smile_" + _smiles[i][0] + ".gif' /></a> ";
	    }
		$(_self).html(_smileHTML);
		$("a", _self).hover(function(){
		    $(this).css({"background":"#8EC2F5"});
		},function(){
		    $(this).css({"background":"#ffffff"});
		}).click(function(){
		    var _textValue = '\\\\' + $(this).attr('smile');
		    var _textObj = textarea.get(0);
		        _textObj.focus();
		    if(document.selection){
		        document.selection.createRange().text = _textValue;
		    }else{
		        _textObj.value = _textObj.value.substr(0, _textObj.selectionStart) + _textValue + _textObj.value.substr(_textObj.selectionEnd);
		    }
		});
	};
})(jQuery);