/**
 * 插入一个Flash
 */
function WriteFlash(flash,_width,_height) {
	document.writeln("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"" + _width + "\" height=\"" + _height + "\">");
	document.writeln("    <param name=\"movie\" value=\"" + flash + "\" />");
	document.writeln("    <param name=\"quality\" value=\"high\" />");
	document.writeln("    <param name=\"wmode\" value=\"transparent\" />");
	document.writeln("    <embed src=\"" + flash + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + _width + "\" height=\"" + _height + "\" wmode=\"transparent\"></embed>");
	document.writeln("</object>");
}

/**
 * 等比缩放图片
 */
function thumbImg(drawImage) {    
    var fixwidth = 300;    
    var fixheight = 260;  
    w=drawImage.width;
	h=drawImage.height;    
    if(w>fixwidth) { drawImage.width=fixwidth;drawImage.height=h/(w/fixwidth);}    
    if(h>fixheight) { drawImage.height=fixheight;drawImage.width=w/(h/fixheight);}          
    drawImage.style.cursor= "pointer";    
    drawImage.onclick = function() { window.open(this.src);}    
    drawImage.title = "点击查看原始图片";  
}

$(document).ready(function() {
	$.ajaxSetup ({
		cache: false
	});
	// 阅读书籍-连接到搜索引擎
	$('a[id="readsearch"], input[id="readsearch"]').each(function(){
		$(this).click(function(){			
			var $searchKey = encodeURI($(this).attr('key').replace(/\//g,'／').replace(/\?/g,'？').replace(/&/g,'＆'));
			var $searchUrl = SEARCH_SERVER_URL + '/search/book/'+$searchKey+'/1-all-all-all';
			window.open($searchUrl);
			return false;
		});
	});
});

/**
 * 获取会员头像地址
 */
var getavatar = function ($uid, $flag){
	$filename = $flag ? 'max_' . $uid : $uid;
	return PASSPORT_IMAGES_URL + '/user/' + (Math.ceil(parseInt($uid) / 1000)) +'/'+ $filename + '.jpg'; // 会员头像
}

/**
 * 处理 Ajax 返回的JSON数据
 * @param string $data JSON数据 array('state'=>'成功1 或 失败0', 'message'=>'提示信息', .....)
 */
var ajaxJsonData = function ($data){
	try{
		var $return = eval('(' + $data + ')');
	}catch(e){					
		$data = '{"state":"0", "message":"数据加载出错!请联系管理员."}';
		var $return = eval("(" + $data + ")");
	}
	$return.state = parseInt($return.state);
	return $return;
}

/**
 * 复制内容到剪切板
 *
 * @param string text 被复制的内容
 * @param string alertmsg Alert 提示信息
 */
function setcopy(text, alertmsg){
	if($.browser.msie) {
		clipboardData.setData('Text', text);
		alert(alertmsg);
	} else if(prompt('Press Ctrl+C Copy to Clipboard', text)) {
		alert(alertmsg);
	}
}

/**
 * 设置cookie
 */
function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds);
	domain = !domain ? '.isoshu.com' : domain;
	path = !path ? '/' : path;
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

/**
 * 读取cookie
 */
function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}
// 注册插件
jQuery.fn.extend({
	/**
	 * 通用选项卡Jquery 插件
	 *
	 * @param string event 事件类型 click|mouseover|........
	 * @param function callback 回调函数
	 * @param int currentItem 当前默认tab索引
	 *
	 * @author bottle hhyisw@163.com
	 */
	tab: function(event, callback, currentItem){
		currentItem = currentItem ? currentItem : 0;
		event = event ? event : 'click';
		var $tabnav = $('div#tabnav ul li', this);
		var $tabs   = $('div#tabs', this).children();
		// tab 默认状态
		$tabnav.eq(currentItem).addClass('selected').siblings().removeClass('selected');		
		if(callback){
			callback($tabs.eq(currentItem), currentItem);
		}
		$tabs.eq(currentItem).show().siblings().hide();
		// tab 切换事件绑定
		$tabnav.each(function(item){
			$(this).bind(event, function(){
				$(this).addClass('selected').siblings().removeClass('selected');
				if(callback){
					// 回调函数 默认传递标签对象和标签索引 用以AJAX更新标签数据
					callback($tabs.eq(item), item);
				}
				$tabs.eq(item).show().siblings().hide();
			});	
		});
	},
	
	/**
	 * 通用 checkbox 全选插件
	 *
	 * @param string childcheckbox 子 checkbox ID
	 *
	 */
	checkbox: function(childcheckbox) { 
	  this.click(function(){
			var checkall  = $(this);			
			var ischecked = checkall.attr('checked');			
			ischecked     = ischecked == true ? true : false;
			$("input[id='"+childcheckbox+"']").each(function() {   
				$(this).attr("checked", ischecked); 
				$(this).click(function () {
					var ischecked = checkall.attr('checked');
					ischecked     = ischecked == true ? true : false;
					if(ischecked){
						checkall.attr('checked', ischecked&0);
					} 
				});
			});
		});
	},

	/**
	 * 通用下拉列表莱单
	 *
	 * @param callback 回调函数
	 * @param many     最大多选数量
	 * @author bottle hhyisw@163.com
	 */
	dropdown: function(callback, many) {
		var $dropoption   = $('ul#dropoption', this);		// 下拉列表对象
		var $dropvalue    = $('input#dropvalue', this);     // 存放selected 值的input
		var $dropselected = $('div#dropselected', this);    // 存放selected 文本的div
		var $dropbotton   = $('a#dropbotton', this);		// 下拉框点击按钮对象
		var $dropclose    = false;							// 决定是否关闭下拉框的状态           
		
		$dropbotton.click(function(){
			$dropoption.show('normal');
		});

		$dropoption.hover(
			function(){
				$dropclose = true;
			},
			function(){
				if($dropclose == true){
					$dropoption.hide('normal', function(){
						// 处理多选
						if(many){
							var $texts = $vals = $flag = '';
							$('input:checked', $dropoption).each(function(item){
								$texts = $texts + $flag + ' ' +$.trim($(this).parent().text());
								$vals  = $vals + $flag + $(this).val();
								$flag = ',';
								if(item == (many-1)) return false;
							});
							$dropselected.text($texts);
							$dropvalue.val($vals);
						}
					});
				}
				$dropclose = false;
			}
		);
		if(many) return false;
		$('li a', $dropoption).each(function(){
			$(this).click(function(){
				var $value = $(this).attr('val');
				var $text  = $(this).text();
				var $currentval = $dropvalue.val();
				$dropvalue.val($value);
				$dropselected.text($text);
				$dropoption.hide('normal');
				$dropclose = false;
				if(callback && !many){
					// 多选时不回调
					callback($value, $currentval); // 默认传递当前选择值和之前值
				}
			});
		});
	}

});