// just a general object to hold global timers... Timers = { } var ImageCache = function(config){ config = config || {"token": Math.random(), 'timerName': "search"} this.timerName = config.timerName clearTimeout(Timers[config.timerName]) this.urls = ('urls' in config) ? config.urls : [] this.loadingImages = [] this.loadedImages = [] this.displayedImages = [] this.loadAtATime = 10 this.token = config.token this.loadWatcher = makeImageCacheWatcher(this) this.loadWatcher() } function makeImageCacheWatcher(imageCache){ return function(){ var newLoadingImages = [] // console.log("hi " + imageCache.token) imageCache.loadingImages.forEach(function(img){ if (img.complete) { if (img.height == 0 || img.width == 0 || img.height == NaN || img.width == NaN) { // call server... dump.fm/cmd/thisImageIsFuckingBrokenY'all?img=foo } else { imageCache.loadedImages.push(img) // console.log("loaded +" + img.src) } } else newLoadingImages.push(img) }) delete imageCache['loadingImages']; imageCache.loadingImages = newLoadingImages while (imageCache.loadingImages.length < imageCache.loadAtATime && imageCache.urls.length) { var img = new Image() img.src = imageCache.urls.shift() img.animated = (parseUri(img.src)["file"].toLowerCase().substr(-3) == "gif") ? true : false; imageCache.loadingImages.push(img) } Timers[imageCache.timerName] = setTimeout(makeImageCacheWatcher(imageCache), 500) }} /* imagecache.add() imagecache.add() imagecache.add() periodically... if (loaded)... (display) pause & start */ /* search adds a script to the page... the script will call either Search.searchResult() or Search.searchError() todo: clean this up. remove duplicated function names etc */ var Search = { 'term': "", 'images': [], 'tokens': [], 'imageCache': new ImageCache(), 'init': function(){ $("#search-query").val("search dump.fm") $("#search-query").focus(function(){ if ($("#search-query").val() == 'search dump.fm') $("#search-query").val("") }) $("#search-query").blur(function(){ if ($("#search-query").val().trim() == '') $("#search-query").val("search dump.fm") }) $("#search-query").keydown(ifEnter(Search.doSearch)) $("#search-results-images a").live("mouseup", Search.click) }, 'addScript': function(term) { $("#search-script").remove() $("head").append("") }, 'setContent': function(x){ $("#search-results-images").html(x) }, 'setMessage': function(x){ $("#search-controls").css("display", "block") $("#search-control-text").html(x) }, 'searchError': function(error){ Search.setContent("") $('#search-control-previous').css("visibility", "hidden") $('#search-control-next').css("visibility", "hidden") Search.setMessage(error) }, 'doSearch': function(){ term = $("#search-query").val().trim().toLowerCase() var rawTokens = term.split(" ") Search.tokens = [] rawTokens.forEach(function(t){ if (t.length > 2) Search.tokens.push(t) }) if (Search.tokens.length == 0) { Search.setMessage("search query too small") } else { Search.setMessage("searching for '"+Search.tokens.join(" and ")+"'") Search.addScript(Search.tokens.join("+")) } }, 'click': function(e){ if (e.which == 1) // left click if (Search.addToChatBoxIfPossible(this)) window.open(this.href) else if (e.which == 2) // middle click window.open(this.href) }, 'addToChatBoxIfPossible': function(img){ var chatBoxExists = $("#msgInput").length if (chatBoxExists) { var chatText = $("#msgInput").val() if (chatText.length && chatText[chatText.length - 1] != " ") chatText += " " chatText += $(img).attr("href") + " " $("#msgInput").val(chatText) $("#msgInput").focus().val($("#msgInput").val()) //http://stackoverflow.com/questions/1056359/ return false } else return true }, 'searchResult': function(results){ Search.images = [] if(results === null || results.length == 0) { Search.setContent("") Search.setMessage("no results found") } else { results.forEach(function(r){ Search.images.push(r.url) }) Search.display() } }, 'display': function(){ $("#content").html("") initSpaceFill() //console.log(Search.images) Search.imageCache = new ImageCache({"urls": Search.images, "token": Math.random(), 'timerName': "search"}) }, 'close': function(){ Search.setContent("") $('#search-control-previous').css("visibility", "hidden") $('#search-control-next').css("visibility", "hidden") $("#search-results-images").css("display", "none") $("#search-controls").css("display", "none") } } $('#content a').live('hover', function(event) { if (event.type == 'mouseover') { var img = Search.imageCache.displayedImages[this.index] if (img.animated) { img.width = img.adjWidth img.height = img.adjHeight $(this).addClass("animating") $(this).append(img) } } else { var img = Search.imageCache.displayedImages[this.index] if (img.animated) { $(this).removeClass("animating") this.removeChild(img) } } });