var DL_bNS4=(document.layers);
var DL_bNS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko"));
var DL_bDOM=(document.getElementById);
var DL_bIE=(document.all);
var DL_bIE4=(DL_bIE && !DL_bDOM);
var DL_bMac = (navigator.appVersion.indexOf("Mac") != -1);
var DL_bIEMac=(DL_bIE && DL_bMac);
var DL_bIE4Mac=(DL_bIE4 && DL_bMac);
var DL_bNS =(DL_bNS4 || DL_bNS6);
var DL_nCurrentX = 0;
var DL_nCurrentY = 0;
var currentZIndex=5001;

var DL_oLayer;
var velocity = 1.01;
var accelFactor = 1.1;

var noteTemplateNode;
var insertBeforeMarker;

var noteNumber=1;

var deleto = false;

function loadNotes() {

	var oHead = document.getElementsByTagName('HEAD').item(0); 
        var oBody = document.getElementsByTagName('BODY').item(0); 
		
        //note template
	var oElement2 = document.createElement("div"); 
	oElement2.id = "notesContainer"; 
	oBody.appendChild(oElement2); 
	document.getElementById('notesContainer').innerHTML = "<div class='notesLayer' id='noteTemplate'><iframe class='bufferIframe' src='http://linkstore.ru/al/transparent-y.htm'  frameborder='0'></iframe>"
        +"<input name='left' type='hidden' value='100px'/><input name='top' type='hidden' value='100px'/><input name='width' type='hidden' value='200px'/><input name='height' type='hidden' value='200px'/>"
        +"<div class='dragBar'><b style='color:#004276;position:absolute;left:5px' id='title'></b> <img src='http://linkstore.ru/al/images/x.gif' style='cursor:pointer;position:relative;top:2px;' onmousedown='opacity(event,this.parentNode.parentNode, 100, 0, 500);' />&nbsp;</div>"
        +"<fieldset style='border:none;padding:0px;padding-left:4px;padding-right:4px;margin-right:0px;'><div style='overflow:auto;'><textarea id='noteBody' class='protoText'></textarea></div></fieldset>"
        +"<table style='float:left;'><tr><td style='padding-left:5px;' id='noteAction'> </td><td><span style='font-size:9px;color:#666666;'> </span></td><td style='width:10px;'></td><td></td><td><input name='noteId' type='hidden' value='null'/></td><td></td></tr></table>"
        +"<img src='http://linkstore.ru/al/images/dragger-y.gif' style='cursor:se-resize;position:relative;bottom:-12px;padding-right:2px;'/></div><a id='bottomMarker'></a>";

	//white mask while dragging
	var oElement3 = document.createElement("div"); 
	oElement3.id = "noteSeparatorLayer"; 
	oBody.appendChild(oElement3);

	var currentNote=document.getElementById('noteTemplate');
	var d=currentNote.parentNode;
	noteTemplateNode = d.removeChild(document.getElementById('noteTemplate'));
	insertBeforeMarker = document.getElementById('bottomMarker');
}

function deleteNote(deletedNoteId) {
deleto=true;setTimeout("resetDeleto()",100);}

function resetDeleto() {deleto=false;}

var targetId;
function insertNote(fadeIn, realId, areaId, title, x, y, readOnly) {
	noteId=null;
	var newNote = noteTemplateNode.cloneNode(true);
	targetId = newNote;
	if (fadeIn) {
		opacity1(targetId, 0, 90, 500); 
		newNote.style.left="100px";
		newNote.style.top="100px";
	} else {
		newNote.style.opacity = (90 / 100); 
		newNote.style.MozOpacity = (90 / 100); 
		newNote.style.KhtmlOpacity = (90 / 100); 
		newNote.style.filter = "alpha(opacity=90)"; 
		newNote.style.display="none";
	}

	infoContainer = document.getElementById('notesContainer');
	infoContainer.insertBefore(newNote,insertBeforeMarker);
	DL_oLayer=newNote;
	currentZIndex++;
	DL_oLayer.style.zIndex=currentZIndex;

	DL_oLayer.getElementsByTagName('div')[0].onmousedown=DL_fGrabEl;  
	iframeBuffer=DL_oLayer.getElementsByTagName('iframe')[0];
	dragResizer=DL_oLayer.getElementsByTagName('img')[DL_oLayer.getElementsByTagName('img').length-1];
	dragResizer.onmousedown= expand;  
	DL_oLayer.getElementsByTagName('div')[0].onmousedown=DL_fGrabEl; 
	DL_oLayer.style.display="none";
	newNote.getElementsByTagName('iframe')[0].src=newNote.getElementsByTagName('iframe')[0].src;

	if (fadeIn) {DL_oLayer.style.display="block";}

        newNote.getElementsByTagName('input')[4].value = realId;
        newNote.getElementsByTagName('textarea')[0].id = areaId;

        if (title)
         newNote.getElementsByTagName('b')[0].innerHTML = title;

        if (x) newNote.style.left=x+"px";
        if (y) newNote.style.top=y+"px";

        if (readOnly)
           newNote.getElementsByTagName('textarea')[0].readonly="readonly";	
        else
        {
           newNote.getElementsByTagName('textarea')[0].focus();	
           newNote.getElementsByTagName('td')[0].id = areaId+"_"+realId;
           newNote.getElementsByTagName('td')[0].innerHTML = '<a class=\'notesSendLink\' title=\'email this link\' onClick=\"mailNote(\''+areaId+'\',\''+realId+'\');return false;\">email</a>'
                                                           + ' | '
                                                           + '<a class=\'notesSendLink\' title=\'twil this link\' onClick=\"twitNote(\''+areaId+'\',\''+realId+'\');return false;\">twitter</a>';

        }
}

