// JavaScript Document

$(document).ready(function(){
	writeIdea($(".write-idea a.submit_form"),$(".write-idea form"));
	loginPanelShow();
	feedbackPanleShow();
	uploadPanelShow();
	tagSelectDrop();
	currentMenu();
	limit();
	changeImg();
	floatDiv($("#help_menu"),2);
	openTip();
	commentFloor();
	voteNum();
	imgSize($(".advance-link").find("img"),690,430);
	//submenu($("#menu-news"));
});

$(".write-idea").ready(function(){
	$(".write-idea").find(".fillarea").areaval();
	$(".write-idea").find(".fillarea").keydown(function(){  
		if($(this).val().length>140){
			var abc=$(this).val().substring(0,140);
			$(this).val(abc);
		}
    }); 
});

$("#comment").ready(function(){
	$(".comment-post").find(".fillinput").inputval();
});

//评论楼层
function commentFloor(){
	var n = $(".list-commentitem li");
	n.each(function(i){
		var j=i+1;
		$(this).find(".floor").text(j+"楼");
	})
}

//主题设计投票编号
function voteNum(){
	var n = $(".topic_vote_list .list-bigthumb3 li");
	n.each(function(i){
		var j=i+1;
		if(j<10){
			$(this).find(".spec_sign").text("#0"+j);
		}else{
			$(this).find(".spec_sign").text("#"+j);
		}
	})
}

//点击展开更多
function clickOpenObj(){
	var obj = $("[openObj]");
	obj.each(function(){
		var par = $(this).attr("openObj");
		$(this).click(function(){
			$("#"+par).toggle();
		});
	});
}

//鼠标经过展开提示
function openTip(){
	var obj = $("[openTip]");
	obj.each(function(){
		var par = $(this).attr("openTip");
		$(this).mouseover(function(){
			$("#"+par).show();
			//$(par).slideDown("fast");
		});
		$(this).mouseout(function(){
			$("#"+par).hide();
			//$(par).slideDown("fast");
		});
	});
}

//验证上传草图表单
function checkUpload(){
	if($(".upload-list .cover").length == 0){
		$(".checkmsg").hide();
		$("#checkmsg_sketch").show();
		$("#checkmsg_sketch").html("<img src='./images/icon_tips_error.gif' />请上传至少1张草图");
		$("h1").ScrollTo(200);
		return false;
	}
	else if($(".tag-type li:nth-child(1) em").attr("class")=="default_icon"){
		$(".checkmsg").hide();
		$("#checkmsg_tag").show();
		$("#checkmsg_tag").html("<img src='./images/icon_tips_error.gif' />必须选择一种类型");
		$(".upload_lnk").ScrollTo(200);
		return false;
	}
	else if($(".tag-mat li:nth-child(1)").attr("class")=="default_icon"){
		$(".checkmsg").hide();
		$("#checkmsg_tag").show();
		$("#checkmsg_tag").html("<img src='./images/icon_tips_error.gif' />必须选择1-3种材料");
		$(".upload_lnk").ScrollTo(200);
		return false;
	}
	else if(!$("#plan_name").val() || $("#plan_name").val().match(/([w]){2,25}$/)) {
		$(".checkmsg").hide();
		$("#checkmsg_name").show();
		$("#checkmsg_name").html("<img src='./images/icon_tips_error.gif' />设计名称不能为空或大于25个字");
		$(".upload_lnk").ScrollTo(200);
		return false;
	}
	else if(!$("#plan_memo").val() || $("#plan_memo").val().match(/([w]){10,500}$/)) {
		$(".checkmsg").hide();
		$("#checkmsg_memo").show();
		$("#checkmsg_memo").html("<img src='./images/icon_tips_error.gif' />设计说明不能小于10个字或大于500个字");
		$(".upload_lnk").ScrollTo(200);
		return false;
	}
	else {
		return true;
	}
}

