$(document).ready(function(){
    $(".date").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: "d-M-yy",
        minDate: -0
    });
// post question
    /*
	$('#question_content').change(function(){
		get_title(this.value);
	});
	
	$('#question_content').change(function(){
		get_title(this.value);
	});
	*/
	$('#post_submit').click(function(){
		/*get_title($('#question_content').value);*/
		if(!check_post_question()){
			return false;
		}
	});
/*	
function get_title(value){
	$('#question_title').val(value.substr(0,128));
}*/

function check_post_question(){
	$("#article_control").html("");
	$email = $("#post_user_email").val();
	$email_dot_index = $email.lastIndexOf(".");
	$email_at_index = $email.indexOf("@");
	var content = $('#question_content').val();
	var title = $("#question_title").val();
	var check_number = $("#check_number").val();
	if($("#post_user_name").val()==""){
		$("#article_control").html("<span class='red'>Please enter your name.</span>");
		return false;
	}
	if($email==""||$email_at_index==0 || $email_at_index==$email.length-1 ||
	$email_at_index==-1 ||$email_dot_index<$email_at_index||$email_dot_index==$email.length-1){
		$("#article_control").html("<span class='red'>Please check your email.</span>");
		return false;
	}
	if(content==""||content == "<br>" ||content=="<p><br></p>"){
		$("#article_control").html("<span class='red'>Please enter content.</span>");
		return false;
	}
	if(title.length>128 || title.length<=0){
		$("#article_control").html("<span class='red'>Please check title.</span>");
		return false;
	}
	if(content.length>7000){
		$("#article_control").html("<span class='red'>Your Content length out of range.</span>");
		return false;
	}
	if(check_number.length!=6 || check_number==""){
		$("#article_control").html("<span class='red'>Please check your code.</span>");
		return false;
	}
	return true;
}



//change city weather
$("#weather").change(function(e){
	if(this.value=='input'){
		set_input_weather();
	}else{
		get_weather(this.value);
	}
});
$("#weather_search_button").click(function(e){
	get_weather($("#weather_city_name").val());
});
$("#weather_search_cancel").click(function(e){
	$("#weather_search_cancel").hide();
	$("#weather_search_button").hide();
	$("#weather_city_name").hide();
	$('#weather_show_text').text('Choose a city:');
	$("#weather").show();
});
function set_input_weather(){
	$("#weather_search_cancel").show();
	$("#weather_search_button").show();
	$("#weather_city_name").show();
	$('#weather_show_text').text('Input city name:');
	$("#weather").hide();
}	
// get weather
function get_weather(city){
	$.get(
		"get_weather.php",
		{
			"weather":city
		},
		function(data){
			//var host = "http://www.google.co.uk";
			var host = "img";
			var $weather = $(data);
			var current = {
				condition : $weather.find("current_conditions condition").attr("data"),
				temp_f : $weather.find("current_conditions temp_f").attr("data"),
				temp_c : $weather.find("current_conditions temp_c").attr("data"),
				humidity : $weather.find("current_conditions humidity").attr("data"),
				icon : $weather.find("current_conditions icon").attr("data").replace("gif","png"),
				wind_condition : $weather.find("current_conditions wind_condition").attr("data")
			};
			current.icon = current.icon.substr(current.icon.lastIndexOf("/"));
			$("#weather_img").attr("src",host+current.icon);
			$("#weather_img").attr("alt",current.condition);
			$("#weather_img").attr("title",current.condition);
			$("#temperature").html(current.temp_c+"&deg;C");
			$("#weather_desc").html(
				"Current: "+current.condition+ "<br/>"+
				 current.wind_condition+ "<br/>"+
				 current.humidity
			);
			delete current;
			$(".weather_list").remove();
			var weather_list = $weather.find("forecast_conditions");
			var html = '<div class="clear"></div>';
			var count = weather_list.length;
			$.each(weather_list,function(index,value){
				var $forecast = $(this);
				var temp = {
					day_of_week : $forecast.find("day_of_week").attr("data"),
					low : parseInt(5/9*(parseInt($forecast.find("low").attr("data"))-32)),
					high : parseInt(5/9*(parseInt($forecast.find("high").attr("data"))-32)),
					icon : $forecast.find("icon").attr("data").replace("gif","png"),
					condition : $forecast.find("condition").attr("data")
				};
				temp.icon = temp.icon.substr(temp.icon.lastIndexOf("/"));
				html += '<div class="weather_date">';
				html += temp.day_of_week;
				html += '</div>';
	            html += '<div class="weather_img">';
	            html += '<img id="weather_img" src="'+host+temp.icon+'" alt="'+temp.condition+'" title="'+temp.condition+'" width="40px" height="40px" />';
	            html += '</div>';
	            html += '<div class="weather_temperature">';
				html += temp.low+"&deg;~"+temp.high+"&deg;";
				html += '</div>';
				html += '<div class="weather_desc">';
	            html += temp.condition;
	            html += '</div>';
	            if(index+1!=count)
	            	html += '<hr width="95%" style="clear:left;margin-left:2%;" color="#ddd" />';
			});
			$("#weather_list").html(html);
		},
		"xml"
	);
}
// get weather
get_weather("beijing");
});
