function set_default_text(objarray){
	for(counter=0;counter<objarray.length;counter++){
		obj=objarray[counter];
		if(obj.attr("defval")==undefined){
			obj.attr("defval",obj.val());
		}
		if(jQuery.trim(obj.val())==""){
			obj.val(obj.attr("defval"));
		}
		obj.attr("defaultclass",obj.attr("className"));

		obj.focus(
			function(){
				if($(this).val()==$(this).attr("defval")){
					$(this).val("");
				}
				focusclass=jQuery.trim($(this).attr("defaultclass").replace("lightgray",""));
				$(this).attr("className",focusclass);
				//$(this).removeClass("lightergray");
			}
		);
		obj.blur(
			function(){
				if($(this).val()==""){
					$(this).val($(this).attr("defval"));
					$(this).attr("className",$(this).attr("defaultclass"));
				}
			}
		);

	}
}



function vote(id,type){
	jQuery.post(
		"?"+Math.random(),
		{
			"aaction"	:"vote",
			"id"		:id,
			"type"		:type
		},
		function(data,status){
			if(status=="success"){
				response=eval(data)[0];
				if(response.result){
					window.location.reload();
				}
			}
		}
	);

}




function AddFavorite(sURL, sTitle){
    try{
        window.external.addFavorite(sURL, sTitle);
        return;
    }catch (e){}

    try{
        window.sidebar.addPanel(sTitle, sURL, "");
        return;
    }catch (e){
        alert("Please use Ctrl+D to bookmark page.");
    }
}



var map = null;
var geocoder = null;
var mapoptions=null;

function google_map_initialize(mapobj,address){

	var geocoder = new google.maps.Geocoder();
	geocoder.geocode(
		{
			'address'	: jQuery.trim(address).replace(" ","+","g"),
			"language"	: "en"
		},
		function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				geolocation=results[0].geometry.location
				test=geolocation;
				loc=new google.maps.LatLng(geolocation.lat(), geolocation.lng());
//				map.setCenter(loc);

			    mapoptions = {
		   	      "zoom": 14,
		   	      "center": loc,
		   	      "mapTypeId": google.maps.MapTypeId.ROADMAP
		   	    };
		   	    map = new google.maps.Map(mapobj, mapoptions);

				new google.maps.Marker({
				      'position': loc,
				      'map': map
//				      'title': request.latLng.toString(),
//				      'clickable': false,
//				      'icon': clickIcon,
//				      'shadow': shadow
				});
			} else {}
		}
	);

}

function report_abuse(id,type,obj){
	jQuery.post(
		"?"+Math.random(),
		{
			"aaction"	:"report_abuse",
			"id"		:id,
			"type"		:type
		},
		function(data,status){
			if(status=="success"){
				response=eval(data)[0];
				if(response.result){
					container=$(obj).parents(".browseFuncBox").parent();
					container.find(".browseFuncBox").css("display","none");
					if(container.find("[name=report_abuse_success]").length>0){
						container.find("[name=report_abuse_success]").css("display","");
					}else{
						$(response.html).insertBefore(container.find("*[name=abuse_report]"));
					}
				}
			}
		}
	);
}

var ttt=null;
$(document).ready(
		function(){

				set_default_text($("*[defval]").map(
						function(){
							return $(this);
						}
				).get());

				// general search box
				$("*[name=btn_general_search]").click(
						function(){
							get_search_url(this);
							return false;
						}
				);

				$("*[name=txt_general_search]").keypress(
						function(e){
							if(e.keyCode==13){
								btn=$(this).parents(".searchBox").find("*[name=btn_general_search]");
								get_search_url(btn);
							}
						}
				);


				// topbar
				if($("*[name=fnc_dw_trending]").length>=4){
					$("*[name=fnc_dw_trending][picopen]").css("display","");
				}

		}

);


function get_search_url(search_button){
	search_area=$(search_button).parents(".searchBox");
	search_text=search_area.find("input[type=text]").val();
	if(jQuery.trim(search_text)==""){
		alert("please input your keyword");
		return false;
	}
	$(search_button).unbind();
	jQuery.post(
			"?"+Math.random(),
			{
				"aaction":"submit_search",
				"keyword":jQuery.trim(search_text)
			},
			function(data,status){
				if(status=="success"){
					response=eval(data)[0];
					if(response.result){
						window.open(response.url,'_self');
					}else{
						$(search_button).click(
								function(){
									get_search_url(this);
									return false;
								}
						);
					}
				}
			}
	);
}