//页面内平滑移动
jQuery.getPos = function (e){
 var l = 0;
 var t = 0;
 var w = jQuery.intval(jQuery.css(e,'width'));
 var h = jQuery.intval(jQuery.css(e,'height'));
 var wb = e.offsetWidth;
 var hb = e.offsetHeight;
 while (e.offsetParent){
 l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
 t += e.offsetTop + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
 e = e.offsetParent;
 }
 l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
 t += e.offsetTop + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
 return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e){
 if (e) {
 w = e.clientWidth;
 h = e.clientHeight;
 } else {
 w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document
.body.offsetWidth;
 h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight :
document.body.offsetHeight;
 }
 return {w:w,h:h};
};
jQuery.getScroll = function (e){
 if (e) {
 t = e.scrollTop;
 l = e.scrollLeft;
 w = e.scrollWidth;
 h = e.scrollHeight;
 } else {
 if (document.documentElement && document.documentElement.scrollTop) {
 t = document.documentElement.scrollTop;
 l = document.documentElement.scrollLeft;
 w = document.documentElement.scrollWidth;
 h = document.documentElement.scrollHeight;
 } else if (document.body) {
 t = document.body.scrollTop;
 l = document.body.scrollLeft;
 w = document.body.scrollWidth;
 h = document.body.scrollHeight;
 }
 }
 return { t: t, l: l, w: w, h: h };
};
jQuery.intval = function (v){
 v = parseInt(v);
 return isNaN(v) ? 0 : v;
};
jQuery.fn.ScrollTo = function(s) {
 o = jQuery.speed(s);
 return this.each(function(){
 new jQuery.fx.ScrollTo(this, o);
 });
};
jQuery.fx.ScrollTo = function (e, o)
{
 var z = this;
 z.o = o;
 z.e = e;
 z.p = jQuery.getPos(e);
 z.s = jQuery.getScroll();
 z.clear = function(){clearInterval(z.timer);z.timer=null};
 z.t=(new Date).getTime();
 z.step = function(){
 var t = (new Date).getTime();
 var p = (t - z.t) / z.o.duration;
 if (t >= z.o.duration+z.t) {
 z.clear();
 setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
 } else {
 st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
 sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
 z.scroll(st, sl);
 }
 };
 z.scroll = function (t, l){window.scrollTo(l, t)};
 z.timer=setInterval(function(){z.step();},13);
};

//显示关闭内容
function visibText(){
	$(".visib_obj").each(function(){
		$(this).hide();
	});
	$(".visib_btn").click(function(){
		var obj = $(this).attr("visib");
		if($("#"+obj).css("display")=="none"){
			$("#"+obj).show();
		}else{
			$("#"+obj).hide();
		}
	})
	$(".visib_obj").find(".close2").click(function(){
		$(this).parents(".visib_obj").hide();
	})
}

//定时跳转页面
function jump(count) {
	var url = $("#target_page").attr("href");
	window.setTimeout(function(){   
		count--;
		if(count > 0) {   
			$('#jumpNum').attr('innerHTML', count);
			jump(count);   
		} else {   
			location.href=url;
		}   
	}, 1000);   
}

//二级菜单
function submenu(menu){
	var position = menu.position();
	var height = menu.height()-3;
	menu.mouseover(function(){
		$(".submenu").css({left:position.left,top:position.top+height});
		$(".submenu").show();
	});
	menu.mouseout(function(){
		$(".submenu").hide();
	});
	$(".submenu").mouseover(function(){
		menu.find("a").addClass("cur");
		$(".submenu").show();
	});
	$(".submenu").mouseout(function(){
		menu.find("a").removeClass("cur");
		$(".submenu").hide();
	});
}

//截取字数
function limit(){
    var self = $("[limit]");
    self.each(function(){
        var objString = $(this).text();
        var objLength = $(this).text().length;
        var num = $(this).attr("limit");
		var sym = $(this).attr("symbol");
        if(objLength > num){
            $(this).attr("title",objString);
            objString = $(this).html(objString.substring(0,num)+sym);
        }
    })
}

