// MYDOWN V4 skin
var urDate  = new Date();
var timeSec = urDate.getTime();

var quick_reply_arr = new Array();
var i = 0;

// 加入收藏
function addFavorite(sURL, sTitle) { 
    try { 
        window.external.addFavorite(sURL, sTitle); 
    } catch (e) { 
        try { 
            window.sidebar.addPanel(sTitle, sURL, ""); 
        } catch (e) { 
            alert("加入收藏失败,请手动添加."); 
        } 
    } 
} 



// 显示/隐藏
function show_hide(show,myArr) {
	for(key in myArr) {
		if (myArr[key] == show){
			$("#"+myArr[key]).show();
		}else{
			$("#"+myArr[key]).hide();
		}
	}
}

// 当前项
function show_class(show,myArr,className) {
	for(key in myArr) {
		if (myArr[key] == show){
			$("#"+myArr[key]).addClass(className);
		}else{
			$("#"+myArr[key]).removeClass(className);
		}
	}
}

//图片等比例缩小
function draw_image(imgsrc,w,h) {
	var image=new Image();
	image.src=imgsrc.src;
	if(image.height<image.width){
		if(image.width>w){
			imgsrc.width=w;
			imgsrc.height=(image.height*w)/image.width;
		}else{
			imgsrc.width=image.width;
			imgsrc.height=image.height;
		}
	}else{
		if(image.height>h){
			imgsrc.height=h;
			imgsrc.width=(image.width*h)/image.height;
		}else{
			imgsrc.width=image.width; 
			imgsrc.height=image.height;
		}
	}
}

// 获取URL参数
function query_string(fieldName){  
	var urlString = document.location.search;
	if(urlString != null){
		var typeQu = fieldName+"=";
		var urlEnd = urlString.indexOf(typeQu);
		if(urlEnd != -1){
			var paramsUrl = urlString.substring(urlEnd+typeQu.length);
			var isEnd =  paramsUrl.indexOf('&');
			if(isEnd != -1){
				return paramsUrl.substring(0, isEnd);
			}else{
				return paramsUrl;
            }
		}else{
			return null;
		}
	}else{
		return null;
	}
}

// load review
function load_review(weburl,node,topid,page) {
	$("#mydown_soft_reviewlist").val("");
	$("#mydown_soft_reviewlist").load(weburl+"/d/review.php?n="+node+"&mod=quote&result=html&top="+topid+"&Page="+page+"&ut="+timeSec);
}

// reply review
function review_reply(weburl,node,topid,pid) {
	var view_reply_id = "#mydown_review_reply_"+pid;
	quick_reply_arr[i] = view_reply_id;
	i++;
	$(view_reply_id).show();
	$(view_reply_id).load(weburl+"/d/review.php?n="+node+"&mod=quickreply&top="+topid+"&pid="+pid+"&ut="+timeSec);
	for (var f in quick_reply_arr) {
		if(quick_reply_arr[f] != view_reply_id){
			$(quick_reply_arr[f]).val("");
			$(quick_reply_arr[f]).hide();
		}
	}
}

