var log = function(){
	var w = window;
	return {
		add: function(m) { w.console && w.console.log(m) },
		js: function() { w.console && w.console.profile() },  
		jsEnd: function() { w.console && w.console.profileEnd() },
		prof: function(code) {
			if ( w.console ) {
				console.profile();
				try { code() } catch(err) { };
				console.profileEnd();
			}
		}
	}
}();

var clsearch = function(){
	var url = "http://pipes.yahoo.com/pipes/pipe.run";

	var __constructor = function(){
		var imgs = [
					"http://clsearch.org/clsearch/img/ico-loading.gif"
				],
				imgObs = [];

		for ( var i=0, l=imgs.length; i<l; i++ ) {
			imgObs.push(new Image());
			imgObs[i].src = imgs[i];
		}
	}();

	var loadJobs = function(d){
		$("#jobs .count").text(d.count+" results found.").show();
		$("#jobs .results").html( buildHtml(d) );
	};

	var loadForSale = function(d){
		$("#forsale .count").text(d.count+" results found.").show();
		$("#forsale .results").html( buildHtml(d) );
	};

	var loadHousing = function(d){
		$("#housing .count").text(d.count+" results found.").show();
		$("#housing .results").html( buildHtml(d) );
	};

	var loadResumes = function(d){
		$("#resumes .count").text(d.count+" results found.").show();
		$("#resumes .results").html( buildHtml(d) );
	};


	var buildHtml = function(d){
		log.add(d);
		var r = d.value.items,
				l = r.length,
				htm = [];

		for ( var i=0; i<l; i++ ) {
			htm.push( '<li>' );
			htm.push( '<h2 class="title">' );
			htm.push( r[i].title );
			htm.push( ' <a class="link" href="'+r[i].link+'" target="_new">⬀</a>' );
			htm.push( ' <a class="more" href="#">more</a>' );
			htm.push( '</h2>' );
			htm.push( '<div class="description">'+r[i].description+'</div>' );
			htm.push( '</li>' );
		}

		return htm.join("");
	};

	var loading = function(form){
		var htm = [],
				list = $(form).parent().find(".results");

		htm.push( '<li class="loading">' );
		htm.push( '<img src="img/ico-loading.gif" alt="Loading" />' );
		htm.push( '</li>' );

		list.html( htm.join("") );
		list.prev().text("");
	};

	return {
		init: function(){
			String.prototype.strip = function(){
				return this.replace(/<\/?[^>]*>/g, "");
			};

			$("nav a").click(function(e){
				var li = $(this).parent(),
						sibs = li.siblings(),
						id = "#"+this.href.split("#")[1];

				e.preventDefault();	
				sibs.removeClass("active");
				li.addClass("active");

				$(".viewport").animate( { scrollLeft: $(id).position().left }, 200);
				$(id).show();
			});

			$(".search").submit(function(e){
				var type = this["type"].value,
						csvkey = $("#region input:checked").val();

				var qstr = {
						_id: "3f779188bad3c3b33c780c7c0a63bfbf",
						_render: "json",
						csvkey: csvkey,
						search: this["keyword"].value
				};

				e.preventDefault();

				if ( type == 'jobs' ) {
					qstr.type = "jjj";
					qstr.addOne = this["addOne"].checked ? this["addOne"].value : "";
					loading(this);
					$.getJSON(url, qstr, loadJobs);
				} else if ( type == 'forsale' ) {
					qstr.type = "sss";
					qstr.minAsk = this["minAsk"].value;
					qstr.maxAsk = this["maxAsk"].value;
					loading(this);
					$.getJSON(url, qstr, loadForSale);
				} else if ( type == 'housing' ) {
					qstr.type = "hhh";
					qstr.minAsk = this["minAsk"].value;
					qstr.maxAsk = this["maxAsk"].value;
					qstr.addTwo = this["addTwo"].checked ? this["addTwo"].value : "";
					qstr.addThree = this["addThree"].checked ? this["addThree"].value : "";
					qstr.hasPic = this["hasPic"].checked ? this["hasPic"].value : "";
					loading(this);
					$.getJSON(url, qstr, loadHousing);
				} else if ( type == 'resumes' ) {
					qstr.type = "res";
					loading(this);
					$.getJSON(url, qstr, loadResumes);
				}

				log.add(qstr);
			});

			$(".more").live("click", function(e){
				var a = $(this),
						div = a.parent().next(".description");
						h = "32px";

				e.preventDefault();

				if ( a.text() == 'more' ) {
					div.css( {
						overflow: "auto",
						height: "auto"
					});
					a.text("less");
				} else {
					div.css( {
						overflow: "hidden",
						height: h 
					});
					a.text("more");
				}
			});
		}
	}
}();