//导航
function currentMenu(){
	switch (cur_page) {
	   case "index" :
		   $("#menu-home a").addClass("cur");
		   break
	   case "design" :
		   $("#menu-design a").addClass("cur");
		   break
	   case "idea" :
		   $("#menu-idea a").addClass("cur");
		   break
	   case "news" :
		   $("#menu-news a").addClass("cur");
		   break
	   case "space" :
		   $("#menu-space a").addClass("cur");
		   break
	   default :
		   break
	}
}

//选择标签Drop-down-list
function tagSelectDrop(){
	$(".tag-type .drop_btn").click(function(){
		$("#tag_mat_all").hide(); 
		$("#tag_type_all").slideDown("slow"); 
	});
	$(".tag-mat .drop_btn").click(function(){
		$("#tag_type_all").hide(); 
		$("#tag_mat_all").slideDown("slow"); 
	});
	$("#tag_type_all .close").click(function(){
		$("#tag_type_all").slideUp("slow"); 
	});
	$("#tag_mat_all .close").click(function(){
		$("#tag_mat_all").slideUp("slow"); 
	});	
	$("#tag_type_all a.tag_icon").click(function(){
		$(this).focus().blur();
		$("#tag_type_all").hide();
		var click_icon = $(this).find("em").clone();
		$(".tag-type em:nth-child(1)").replaceWith(click_icon);
		var a_tid = $(this).attr("tid");
		$(".tag-type label").each(function(i){
			var input_tid = $(".tag-type label").eq(i).find("input").attr("tid");
			if(input_tid==a_tid){
				$(".tag-type label").eq(i).find("input").attr({checked:"checked"});
			}
		});
	});
	$("#tag_mat_all a.tag_icon").click(function(){
		$(this).focus().blur();
		if($(this).attr("class")=="tag_icon"){
			$(this).addClass("cur");
		} else {
			$(this).removeClass("cur");
		}
	});
	$("#tag_mat_all a.confirm").click(function(){
		$("#tag_mat_all").hide();
		var num=$("#tag_mat_all a.cur").length;
		if(num>0){
			$(".tag-mat li[title='tag']").remove();
			for (var i=1;i<=num;i++){
				$(".tag-mat ul").prepend("<li title='tag'><a href='#' class='tag_icon' title='{$plan.plan_type_id}'></a></li>");
			}
			for (var i=0;i<=num;i++){
				var click_icon = $("#tag_mat_all a.cur").eq(i).find("em").clone();
				$(".tag-mat li[title='tag']").eq(i).find("a").append(click_icon);
			};
			$(".tag-mat label").each(function(i){
				$(".tag-mat label").eq(i).find("input").removeAttr("checked");
			})
			$("#tag_mat_all a").each(function(j){
				var a_tid = $("#tag_mat_all a").eq(j).attr("tid");
				var a_class = $("#tag_mat_all a").eq(j).attr("class");
				$(".tag-mat label").each(function(i){
					var input_tid = $(".tag-mat label").eq(i).find("input").attr("tid");
					if(input_tid==a_tid && a_class=="tag_icon cur"){
						$(".tag-mat label").eq(i).find("input").attr({checked:"checked"});
					}
				});
			});	
		} else {
			$(".tag-mat li[title='tag']").remove();
			$(".tag-mat ul").prepend("<li title='tag' class='default_icon'><a href='javascript:void(0);' class='tag_icon'title='{$plan.plan_type_name}'><em style='background-position:-480px -80px'></em></a></li>");
		}
	});
}

