//Wrap in a closure
(function($) {

	$.fn.imgTags = function(n) {
        //alert(n);
        /*
		if(undefined != n){
            alert('cae');
			tags = n;
		}
        */
            if (window.addEventListener) {
                window.addEventListener("onclick", function(event){cancelResult(event)}, false);
            } else if (window.attachEvent) {
                window.attachEvent("onclick", function(event){cancelResult(event)}, false);
            }

        //window.addEventListener("onclick", function(event){cancelResult(event)}, false);
		image = this;
        imgOffset = $(image).offset();
        if(n == 'addnew') {
            var len = tags.length;
            appendtag(tags[len-1]);
        }
        else {
            $(tags).each(function(){
                appendtag(this);
            });
        }

        $(image).hover(
			function(){
				$('.tag').show();
			},
			function(){
				$('.tag').hide();
			}
		);

		addtagevents();
		
		$(window).resize(function () {
			$('.tag').remove();

			imgOffset = $(image).offset();
			$(tags).each(function(){
				appendtag(this);				
			});

			addtagevents();

		});
	} 
	
	function addtagevents() {
		$('.tag').hover(
			function(){
                if(addTagActivated == true) return;
				$('.tag').show();
				$(this).next('.tagp').show();
				$(this).next('.tagp').css("z-index", 10000);
				$(this).css("display", "block");
                $(this).css("border", "1px solid white");
			},
			function(){
                if(addTagActivated == true) return;
				$('.tag').show();
				$(this).next('.tagp').hide();
				$(this).next('.tagp').css("z-index", 0);
				$(this).css("display", "none");
                $(this).css("border", "0px solid white");

			}
		);
	}


	function appendtag(tag_data){
		tag_left  = parseInt(imgOffset.left) + parseInt(tag_data.x1);
		tag_top   = parseInt(imgOffset.top) + parseInt(tag_data.y1);
		tag_p_top = tag_top + parseInt(tag_data.height)+5;
        tagid  = tag_data.tagid;
        friendid = tag_data.friendid;
        
        tag_area_div = $("<div name='tagarea_"+tagid+"' id='tagarea_"+tagid+"' class='tag'  onclick=goProfile("+friendid+")></div>").css({ left: tag_left + 'px', top: tag_top + 'px', width: tag_data.width + 'px', height: tag_data.height + 'px' });
		tag_text_div = $("<div name='tagtext_"+tagid+"' id='tagtext_"+tagid+"' class='tagp' onclick=goProfile("+friendid+")>"+tag_data.tag+"</div>").css({ left: tag_left + 'px', top: tag_p_top + 'px'});
		//$('body').remove($('.tag'));
		//$('body').remove($('.tagp'));
        
        
        //alert(body.getElementById('tagarea_97').id);
		$('body').append(tag_area_div);
		$('body').append(tag_text_div);
	}

// End the closure
})(jQuery);