(function(){
    var w = window;
    var d = w.document;
	var original_body_content;
	
    w.setEventHandler=(function(){
        if (w.addEventListener) {
            return function(obj,evt,handler){obj.addEventListener(evt,handler,false)};
        }
        else if (w.attachEvent) {
            return function(obj,evt,handler){obj.attachEvent('on'+evt,handler)};
        }
        else {
            return function(obj,evt,handler){var org=obj['on'+evt];obj['on'+evt]=function(){if(typeof org=='function')org();handler()}};
        }
    })();
    w.setMouseHandler=function(obj, evt, handler) {
        var mouseHandler=function(curEvent,e) {
            if (!e) e=w.event;
            if (!e) return;
            var prop=obj._MouseProp_;
            var curStatus=prop.status;
            if (curStatus==curEvent) return;
            if (curStatus=='out') { //  outer to inner
                var fncArrayOver=prop.fncArrayOver;
                for (var ci=0,len=fncArrayOver.length; ci<len; ci++) {
                    fncArrayOver[ci].apply(obj,[e]);
                }
            }
            else {                  //  inner to outer
                var chkNode=e.toElement||e.relatedTarget;
                while (chkNode) {
                    if (chkNode==obj) return;
                    chkNode=chkNode.parentNode;
                }
                var fncArrayOut=prop.fncArrayOut;
                for (var ci=0,len=fncArrayOut.length; ci<len; ci++) {
                    fncArrayOut[ci].apply(obj,[e]);
                }
            }
            prop.status=curEvent;
        };
        var prop=obj._MouseProp_;
        if (!prop) {
            prop=obj._MouseProp_={status:'out',fncArrayOver:[],fncArrayOut:[]};
            w.setEventHandler(obj,'mouseover',function(e){mouseHandler('over',e)});
            w.setEventHandler(obj,'mouseout',function(e){mouseHandler('out',e)});
        }
        if (evt=='mouseover') {
            prop.fncArrayOver[prop.fncArrayOver.length]=handler;
        }
        else if (evt=='mouseout') {
            prop.fncArrayOut[prop.fncArrayOut.length]=handler;
        }
        else {
            w.setEventHandler(obj,evt,handler);
        }
    };
})();