//表单文字消失
$.fn.areaval=function(className){ //className （当前状态的类名） 是唯一的参数 而且可选
	className= className || ""; //是否有className 参数
	temtext=$(this).val(); //用 temval 记载它起始的值 不使用 var 开头
	$(this).focus(function(){ //获得焦点时
		if($(this).val()==temtext){
			$(this).val(""); //清空输入框
			$(this).removeClass(className);
		}
	})
	$(this).blur(function(){
		if($(this).val()==""){
			$(this).val(temtext);
			$(this).addClass(className);
		}
	})
}
$.fn.inputval=function(className){
	className= className || "";
	temval=$(this).val();
	$(this).focus(function(){
		if($(this).val()==temval){
			$(this).val("");
			$(this).removeClass(className);
		}
	})
	$(this).blur(function(){
		if($(this).val()==""){
			$(this).val(temval);
			$(this).addClass(className);
		}
	})
}

//浮动对话框
function floatDialog(dialog_obj){
	$("#darkBg").css({width:$("body").width(),height:$("body").height()});
	$("#darkBg").fadeIn("fast"); 
	$("#"+dialog_obj).fadeIn("fast");
	$("#"+dialog_obj).css({left:($(window).width()-$("#"+dialog_obj).width())/2,top:($(window).height()-$("#"+dialog_obj).height())/2+$(document).scrollTop()});
	floatDiv($("#"+dialog_obj),0);
	$("#"+dialog_obj+" a.close").click(function(){
		$("#"+dialog_obj).fadeOut("fast");
		$("#darkBg").fadeOut("fast");
	});
	$("#"+dialog_obj+" a.cancel").click(function(){
		$("#"+dialog_obj).fadeOut("fast");
		$("#darkBg").fadeOut("fast");
	});
	$("#darkBg").click(function(){
		$("#"+dialog_obj).fadeOut("fast");
		$("#darkBg").fadeOut("fast");
	});
}

//登录面板
function loginPanelShow(){
	$("a.login_lnk").click(function(){
		$("#popupLogin").fadeIn("fast");
		$("#popupLogin").css({left:($(document).width()-$("#popupLogin").width())/2,top:150+$(document).scrollTop()});
	});
	$("#popupLogin a.close").click(function(){
		$("#popupLogin").fadeOut("fast");
	});
	$("#popupLogin a.cancel").click(function(){
		$("#popupLogin").fadeOut("fast");
	});
}


//意见反馈面板
function feedbackPanleShow(){ 
	$("#feedback_float").css({left:0,top:($(window).height()-$("#feedback_float").height())/2+$(document).scrollTop()});
	floatDiv($("#feedback_float"),0);
	//floatDiv($("#feedbackPanel"),0);
	$("#feedback_float a").click(function(){
		$("#feedbackPanel").fadeIn("fast");
		$("#feedbackPanel").css({left:($(document).width()-$("#feedbackPanel").width())/2,top:($(window).height()-$("#feedbackPanel").height())/2+$(document).scrollTop()});
	});
	$("a.feedback_btn").click(function(){
		$("#feedbackPanel").fadeIn("fast");
		$("#feedbackPanel").css({left:($(document).width()-$("#feedbackPanel").width())/2,top:($(window).height()-$("#feedbackPanel").height())/2+$(document).scrollTop()});
	});
	$("#feedbackPanel a.close").click(function(){
		$("#feedbackPanel").fadeOut("fast");
	});
	$("#feedbackPanel a.cancel").click(function(){
		$("#feedbackPanel").fadeOut("fast");
	});
}

//浮动层
function floatDiv(obj,type){
	var position = obj.position();
	var header_height = $("#header").height()+20;
	$(window).scroll(function() {    // 页面发生scroll事件时触发  
		var bodyTop = 0;  
		if (typeof window.pageYOffset != 'undefined') {  
			bodyTop = window.pageYOffset;  
		} else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {  
			bodyTop = document.documentElement.scrollTop;  
		}  
		else if (typeof document.body != 'undefined') {  
			bodyTop = document.body.scrollTop;  
		}  
		switch (type){
			case 2:
				if (bodyTop>=header_height){
					obj.css("top", bodyTop);   // 设置层的CSS样式中的top属性, 注意要是小写，要符合“标准”
				}
				break;
			default:
				obj.css("top", ($(window).height()-$(obj).height())/2+bodyTop);   // 设置层的CSS样式中的top属性, 注意要是小写，要符合“标准”
		}
	});
}

