var f5f5f5 = {
	images:[],
	init:function(count) {
		this.points = [[568,0,882,222]];
		this.count = count;
		this.get_words_library(count);
		this.j_control = $('#control');
	},
	get_words_library:function(){
		var that = this;
		$.getJSON("library.json", function(json){
			that.lib = json.lib ? json.lib : [];
			that.lib_count = that.lib.length-1;
			that.getImages();
		});
	},
	search_image:function(q){
	    google.load("search", "1");
		google.setOnLoadCallback(function() {
		  // Create a search control
	      var searchControl = new google.search.SearchControl();
	
	      // Add in a full set of searchers
	      var localSearch = new google.search.LocalSearch();
	      searchControl.addSearcher(localSearch);
	      searchControl.addSearcher(new google.search.WebSearch());
	      searchControl.addSearcher(new google.search.VideoSearch());
	      searchControl.addSearcher(new google.search.BlogSearch());
	
	      // Set the Local Search center point
	      localSearch.setCenterPoint("New York, NY");
		  searchControl.setRestriction(google.search.Search.RESTRICT_SAFESEARCH,
                    google.search.Search.SAFESEARCH_OFF);

	      // Execute an inital search
	      searchControl.execute("Google");
		});
	},
	googleAjax: function(q) {
		var that = this,
			rnd = Math.random(),
			size = rnd < 0.33 ? 'icon' : ( rnd < 0.66 ? 'small' : 'medium' ),
			rnd2 = Math.random(),
			start = rnd2 < 0.25 ? 0 : (rnd2 < 0.5 ? 4 : (rnd2 < 0.75 ? 8 : 12));
			
		$.getJSON("http://ajax.googleapis.com/ajax/services/search/images?q="+q+"&v=1.0&rsz=large&imgsz="+size+"&callback=?", function(data){
				if (data.responseData.results) {
					that.show_images(data.responseData.results);
					return data.responseData.results;
				}
				if (error) return error(); 
				return false;
		});
	},
	show_images: function(data) {
		if(!data[0]) return false;
		
		var index = parseInt(Math.random() * data.length),
			screenWidth = $("body").width(),
			screenHeight = $("body").height(),
			left = 0,
			top = 0,
			w = parseInt(data[index].width),
			h = parseInt(data[index].height),
			that = this;
		
		function checkPoint(x1, y1, x2, y2){

			var ret = true;
			$.each(that.points, function(i, point){
				if(!ret || (x1 < point[2] && x2 > point[0] && y1 < point[3] && y2 > point[1])) {
					return ret = false;
				}
			});
			return ret;
		}
		function getPoint(w, h){
			var coefficient = 0.7,
				point = [],
				left = 0,
				top = 0,
				isOk = false;

			while(!isOk) {
				left = coefficient*Math.random()*screenWidth;
				top = coefficient*Math.random()*screenHeight;
				isOk = checkPoint(left, top, left+w, top+h) ? true : false;
				coefficient += 0.1;
			}

			return that.points[that.points.length] = [left, top, left+w, top+h];
		}
		
		point = getPoint(w, h);
		$('<img />').attr({
			width: w,
			height: h,
			src: data[index].unescapedUrl
		}).css({
			left: point[0]+'px',
			top: point[1]+'px'
		}).appendTo(this.j_control);
	},
	getImages:function(){
		this.images = [];
		for(var i = 0; i < this.count; i++){
			this.images.pop(this.googleAjax(this.lib[parseInt(Math.random()*this.lib_count)]));
		}
	}
}

var i = new Image(),
	src = "i/F5F5F5_tutorial.gif";
i.src = src;

$(function(){
	f5f5f5.init(parseInt($("body").width()*$("body").height()/50000));
	$('#logo .help').show().add("#bck").click(function(){
		$("#help").toggle();
	});
	if($.cookie('not-first-visit')=="true"){
		setTimeout('$("#logo .finger").attr("src", src)', 1000*60*4);
	} else {
		$("#logo .finger").attr("src", src);
		$.cookie('not-first-visit', "true");
	}
});