function Found() {
    this.initialize.apply(this, arguments);
}
Found.prototype = {
   endPoint: 'http://imgfave.com/add',
   // endPoint: 'http://4u.straightline.jp/power/manage/register',
  //  staticUrl: 'http://www.straightline.jp/html/found/static/',
    ban: {
        width: 200,
		height:200
    },
    postStyle: {
        id: '__found_overlay',
        borderWidth: 10,
        color: '#DF0D0D',
        hoverColor: '#000'
    },
    dialogStyle: {
        width: 160,
        height: 100,
        color: '#fff',
        anchorColor: '#fff',
        backgroundColor: '#DF0D0D'
    },
   
    initialize: function() {
		original_body_content = document.body.innerHTML;
        this.w = window;
        if(0 < document.getElementsByTagName('frame').length){
            this.d=window[0].document;
        }else{
            this.d=document;
        }
		
		// If on flickr.com, remove "zoom" div that steals onmousover event
		if (window.location.href.indexOf('flickr.com') != -1) {
			var flickr_photo_div = this.d.getElementById('photo');
			var flickr_photo_proxy = this.d.getElementById('photo-drag-proxy');
			if (flickr_photo_div && flickr_photo_proxy) {
				flickr_photo_div.removeChild(flickr_photo_proxy);
			}
		}

        this.post = this.d.getElementById(this.postStyle.id);
        if (!this.post) {
            this.post = this.d.createElement('div');
            this.post.style.display = 'none';
            this.post.style.fontFamily = 'Georgia';
            this.post.style.position = 'absolute';
            this.post.style.zIndex = '7777';
            this.post.style.backgroundColor = 'transparent';
            //this.post.style.backgroundImage = 'url(http://imgfave.com/js/bg.gif)';
            this.post.style.top = '0px';
            
            this.dialog = this.d.createElement('div');
            this.dialog.style.position = 'absolute';
            this.dialog.style.textAlign = 'center';
            this.dialog.style.width = this.dialogStyle.width + 'px';
            this.dialog.style.color = this.dialogStyle.color;
          this.dialog.style.paddingTop = '0';
          this.dialog.style.paddingBottom = '0';
            //this.dialog.style.backgroundColor = this.dialogStyle.backgroundColor;
                this.dialog.style.backgroundColor = 'transparent';
             this.dialog.style.fontSize = '18px';
            
            this.text = this.d.createElement('div');
             this.text.style.paddingTop = '15px';
          this.text.style.paddingBottom = '15px';
            this.text.style.background = '#E30000'; // dark red
			this.text.style.fontSize = '20px';
			this.text.style.color = '#fff';
            this.text.appendChild(this.d.createTextNode('Add to imgfave'));

            
            this.options_div = this.d.createElement('div');
            this.options_div.style.background = '#E30000'; // dark red
              this.options_div.style.marginTop = "7px";
			this.options_div.style.fontFamily = "Arial";
			this.options_div.style.color = "#fff";
	

			this.tags_div = this.d.createElement('div');
			this.tags_div.style.padding = "5px 5px 0 5px";
			this.tags_div.style.fontSize = "12px";
			this.tags_div.style.color = "#fff";
			this.tags_input = this.d.createElement( "input" );
			this.tags_input.type = "text";
			this.tags_input.id = "tags";
			this.tags_input.name = "tags";
			this.tags_input.style.zIndex = '8888';
			this.tags_input.style.margin = '4px 3px 0 3px';
			this.tags_input.style.width = '140px';
			this.tags_input.style.color = "#000";
			this.tags_input.style.fontSize = "12px";
			this.tags_div.appendChild(this.d.createTextNode('Tags (comma seperated)'));
			this.tags_div.appendChild(this.tags_input);
			this.options_div.appendChild(this.tags_div);
 
  
          this.cb = this.d.createElement( "input" );
		this.cb.type = "checkbox";
		this.cb.id = "nudity_check";
		this.cb.name = "nudity_check";
		this.cb.value = "nudity";
		this.cb.checked = false;
		this.cb.style.zIndex = '8888';
		this.options_div.appendChild(this.cb);
		

		
		
		
		
		this.label = this.d.createElement('label');
         this.label.setAttribute('for','nudity_check');
         this.label.style.lineHeight = "26px";
          this.label.style.fontSize = "12px";
         this.label.style.marginLeft = "2px";
		this.label.appendChild(this.d.createTextNode('contains nudity'));
         this.options_div.appendChild(this.label);
            this.dialog.appendChild(this.text);
            
           this.dialog.appendChild(this.options_div);
            
           
            //text = this.d.createElement('p');
           // text.appendChild(this.d.createTextNode('is she nude or semi-nude?'));
           //this.dialog.appendChild(text);
            
           /* this.a_is_not_nude = this.d.createElement('a');
            this.a_is_not_nude.href = '#';
            this.a_is_not_nude.style.color = this.dialogStyle.anchorColor;
            
            this.a_is_not_nude.style.fontSize = 'x-large';
            this.a_is_not_nude.style.textDecoration = 'underline';
            this.a_is_not_nude.style.margin = '0.5em';
           // this.a_is_not_nude.appendChild(this.d.createTextNode('NO'));
            this.dialog.appendChild(this.a_is_not_nude);
            
            this.a_is_nude = this.d.createElement('a');
            this.a_is_nude.href = '#';
            this.a_is_nude.style.color = this.dialogStyle.anchorColor;
            this.a_is_nude.style.fontSize = 'x-large';
            this.a_is_nude.style.textDecoration = 'underline';
            this.a_is_nude.style.margin = '0.5em';
           // this.a_is_nude.appendChild(this.d.createTextNode('YES'));
            this.dialog.appendChild(this.a_is_nude); */
            
            this.post.appendChild(this.dialog);
            
            this.d.body.appendChild(this.post);
            
            this.w.setMouseHandler(this.post, 'mouseout', function() {
                this.style.display = 'none';
            });
			
		    this.w.setMouseHandler(this.text, 'mouseover', function() {
	                this.style.backgroundColor = 'red';
	            });
		
	       this.w.setMouseHandler(this.text, 'mouseout', function() {
				this.style.backgroundColor = '#E30000';
            });


                
        }
    },
   
    scanImg: function() {
        var self = this;
        var found = false;
        var imgs = this.d.getElementsByTagName('img');
        for(var i = 0; i < imgs.length; i++){
            var img = imgs[i];
           
            if(window.location.href.match(/imgfave\.com/)){
            alert("To save an image that's already on imgfave just click the \"fave\" button.");
            return;
            }
            
           
   			// took this out because sometimes images dont have an extension (in the case of a redirect)
            //  if (img.src.match(/\.(jpe*g|png|gif)/i)) {
         if (1 == 1) {
                if(img.src.match(/spaceball\.gif/) || (img.width < self.ban.width || img.height < self.ban.height)){
                    continue;
                }
               
                found=true;
               
                img.style.border = self.postStyle.borderWidth + 'px solid ' + self.postStyle.color;
             
                     
               self.text.onmouseover = function() {
                
                  self.text.style.cursor = 'pointer';
                    
                 }
                 
                  
                img.onmouseout = function() {
					self.tags_input.value = "";
				}
                
                img.onmouseover = function() {
                    var img = this;
                    var offset = self.getElementOffset(this);
                    var w = self.postStyle.borderWidth;
                    self.post.style.left = offset[0] + 'px';
                    self.post.style.top = offset[1] + 'px';
                    self.post.style.width = img.width + w * 2 + 'px';
                    self.post.style.height = img.height + w * 2 + 'px';
                    self.post.style.display = 'block';
                    self.text.cursor = 'pointer';
                   
                   
                   self.text.onclick = function() {
					
					//img.style.display = "none";
        
                    var pass_params = {
                            i: img.src,
                            //u: (img.src==location.href?document.referrer:location.href),
							u: location.href,
                            t: (self.d.title.replace(/\|/g, "/"))
                           /* alt: img.alt,
                            is_nude: 1,
                            bookmarklet: 1 */
                        };
                        
						// strip demensions info that appear after the filname (I should instead just grab the img.src and strip off the preceding path)
						if(pass_params['i']==window.location){
							pass_params['t'] = pass_params['t'].substring(0,pass_params['t'].indexOf(' '));	
						}
						
                        return self.postImg(img, pass_params);
                        
                    }
                    
					
                    self.dialog.style.left = ((img.width + w * 2) / 2 - self.dialogStyle.width / 2) + 'px';
                    self.dialog.style.top = ((img.height + w * 2) / 2 - self.dialogStyle.height / 2) + 'px';
                    
                 
                    
                   /* self.a_is_nude.onclick = function() {
                        var params = {
                            src: img.src,
                            site_url: (img.src==location.href?document.referrer:location.href),
                            site_title: self.d.title,
                            alt: img.alt,
                            is_nude: 1,
                            bookmarklet: 1
                        };
                        return self.postImg(img, params);
                    }
                    self.a_is_not_nude.onclick = function() {
                        var params = {
                            src: img.src,
                            site_url: (img.src==location.href?document.referrer:location.href),
                            site_title: self.d.title,
                            alt: img.alt,
                            bookmarklet: 1
                        };
                        return self.postImg(img, params);
                    } */
                };
               
            }
        }
       
        if(!found){
            window.alert('Image not found.')
        }
       
    },

	createIframe: function(go_url){
		if (document.getElementById("bookmarklet_iframe_container") != null){
			document.getElementById('bookmarklet_iframe').src = go_url;
			return;
		}
		var container = document.createElement('div');
		container.id = "bookmarklet_body";
		container.innerHTML = '<div id="bookmarklet_iframe_container" style="z-index: 9999; background: #fff; position:fixed; top: 0px; left:0px; width:100%;height: 130px; border-bottom: 10px solid blue;"><div style="position:absolute; top:5px; right:5px"><a href="#" id="iframe_close">Close</a></div><iframe style="z-index: 9999; width:100%;height: 100%; background: #fff;" allowtransparency="false" frameborder="0" id="bookmarklet_iframe" scrolling="no" src="'+go_url+'"></iframe></div>';
		
		//container.innerHTML = '<iframe frameborder="0" id="bookmarklet_iframe" scrolling="no" style="position:relative;top:0px;left:0px;width:100%;height:100%;border:0px;padding:0px;margin:0px"></iframe>';
		
		document.body.style.padding = (document.body.style.padding+130)+"px 0 0 0";
	
		document.body.appendChild(container);
		iframe_active = true;
		
		document.getElementById('iframe_close').onclick = function() {
			document.body.innerHTML = original_body_content;
			document.body.style.padding = '0 0 0 0';
			return false;
		}
		
		
	},
	

    postImg: function(img, pass_params) {
        var self = this;
        var url = [];

        url.push(self.endPoint);
        url.push('?');


		

        for(var n in pass_params){
        	if (n == "i" || n == "t" || n == "u"){
            	url.push(encodeURIComponent(n));
            	url.push('=');
            	url.push(encodeURIComponent(pass_params[n]));
           		 url.push('&')
            }
        }

		var go_url = url.join('');
		
		if (this.tags_input.value) {
			go_url = go_url+"tags="+encodeURIComponent(this.tags_input.value);
		}
		
     
        if (this.cb.checked == true){
          go_url = go_url+"&n=1";
        }
        

	   if(pass_params['i']==window.location){ // if were viewing the image directly
		window.location = go_url;
		}else{
       this.createIframe(go_url);
		}
		

       //return;
      
	var offset = self.getElementOffset(img);
	        var w = self.postStyle.borderWidth;        
	        var div = self.d.createElement('div');
	        div.id = img.src;
	        div.style.position = 'absolute';
	        div.style.left = offset[0] + 'px';
	        div.style.top = offset[1] + 'px';
	        div.style.width = img.width + w * 2 + 'px';
	        div.style.height = img.height + w * 2 + 'px';
	            //div.style.backgroundImage = 'url(http://www.straightline.jp/html/found/static/image/bg_done.gif)';
	        div.style.zIndex = '7777';
	        self.d.body.appendChild(div);

           
        return false;
    },

    getElementOffset: function(element) {
        var valueT = 0;
        var valueL = 0;
        while (element) {
            valueT += element.offsetTop||0;
            valueL += element.offsetLeft||0;
            element = element.offsetParent;
            if(element){
                p = element.style.position;
                if (p=='relative' || p=='absolute') break
            }
        }
        return [valueL, valueT]
    }
};

var found = new Found();
found.scanImg();
 