$(document).ready(function(){
	var weburl  = $("#weburl").val();
	var node    = $("#n").val();

	if($("#quick_search")[0]){
		// search
		$("#search_nav_soft").mousemove(function(){
			var searchArr_t = new Array("search_nav_soft","search_nav_article");
			show_class("search_nav_soft",searchArr_t,"search-tool-active");
			if ($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")){
				$.browser.version == "7.0" ? $("#search_nav_article").css("margin-left","60") : $("#search_nav_article").css("margin-left","30");
				$.browser.version == "7.0" ? $("#search_nav_soft").css("margin-left","-122") : $("#search_nav_soft").css("margin-left","-122");
			}
			$("#n").val("1");
			$("#quick_search").flushCache();
		});
		
		$("#search_nav_article").mousemove(function(){
			var searchArr_t = new Array("search_nav_soft","search_nav_article");
			show_class("search_nav_article",searchArr_t,"search-tool-active");
			if ($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")){
				$("#search_nav_soft").css("margin-left","-2");
				$("#search_nav_article").css("margin-left","0");
			}
			$("#n").val("0");
			$("#quick_search").flushCache();
		});
		// quick search
		$("#quick_search").autocomplete(  
			weburl+"/d/search.php?mod=doquick",
			{
				minChars: 2,
				autoFill:false,
				max: 15,
				scroll: false,
				dataType: 'json',
				width: 369,
				extraParams:{ n:function(){return $('#n').val()} ,keyword: function(){return $.trim($('#quick_search').val());} },
				parse: function(data) {
					var rows = []; 
					if (data){
						var rows = [];   
						for(var i=0; i<data.length; i++){
							if (data[i].name){
								var fullname = data[i].name+" "+data[i].version;
							}else{
								var fullname = data[i].title;
							}
							
							fullname = fullname.replace(eval("/("+$('#quick_search').val()+")/gi"),"<font color=red>$1</font>");
							rows[rows.length] = {
								data:"<span style='float:left;'>"+fullname+"</span> <span style='color:#ccc;float:right;'>"+(data[i].size ? data[i].size : data[i].author)+"</span>",   
								value:data[i].pagename
							};   
						}   
					}
					return rows;   
				},   
				formatItem: function(row, i, n) {   
					return row;
					//return row.replace(eval("/("+$('#quick_search').val()+")/gi"),"<font color=red>$1</font>");
				},
				formatResult: function(row) {
					return row.value;
				}
			}
		);
		$("#quick_search").result(function(event, data, formatted) {
			location.href = formatted;
		});
	}


	// subject && news && commend && hot && article 
	if ($("#mydown_index_news_t")[0]){
		var newsArr_t = new Array("mydown_index_subject_t","mydown_index_news_t","mydown_index_usercommend_t","mydown_index_weekhot_t","mydown_index_article_t");
		var newsArr_c = new Array("mydown_index_subject_c","mydown_index_news_c","mydown_index_usercommend_c","mydown_index_weekhot_c","mydown_index_article_c");
		$("#mydown_index_subject_t").mousemove(function(){
			show_class("mydown_index_subject_t",newsArr_t,"current");
			show_hide("mydown_index_subject_c",newsArr_c);
		});
		$("#mydown_index_news_t").mousemove(function(){
			show_class("mydown_index_news_t",newsArr_t,"current");
			show_hide("mydown_index_news_c",newsArr_c);
		});
		$("#mydown_index_usercommend_t").mousemove(function(){
			show_class("mydown_index_usercommend_t",newsArr_t,"current");
			show_hide("mydown_index_usercommend_c",newsArr_c);
		});
		$("#mydown_index_weekhot_t").mousemove(function(){
			show_class("mydown_index_weekhot_t",newsArr_t,"current");
			show_hide("mydown_index_weekhot_c",newsArr_c);
		});
		$("#mydown_index_article_t").mousemove(function(){
			show_class("mydown_index_article_t",newsArr_t,"current");
			show_hide("mydown_index_article_c",newsArr_c);
		});

	}

	// announce && hottag && vote
	if ($("#mydown_announce_t")[0]){
		var annArr_t = new Array("mydown_announce_t","mydown_hottag_t","mydown_vote_t");
		var annArr_c = new Array("mydown_announce_c","mydown_hottag_c","mydown_vote_c");
		$("#mydown_announce_t").mousemove(function(){
			show_class("mydown_announce_t",annArr_t,"current");
			show_hide("mydown_announce_c",annArr_c);
		});
		$("#mydown_hottag_t").mousemove(function(){
			show_class("mydown_hottag_t",annArr_t,"current");
			show_hide("mydown_hottag_c",annArr_c);
		});
		$("#mydown_vote_t").mousemove(function(){
			show_class("mydown_vote_t",annArr_t,"current");
			show_hide("mydown_vote_c",annArr_c);
		});
	}

	// top: total/week/month/day
	if ($("#mydown_top_total_t")[0]){
		var topArr_t = new Array("mydown_top_total_t","mydown_top_month_t","mydown_top_week_t","mydown_top_day_t");
		var topArr_c = new Array("mydown_top_total_c","mydown_top_month_c","mydown_top_week_c","mydown_top_day_c");
		$("#mydown_top_total_t").mousemove(function(){
			show_class("mydown_top_total_t",topArr_t,"current");
			show_hide("mydown_top_total_c",topArr_c);
		});
		$("#mydown_top_month_t").mousemove(function(){
			show_class("mydown_top_month_t",topArr_t,"current");
			show_hide("mydown_top_month_c",topArr_c);
		});
		$("#mydown_top_week_t").mousemove(function(){
			show_class("mydown_top_week_t",topArr_t,"current");
			show_hide("mydown_top_week_c",topArr_c);
		});
		$("#mydown_top_day_t").mousemove(function(){
			show_class("mydown_top_day_t",topArr_t,"current");
			show_hide("mydown_top_day_c",topArr_c);
		});
	}

	// down tag info tag list
	if ($("#mydown_down_tag_t")[0]){
		var tagArr_t = new Array("mydown_down_tag_t","mydown_info_tag_t");
		var tagArr_c = new Array("mydown_down_tag_c","mydown_info_tag_c");
		$("#mydown_down_tag_t").mousemove(function(){
			show_class("mydown_down_tag_t",tagArr_t,"current");
			show_hide("mydown_down_tag_c",tagArr_c);
		});
		$("#mydown_info_tag_t").mousemove(function(){
			show_class("mydown_info_tag_t",tagArr_t,"current");
			show_hide("mydown_info_tag_c",tagArr_c);
		});
	}

	// reload chkcode_img
	if ($("#mydown_chkcode_img")[0]){
		$("#mydown_chkcode_img").attr("title","看不清楚? 点击我刷新");
		$("#mydown_chkcode_img").click(function(){
			$("#mydown_chkcode_img").attr("src",$("#mydown_chkcode_img").attr("src")+"&ut="+timeSec);
		});
	}

	// download file
	if ($("#mydown_downfile_down")[0]){
		$("#mydown_downfile_down").click(function(){
			if ($("#reply_chkcode").val() == ""){
				alert("验证码未填写!");
				return false;
			}
		});
	}


	// share to
	if ($("#mydown_share_to")[0]){
		var share_url     = encodeURIComponent($("#mydown_share_url").attr("href"));
		var share_title   = encodeURIComponent($("#mydown_share_url").text());
		var share_img     = encodeURIComponent($("#mydown_share_img").attr("href"));
		var share_content;
		share_content  = "<a title=\"转发至QQ空间\" href=\"http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+share_url+"\" target=\"_blank\"><img src=\"/css/shareto_icons/ico_Qzone.gif\" /></a>&nbsp;";
		share_content += "<a title=\"转发至人人网\" href=\"http://share.renren.com/share/buttonshare.do?link="+share_url+"&title="+share_title+"\" target=\"_blank\"><img src=\"/css/shareto_icons/ico_renren.gif\" /></a>&nbsp;";
		share_content += "<a title=\"转发至新浪微博\" href=\"http://v.t.sina.com.cn/share/share.php?appkey=2684493555&url="+share_url+"&title="+share_title+"&sourceUrl="+weburl+"&content=utf8&pic="+share_img+"\" target=\"_blank\"><img src=\"/css/shareto_icons/ico_sina.gif\" /></a>&nbsp;";
		share_content += "<a title=\"分享到腾讯朋友\"  href=\"http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?to=pengyou&url="+share_url+"&title="+share_title+"\" target=\"_blank\"><img src=\"/css/shareto_icons/ico_pengyou.png\" /></a>&nbsp;";
		share_content += "<a title=\"分享到腾讯微博\" href=\"http://v.t.qq.com/share/share.php?title="+share_title+"&url="+share_url+"&appkey=mydownv4&site="+weburl+"\" target=\"_blank\"><img src=\"/css/shareto_icons/ico_qq_t.png\" /></a>&nbsp;";
		share_content += "<a title=\"转发至开心网\" href=\"http://www.kaixin001.com/repaste/share.php?rurl="+share_url+"&rcontent="+share_url+"&rtitle="+share_title+"\" target=\"_blank\"><img  src=\"/css/shareto_icons/ico_kaixin.gif\" /></a>&nbsp;";

		$("#mydown_share_to").html(share_content);
	}

	// show content
	if ($("#mydown_soft_content_show")[0]){
		$("#mydown_soft_content_show").click(function(){
			$("#mydown_soft_content_part").hide();
			$("#mydown_soft_content_full").show();
		});
	}

	// downfile
	if ($("#mydown_downfile_chk")[0]){
		//$.getScript(weburl+"/d/include/jquery/downfile.throwdiv.js");
		$("#mydown_downfile_chk > a").each(function(){
			$(this).bind("click",function(){
				//alert($(this).attr("href"));
				//$("#message_box").show();
				$("#mydown_downfile_form").attr("action",$(this).attr("href"));
				$("#mydown_chkcode_img").attr("src",$("#mydown_chkcode_img").attr("src")+"&ut="+timeSec);
				$("#message_box").show();
				showProc();
				return false;
			});
		});
		$(".linkbox > a").each(function(){
			$(this).bind("click",function(){
				//alert($(this).attr("href"));
				//$("#message_box").show();
				$("#mydown_downfile_form").attr("action",$(this).attr("href"));
				$("#mydown_chkcode_img").attr("src",$("#mydown_chkcode_img").attr("src")+"&ut="+timeSec);
				$("#message_box").show();
				showProc();
				return false;
			});
		});
	}

	// onlinepay
	if ($("#mydown_onlinepay_tointegral")[0]){
		var buyintegral = $("#paymoney").val()*$("#integralmoney").val();
		var addintegral = parseInt($("#paymoney").val()*$("#integralmoney").val()*$("#addintegral").val());
		$("#mydown_onlinepay_tointegral").html("充值成功后,您将获得积分:"+parseInt(buyintegral+addintegral)+(addintegral != 0 ? "&nbsp;(赠送:"+addintegral+")" : ""));
		$("#paymoney").focusout(function(){
			buyintegral = $("#paymoney").val()*$("#integralmoney").val();
			addintegral = parseInt($("#paymoney").val()*$("#integralmoney").val()*$("#addintegral").val());
			$("#mydown_onlinepay_tointegral").html("充值成功后,您将获得积分:"+parseInt(buyintegral+addintegral)+(addintegral != 0 ? "&nbsp;(赠送:"+addintegral+")" : ""));
		});
	}

});