//写点子
function writeIdea(btn,element){
	btn.click(function(){
		element.submit();
	});	
}

//上传面板
function uploadPanelShow(){
	if(document.getElementById("uploadPanel")){
		var position = $("a.upload_lnk").position();
		$("#uploadPanel").css({left:position.left,top:position.top});
		$("#uploadPanel a.submit").click(function(){
			$("#uploadPanel form").submit();
		});
	}
}

//回车键提交
function keyEnterSubmit(element,key_code){
	element.keydown(function(event){
		if(event.keyCode==key_code) {
			element.submit();
		}
    });	
}

//图片尺寸控制
function imgSize(self,maxW,maxH){
	var w = self.width();
	var h = self.height();
	if(w >= h && w > maxW){
		self.attr("width",maxW);
		var new_h = h*maxW/w;
		self.attr("height",new_h);
	} else if (w < h && h > maxH){
		self.attr("height",maxH);
		var new_w = w*maxH/h;
		self.attr("width",new_w);
	} else if (w >= h && h > maxH){
		self.attr("height",maxH);
		var new_w = w*maxH/h;
		self.attr("width",new_w);
	} else if (w < h && w > maxW){
		self.attr("width",maxW);
		var new_h = h*maxW/w;
		self.attr("height",new_h);
	}
}

//设计详情大图最大宽度控制
function imgMaxWidth(element,size){
	element.each(function(){
		var width = $(this).width();
		//alert(width);
		if(width>size){
			$(this).attr("width",size);
		}
	});
}

//设计详情大图浏览
jQuery.fn.loadthumb = function(options) {
	options = $.extend({
		 src : ""
	},options);
	var _self = this;
	//_self.hide();
	var img = new Image();
	$(img).load(function(){
		_self.attr("src", options.src);
		//_self.show();
		//_self.fadeIn("slow");
	}).attr("src", options.src);  //.atte("src",options.src)要放在load后面，
	return _self;
}

function changeImg(){
	var i = 5;  //已知显示的<a>元素的个数
	var m = 5;  //用于计算的变量
	var count = $(".img-menu").find("a").length;
	$(".img-menu").each(function(i){
		$(this).find("a").click(function(){
			var img_src = $(this).find("img").attr("imgb");
			var obj = $(this).find("img").attr("obj");
			var maxW = $(this).attr("maxW");
			var maxH = $(this).attr("maxH");
			$("#"+obj).attr("href",img_src);
			$("#"+obj).find("img").remove();
			$("#"+obj).append('<img src="'+img_src+'" class="img-one" />');
			$(this).addClass("cur").siblings().removeClass("cur");
			
			var self = $("#"+obj).find("img");
			var _img = new Array();
			_img[i] = new Image();
			_img[i].src = self.attr("src");
			$(_img[i]).load(function(){
				self.ready(function(){
					var w = self.width();
					var h = self.height();
					if(w >= h && w > maxW){
						self.attr("width",maxW);
						var new_h = h*maxW/w;
						self.attr("height",new_h);
	//					if(new_h > maxH){
	//						self.attr("height",maxH);
	//						var new_w = maxW*maxH/h;
	//						self.attr("width",new_w);
	//					}
					} else if (w < h && h > maxH){
						self.attr("height",maxH);
						var new_w = w*maxH/h;
						self.attr("width",new_w);
	//					if(new_w > maxW){
	//						self.attr("width",maxW);
	//						var new_h = h*maxW/w;
	//						self.attr("height",new_h);
	//					}
					}
				});
			});
		});
		$(this).find("a:nth-child(1)").click();
	});
}