function newNote()
{
 insertNote(true, "_noteId"+noteNumber,"_noteAreaId"+noteNumber, "", 20+Math.floor(Math.random()*500),20+Math.floor(Math.random()*200)); 
 document.getElementById("_noteAreaId"+noteNumber).value='';
 noteNumber++;}

function writeNote(txt)
{
insertNote(true, "_noteId","_noteAreaId", "", 200,100);               
document.getElementById("_noteAreaId").innerHTML = txt;
}

function showNote() {DL_oLayer.style.display="block";}

function showHideAllNotes(hide) {
	allDivs =document.getElementsByTagName('div');
	numOfDivs = allDivs.length; notesExist=false;
	// get all notes DIVS
	for (var i = 0; i < numOfDivs; i++) {
		if (allDivs[i].className=="notesLayer") {
			notesExist=true;
			if (!hide) { allDivs[i].style.display="none";
			} else { allDivs[i].style.display="block"; }
		} }
	if (!notesExist && !isNewNote) alert('There are no notes on this page.');}

function mailNote(areaId, realId)
{ var o = document.getElementById(areaId);
  if (!o) return false;
  if (!o.value) return false;

  var s = o.value;
  if (s=='') return false;

  var query = 'n='+encodeURIComponent(s)+"&c="+document.location.href;
  var uri = 'http://linkstore.ru/al/savenote.jsp';

  var beforeAction = function() 
  { document.getElementById(areaId+"_"+realId).innerHTML = "Sending <img src='http://linkstore.ru/al/images/indicator.gif' alt=''/>"; };

  var afterAction = function() 
  { document.getElementById(areaId+"_"+realId).innerHTML = '<a class=\'notesSendLink\' title=\'send this link\' onClick=\"mailNote(\''+areaId+'\',\''+realId+'\');return false;\">Send</a>';};

  noteAjaxEnginePost(uri, query, noteHandlerFunction, noteErrorFunction, beforeAction, afterAction);}

function twitNote(areaId, realId)
{ var o = document.getElementById(areaId);
  if (!o) return false;
  if (!o.value) return false;

  var s = o.value;
  if (s=='') return false;

  var query = 'n='+encodeURIComponent(s)+"&c="+document.location.href;
  var uri = 'http://linkstore.ru/al/savenote1.jsp';

  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", "http://linkstore.ru/al/savenote1.jsp?"+query);
  document.getElementsByTagName("head")[0].appendChild(fileref);
}


function noteErrorFunction(){  alert("Could not save note ...");}

function noteHandlerFunction(txt, xmlDoc)
{ if (txt)
  { var s = trimResponse(txt);
    sendMail(s);}
}

function noteAjaxEnginePost(uri, query, handlerFunction, errorFunction, beforeAction, afterAction)
{
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

    if (handlerFunction==null)  handlerFunction = function() {};
    if (errorFunction==null)    errorFunction = function () {};
    if (beforeAction == null) beforeAction = function() {};
    if (afterAction == null) afterAction = function() {};  
    if (query==null) query="";
    var r = (window.ActiveXObject)?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
    if (r) { r.onreadystatechange = function()
      {	if (r.readyState == 4)
        {afterAction();
         if (r.status == 200)
  	 { xmlDoc = r.responseXML; txt = r.responseText;
           handlerFunction(txt, xmlDoc);}
         else          errorFunction();
        } }

     beforeAction(); r.open("POST", uri, true);
     r.setRequestHeader("Content-Type", contentType);
     r.setRequestHeader("Content-length", query.length);
     r.setRequestHeader("Connection", "close");
     r.send(query); return true;
  } else { errorFunction(); return false; }}

function trimResponse(s0)
{ if (s0==null) return s0; s=s0;

  while (s.length>0)
  { s1=s.substring(0,1);
    if (s1==' ' || s1=='\r' || s1=='\n' || s1=='\t') s=s.substring(1);
    else  return s; }

  return '';}

function sendMail(sUrl)
{    var subjectLine='Take a look at this note'; 
     var bodyText='You can see this note at: '+sUrl;

     var message='mailto:?subject='+escape(subjectLine)+'&body='+escape(bodyText);
     var messageIE='mailto:?subject='+(subjectLine)+'&body='+escape(bodyText);     

     if(document.all) location.href=messageIE; 
     else  location.href=message; }

function sendTwit(sUrl)
{    
     var bodyText='link and note: '+sUrl;

     var message='http://twitter.com/home?status='+escape(bodyText);

     location.href=message; 
}



//loadNotes();