﻿// JavaScript Document

//图片读取后限定大小
$(document).ready( function() {
	
	/*$(".pic").each( function() {
		load_img($(this).find("img").eq(0));
	} );*/
	if ( $.browser.msie ) {
		$(".pic").each( function() {
			if ( $(this).find("img").eq(0).attr("height") < 145 ) {
				$(this).find("img").eq(0).css( "padding" , (145 - $(this).find("img").eq(0).attr("height")) / 2 + "px 0" );
			}
		} );
	}
	else {
		
	}

} );

function load_img(obj) {
	var img = new Image();
	img.src = obj.attr("rel");
	if ( img.complete ) {
		if ( img.width >= img.height ) {
			obj.attr("width",145);
			obj.attr("height",(145 / img.width * img.height));
			obj.css( "padding" , (img.width - img.height) / 2 + "px 0" );
		}
		else {
			obj.attr("height",145);
			obj.attr("width",(145 / img.height * img.width));
			obj.css( "padding" , "0" + (img.height - img.width) / 2 + "px" );
		}
		obj.attr("src",img.src);
		obj.attr("rel","load_OK");
		return;
	}
}



//首页图片展示效果
$(document).ready( function() {
	
	//浮动图片
	var floatpic_map = "";
	floatpic_map += "<div id='floatpic' style='position:absolute;z-index:150;top:0;left:0;border:1px #D3E3EA solid;background:#FFF url(/images/industry_bg.jpg) repeat-x;padding:10px;display:none;'>";
		floatpic_map += "<div style='width:358px;margin-bottom:10px;text-align:left;height:35px;line-height:35px;border-bottom:4px #DDD groove;font-size:19px;font-family:黑体;letter-spacing:1px;font-weight:bold;'></div>";
		floatpic_map += "<div style='width:358px;min-height:100px;background:url(images/ajax-loader-u.gif) no-repeat center;'><img src='' width='358' style='display:none;' /></div>";
	floatpic_map += "</div>";
	$(document.body).append(floatpic_map);
	
	var showfloating = true , floatshowing = false , lastfloatobj = "" , locateallow = false;
	var allcanshow = true , hidepic = "";
	$(".pic img:first").mouseover( function() {
		if ( allcanshow ) {
			showfloatpic($(this));
		}
	} ).mousemove( function() {
		if ( allcanshow ) {
			movefloatpic();
		}
	} ).mouseout( function() {
		if ( allcanshow ) {
			hidefloatpic($(this));
		}
	} );
	
	function checklocate(getwidth,getheight) {
		locate();
		if ( cx + 20 + getwidth > document.documentElement.clientWidth + document.documentElement.scrollLeft + document.body.scrollLeft ) {
			cx = cx + document.documentElement.scrollLeft + document.body.scrollLeft - 30 - getwidth;
		}
		if ( cy + 20 + getheight > document.documentElement.clientHeight + document.documentElement.scrollTop + document.body.scrollTop ) {
			cy = document.documentElement.clientHeight + document.documentElement.scrollTop + document.body.scrollTop - 20 - getheight;
		}
	}
	function showfloatpic(obj) {
		var src = obj.attr("rel").split(",")[0];
		var text = obj.attr("rel").split(",")[1];
		$("#floatpic div:first").text(text);
		$("#floatpic").css( { "left" : cx + 20 + "px" , "top" : cy + 20 + "px" } ).show();
		var img = new Image();
		img.src = src;
		if ( img.complete ) {
			$("#floatpic img").attr("src",src).show();
		}
		img.onload = function() {
			$("#floatpic img").attr("src",src).show();
		}
	}
	function movefloatpic() {
		checklocate(document.getElementById("floatpic").offsetWidth,document.getElementById("floatpic").offsetHeight);
		$("#floatpic").css( { "left" : cx + 20 + "px" , "top" : cy + 20 + "px" } );
	}
	function hidefloatpic(obj) {
		$("#floatpic img").attr("src","").hide();
		$("#floatpic div:first").empty();
		$("#floatpic").hide();
	}
	
	
	//非火狐可以执行图片拖动
	if ( !$.browser.mozilla ) {
		//图片拖动
		floatpic_map = "";
		floatpic_map += "<div id='downpic' style='position:absolute;z-index:151;top:0;left:0;border:1px #D3E3EA solid;background:#FFF url(/images/industry_bg.jpg) repeat-x;padding:5px;padding-bottom:0;display:none;'>";
			floatpic_map += "<div style='width:60px;border:2px #DDD solid;'>";
				floatpic_map += "<img src='' width='60' />";
			floatpic_map += "</div>";
			floatpic_map += "<div style='width:64px;height:24px;line-height:24px;font-size:14px;font-weight:bold;text-align:center;'></div>";
		floatpic_map += "</div>";
		$(document.body).append(floatpic_map);
		
		$(".pic img:first").bind( "dragstart", function() { return false; } ).mousedown( function() {
			allcanshow = false;
			locateallow = true;
			hidefloatpic($(this));
			showhiddenpic($(this));
		} );
		$("#downpic").bind( "dragstart", function() { return false; } );
		
		function showhiddenpic(obj) {
			hidepic = obj;
			$("#downpic div:first img:first").attr("src",obj.attr("src"));
			$("#downpic div:last").text(obj.attr("rel").split(",")[1]);
			locate();
			$("#downpic").css( { "left" : cx - 36 + "px" , "top" : cy - 48 + "px" } ).show().fadeTo(0,0.6);
		}
		$().mousemove( function() {
			if ( !allcanshow ) {
				locate();
				if ( cx < hidepic.offset().left || cx > ( hidepic.offset().left + hidepic.get(0).offsetWidth ) || cy < hidepic.offset().top || cy > ( hidepic.offset().top + hidepic.get(0).offsetHeight ) && locateallow ) {
					locateallow = false;
				}
				if ( cx <= $(document.documentElement).get(0).scrollLeft + 5 || cy <= $(document.documentElement).get(0).scrollTop + 5 || cx >= $("#body").get(0).clientWidth + $(document.documentElement).get(0).scrollLeft - 5 || cy >= $(document.documentElement).get(0).clientHeight + $(document.documentElement).get(0).scrollTop - 5 ) {
					$("#downpic").hide();
					hidepic = "";
					allcanshow = true;
					leaveattech = true;
					locateallow = false;
				}
				$("#downpic").css( { "left" : cx - 36 + "px" , "top" : cy - 48 + "px" } );
			}
		} ).mouseup( function() {
			if ( !allcanshow ) {
				var i = 0 , x = 0 , y = 0;
				locate();
				var checkdel = true;
				$("#irightrollin div.pic").each( function() {
					x = $(this).offset().left;
					y = $(this).offset().top;
					if ( cx >= x && cx <= ( x + 60 ) && cy >= y && cy <= ( y + 60 ) ) {
						addattech(i,hidepic,leaveattech,false);
						checkdel = false;
					}
					i++;
				} );
				if ( !leaveattech && checkdel ) {
					addattech(i,hidepic,leaveattech,true);
				}
				$("#downpic").hide();
				if ( cx >= hidepic.offset().left && cx <= ( hidepic.offset().left + hidepic.get(0).offsetWidth ) && cy >= hidepic.offset().top && cy <= ( hidepic.offset().top + hidepic.get(0).offsetHeight ) && locateallow ) {
					window.location.href = "project.asp?id=" + hidepic.attr("rel").split(",")[2];
				}
				hidepic = "";
				allcanshow = true;
				leaveattech = true;
				locateallow = false;
			}
		} );
		function addattech(i,obj,isleave,isdel) {
			var id = obj.attr("rel").split(",")[2];
			loadattech(i,id,isleave,obj.attr("rel").split(",")[3],isdel);
		}
	}
	else {
		$(".pic img:first").click( function() { window.location.href = "project.asp?id=" + $(this).attr("rel").split(",")[2]; } );
	}
	//读取写入操作
	var leaveattech = true;
	function loadattech(i,id,isleave,prei,isdel) {
		$.ajax( {
			type: "post",
			url: "/ajax/loadattech.asp",
			cache: false,
			data: "id=" + id + "&i=" + i + "&prei=" + prei + "&leave=" + isleave + "&isdel=" + isdel,
			dataType: ($.browser.msie) ? "text" : "xml",
			error: function() { },	//alert("Error loading XML document");
			success: function(result) {
				if ( typeof result == "string" ) {
					var xml = new ActiveXObject("Microsoft.XMLDOM");
					xml.async = false;
					xml.loadXML(result);
				}
				else {
					var xml = result;
				}
				var attech = xml.getElementsByTagName("root")[0].getElementsByTagName("attech");
				var read = xml.getElementsByTagName("root")[0].getAttribute("read");
				var locatid = xml.getElementsByTagName("root")[0].getAttribute("locatid");
				var ids = "";
				if ( read == "have" ) {
					alert("您已经选择了这个盘古建站。");
				}
				else if ( read == "locat" ) {
					window.location.href = "project.asp?id=" + locatid;
				}
				else if ( read == "ok" ) {
					for ( var i = 0 ; i < attech.length ; i++ ) {
						if ( attech[i].getAttribute("see") == "true" ) {
							$("#irightrollin div.pic:eq(" + i + ")").empty().append("<a href='project.asp?id=" + attech[i].getAttribute("id") + "' title='" + attech[i].getAttribute("W_id") + "'><img src='" + attech[i].getAttribute("pic") + "' width='60' rel='" + attech[i].getAttribute("pic") + "," + attech[i].getAttribute("W_id") + "," + attech[i].getAttribute("id") + "," + i + "' /></a>");
							ids += attech[i].getAttribute("id") + ",";
						}
						else {
							$("#irightrollin div.pic:eq(" + i + ")").empty();
						}
					}
					if ( !$.browser.mozilla ) {
						$("#irightrollin div.pic a").bind( "dragstart", function() { return false; } );
						$("#irightrollin div.pic img").bind( "dragstart", function() { return false; } ).mousedown( function() {
							allcanshow = false;
							leaveattech = false;
							showhiddenpic($(this));
						} );
					}
				}
				if ( ids != "" ) {
					if ( ids.split(",")[ids.split(",").length - 1] == "" ) {
						ids = ids.substring(0,ids.length - 1);
					}
					$("#irightrollin #btn a").attr("href","index.asp?list=attech&ids=" + ids);
				}
				else {
					$("#irightrollin #btn a").attr("href","index.asp");
				}
			}
		} );
	}
	loadattech("","",leaveattech,0,false);
	//点击选择对比可以快截加入对比
	function fastattech(id,del) {
		$.ajax( {
			type: "post",
			url: "/ajax/loadattech.asp",
			cache: false,
			data: "id=" + id + "&fast=ok&fastdel=" + del,
			dataType: ($.browser.msie) ? "text" : "xml",
			error: function() { alert("Error loading XML document"); },
			success: function(result) {
				if ( typeof result == "string" ) {
					var xml = new ActiveXObject("Microsoft.XMLDOM");
					xml.async = false;
					xml.loadXML(result);
				}
				else {
					var xml = result;
				}
				var attech = xml.getElementsByTagName("root")[0].getElementsByTagName("attech");
				var read = xml.getElementsByTagName("root")[0].getAttribute("read");
				var ids = "";
				if ( read == "have" ) {
					alert("您已经选择了这个盘古建站。");
				}
				else if ( read == "ok" || read == "remove" ) {
					for ( var i = 0 ; i < attech.length ; i++ ) {
						if ( attech[i].getAttribute("see") == "true" ) {
							$("#irightrollin div.pic:eq(" + i + ")").empty().append("<a href='project.asp?id=" + attech[i].getAttribute("id") + "' title='" + attech[i].getAttribute("W_id") + "'><img src='" + attech[i].getAttribute("pic") + "' width='60' rel='" + attech[i].getAttribute("pic") + "," + attech[i].getAttribute("W_id") + "," + attech[i].getAttribute("id") + "," + i + "' /></a>");
							ids += attech[i].getAttribute("id") + ",";
						}
						else {
							$("#irightrollin div.pic:eq(" + i + ")").empty();
						}
					}
					if ( !$.browser.mozilla ) {
						$("#irightrollin div.pic a").bind( "dragstart", function() { return false; } );
						$("#irightrollin div.pic img").bind( "dragstart", function() { return false; } ).mousedown( function() {
							allcanshow = false;
							leaveattech = false;
							showhiddenpic($(this));
						} );
					}
				}
				if ( ids != "" ) {
					if ( ids.split(",")[ids.split(",").length - 1] == "" ) {
						ids = ids.substring(0,ids.length - 1);
					}
					$("#irightrollin #btn a").attr("href","index.asp?list=attech&ids=" + ids);
					if ( read == "remove" ) {
						window.location.href = "index.asp?list=attech&ids=" + ids;
					}
				}
				else {
					$("#irightrollin #btn a").attr("href","index.asp");
					if ( read == "remove" ) {
						window.location.href = "index.asp";
					}
				}
			}
		} );
	}
	$(".show a:last").click( function() {
		fastattech($(this).attr("rel"),false);
	} );
	$(".showdel a:last").click( function() {
		fastattech($(this).attr("rel"),true);
	} );
} );