if ($('#datepicker').length>0){
	$('#datepicker').datepicker({maxDate: '-1D'});
	$("*[name=datepicker]").click(
		function() {
			$("#datepicker").css("display","").focus().css("display","none");
		}
	);
}




var topic_search_timer=null;
function get_topic_dropdown(){
	if($(".headerInfo .listDiv .listBox").length>0){
		$(".headerInfo .listDiv .listBox").toggle();

		uid=$(".headerInfo .listDiv .listBox").attr("id")
		if(quickpopup[uid]!=undefined){
			quickpopup[uid]=false;
		}
		return false;
	}
	jQuery.post(
		"?"+Math.random(),
		{
			"aaction"	:"get_topic_dropdown_menu"
		},
		function(data,status){
			if(status=="success"){
				response=eval(data)[0];
				$(".headerInfo .listDiv").append($(response.html));
				$(".headerInfo .listDiv").css("display","");
				newid=get_uid();
				$(".headerInfo .listDiv .listBox").attr("id",newid);
				quickpopup[newid]=true;

				// set background text
				inputele=$(".headerInfo .listDiv .listBox input[type=text]");
				set_value_background([inputele]);

				// add keypress event
				$(".headerInfo .listDiv .listBox input[type=image]").click(
						function(){
							inputele=$(".headerInfo .listDiv .listBox input[type=text]");
							if(jQuery.trim(inputele.val())!=""){
								topic_keyword_search();
							}
						}
				);


				// submit query when pressing keyword
//				inputele=$(".headerInfo .listDiv .listBox input[type=text]");
//
//				inputele.keypress(
//						function(){
//							keyword=jQuery.trim($(this).val());
//							if(topic_search_timer && keyword!=""){
//								clearInterval(topic_search_timer);
//								topic_search_timer=null;
//							}
//
//
//							topic_search_timer=setTimeout(
//										topic_keyword_search,
//										2000
//							);
//						}
//				);

			}
		}
	);
}

function topic_keyword_search(){
	jQuery.post(
			"?"+Math.random(),
			{
				"aaction"	:"get_topic_by_captical",
				"keyword"	:$(".headerInfo .listDiv .listBox input[type=text]").val()
			},
			function(data, status){
				if(status=="success"){
					response=eval(data)[0];
					insertpoint=$(".headerInfo .listDiv .listBox").find("*[name=keyword_search_list]");
					$(response.html).insertAfter(insertpoint);
					insertpoint.remove();
				}
			}
	);
}

function get_uid(){
	return "i"+Math.round(Math.random()*10000);
}

var quickpopup={};
$(document.body).click(
	function(event){
		for(objid in quickpopup){
			if(
					quickpopup[objid]
					&& $(event.target).attr("id")!=undefined
					&& $(event.target).attr("id")!=objid
					&& $(event.target).parents("[id="+objid+"]").length<1
					&& $("#"+objid).css("display")!="none"
			){
				$("#"+objid).css("display","none");
			}
			quickpopup[objid]=true;
		}
	}
);


function set_value_background(objarray){
	for(counter=0;counter<objarray.length;counter++){
		iobj=objarray[counter];
		if(iobj.attr("defval")==undefined){
			iobj.attr("defval",iobj.val());
		}
		if(jQuery.trim(iobj.val())==""){
			iobj.val(iobj.attr("defval"));
		}
		iobj.attr("defaultclass",iobj.attr("className"));

		iobj.focus(
			function(){
				if($(this).val()==$(this).attr("defval")){
					$(this).val("");
				}
				focusclass=jQuery.trim($(this).attr("defaultclass").replace("lightergray",""));
				$(this).attr("className",focusclass);
				//$(this).removeClass("lightergray");
			}
		);
		iobj.blur(
			function(){
				if($(this).val()==""){
					$(this).val($(this).attr("defval"));
					$(this).attr("className",$(this).attr("defaultclass"));
					//$(this).removeClass("lightergray");
					//$(this).addClass("lightergray");
				}
			}
		);

	}
}
