/**
 * imgnotes jQuery plugin
 * version 0.1
 *
 * Copyright (c) 2008 Dr. Tarique Sani <tarique@sanisoft.com>
 *
 * Dual licensed under the MIT (MIT-LICENSE.txt) 
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * @URL      http://www.sanisoft.com/blog/2008/05/26/img-notes-jquery-plugin/
 * @Example  example.html
 *
 **/


function display_tag(notenum) {
	$("#note_"+notenum).css('border', '2px solid rgb(255, 255, 255)');
	$("#notep_"+notenum).show();
}
function hide_tag(notenum) {
	$("#note_"+notenum).css('border', '0px');
	$("#notep_"+notenum).hide();
			
}

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

	$.fn.imgNotes = function(n, div_id) {
		
		if(undefined != n){
			notes = n;
		} 
				
		if (!$.noteNumber) {
			$.noteNumber = 0;
		}
		
		if (!$.allNotes) {
			$.allNotes = [];
		}
		if (!$.divTags) {
			$.divTags = [];
		}
		if (!$.allTags) {
			$.allTags = [];
			$.allTagsRemove = [];
		}
				
		$.disable_hover = 0;
		
		image = this;
		
		imgOffset = $(image).offset();
		
		$(notes).each(function(){
			if (this.width != undefined) {
				appendnote(this, div_id);
			}
		});

		addnoteevents();
		
		$(window).resize(function () {

			$('.note').remove();

			imgOffset = $(image).offset();

			$(notes).each(function(){
				appendnote(this);				
			});

			addnoteevents();

		});
	} 
	
	function addnoteevents() {
	
	}


	function appendnote(note_data, div_id){
		
		$.allNotes[$.noteNumber] = note_data;
		
		//alert(note_data);
		
		note_left  = parseInt(imgOffset.left) + parseInt(note_data.x1);
		note_top   = parseInt(imgOffset.top) + parseInt(note_data.y1);
		note_p_left = note_left + (parseInt(note_data.width)/2)-15;
		note_p_top = note_top + (parseInt(note_data.height)/2)-6;

		//alert(div_id);
		
		note_tag = note_data.note;
		var note_tag=note_tag.split("|"); 
		
		note_overlay = note_tag[1];
		if (note_tag[0] == 0) {
			note_tag_final = note_tag[1];
		} else {
			note_tag_final = '<a href="/memberlist.php?mode=viewprofile&u='+note_tag[0]+'" target="_blank">'+note_tag[1]+'</a>';
		}
		
		note_area_div = $("<div class='note' onmouseover='display_tag(\""+$.noteNumber+"\")' onmouseout='hide_tag(\""+$.noteNumber+"\")' id='note_"+$.noteNumber+"'></div>").css({ left: note_left + 'px', top: note_top + 'px', width: note_data.width + 'px', height: note_data.height + 'px' });	
		note_text_div = $('<div class="notep" name="'+div_id+'_notep" id="notep_'+$.noteNumber+'">'+note_overlay+'</div>').css({ left: note_p_left + 'px', top: note_p_top + 'px'});
	
		$('body').append(note_area_div);
		$('body').append(note_text_div);
		
		
		// Append note text below img

		if ($.allTags[div_id] == undefined) {
			comma_sep = ""; 
			$.allTags[div_id] = "";
		}
		if ($.allTagsRemove[div_id] == undefined) {
			$.allTagsRemove[div_id] = "";
		}
		else comma_sep = ", ";
		$.allTags[div_id] += comma_sep+'<span id="tag_'+$.noteNumber+'"><span onmouseover="display_tag(\''+$.noteNumber+'\')" onmouseout="hide_tag(\''+$.noteNumber+'\')">'+note_tag_final+'</span></span>';
		$.allTagsRemove[div_id] += comma_sep+'<span id="tag_'+$.noteNumber+'"><span onmouseover="display_tag(\''+$.noteNumber+'\')" onmouseout="hide_tag(\''+$.noteNumber+'\')">'+note_tag_final+'</span> (<a href="#" onclick="noteRemove('+$.noteNumber+'); return false;">fjern</a>)</span>';
		//$.divTags[]
		
		$.noteNumber++;
	}
	
	// Public methods
	var newMethods = {
		hoverDisable : function() {
			$.disable_hover = 1;
		},
		hoverEnable : function() {
			$.disable_hover = 0;
		},
		getData : function() {
			return $.allNotes;
		},
		updateTags : function(div_id) {
			//$('#'+div_id+'_imagetags').html('');
			//alert($.allTags);
			if ($.allTags[div_id] != undefined) {
				$('#'+div_id+'_imagetags').html($.allTags[div_id]);
			}
		},
		updateTagsRemove : function(div_id) {
			//$('#'+div_id+'_imagetags').html('');
			//alert($.allTags);
			if ($.allTagsRemove[div_id] != undefined) {
				$('#'+div_id+'_imagetags').html($.allTagsRemove[div_id]);
			}
		},
		resetTags : function() {
			$.disable_hover = 1;
			$.allTags = [];
			$.allTagsNoRemove = [];
		}
	};
	// Load public methods
	jQuery.each(newMethods, function(i) {
		jQuery.fn[i] = this;
	});

// End the closure
})(jQuery);