var inProgress = "N";
var currentSelected = "";
var layout = "0";
var hashtable = {};

function show(element){
    if(document.getElementById(element).style.display == "none") {
   		Effect.BlindDown(element,{duration:0.75,afterFinish:inProgress="N"});
    } else {
       	Effect.BlindUp(element,{duration:0.75,afterFinsih:inProgress="N"});
	}
}


function toggleLogin(element){
    if(inProgress == "N"){
    	inProgress = "Y"
    	if(document.getElementById(element).style.display == "none") {
    	    Effect.BlindDown(element,{duration:0.75,afterFinish:changeLoginFocus});
    	} else {
    	    Effect.BlindUp(element,{duration:0.75});
    	}
    } else {
    	inProgress = "N";
    }
    return(false);
}

function setFocus(){
    document.forms['signin'].email.focus;
}

function dumpIt(id){
    var parentElement = $('page') ;
    var childElement = null;
    var children = parentElement.childNodes;
    Effect.DropOut(id);
    for (i=0; i<children.length; i++){
        if(children.item(i).id == id){
            //parentElement.removeChild( children.item(i));
        }
    }
    for(i=0; i < sections.length;i++){
        if(sections[i] == id)
            sections.splice(i,1);
    }
}

var doubleGlobal;
var editing;
var n = "";

function editNote(element_id){
    editing = element_id;
    document.forms['note_form'].element_id.value = element_id;
    edit(editing);
}

function createNewSection() {
    resetForm();
   
    n = "Y";
    w = Math.round(browserSize('w')/2-110);
    h = Math.round(browserSize('h')/2-190);
 
    currentTime = new Date();
    var xmlhttp = createRequest();
    xmlhttp.open('POST','generateid.php',true);
        
    xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {
        	if (xmlhttp.status == 200){
                rid = xmlhttp.responseText;
			    editing = rid;
                document.forms['note_form'].element_id.value = rid;
                var newDiv = Builder.node('div', {id: rid, className: 'section', style: 'display:none;left:'+w+'px;top:'+h+'\px;z-index:'+currentIndex+';',onclick:'bringToFront(this)',ondblclick:'editNote(\'' + rid + '\');'}, [
			                 Builder.node('div',{id: "inner-"+rid,className: 'inner-note-text'},"")]);
			    sections.push(newDiv.id);
			    
			    $('page').appendChild(newDiv);
			    Effect.Appear(newDiv.id);
			    createLineItemSortables();
		document.getElementById('note').style.background='#F5EEBA';
                Effect.Appear("enter-it",{duration:0.0});
            } else { alert("error"); }
        }
    }     
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("");   
}

function createLineItemSortables() {
    for(var i = 0; i < sections.length; i++) {
        new Draggable(sections[i],{
            scroll:window,
            onStart:function(draggable){
            	bringToFront(draggable.element);
            },
            onEnd:function(draggable){
                updatePosition(findPosX(draggable.element),(findPosY(draggable.element)),draggable.element.id);
                doubleGlobal=0;
                bringToFront(draggable.element);
                
            }
        });

    }
}



function loadNotes(){
    for(var i = 0; i < sections.length; i++) {
        createLineItemSortables();
        Effect.Appear(sections[i],{duration:1.0,from:1.0,to:1.0});   
    }
}

function mkRandomString() {
       return Math.floor(Math.random()*90000)
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
  
function popArray(element_id){
    for(i=0; i < sections.length;i++){
        if(sections[i] == element_id)
            sections.splice(i,1);
    }
}

function resetForm(){
	document.forms['note_form'].reset();
	document.getElementById('file-upload').style.display="inline";
	document.getElementById('file-upload-text').style.display="inline";
	document.getElementById('file-upload-head').style.display="inline";
	document.getElementById('uploaded-file').style.display="none";
	document.getElementById('uploaded-file-head').style.display="none";
	document.forms['note_form'].p.value = "L";
}

function changeLoginFocus(){
	document.getElementById('email').focus();
	inProgress="N";
}

currentIndex=1000;

function bringToFront(element){
	element.style.zIndex = currentIndex++;
	currentIndex++;

}


function countChar(str,f){
	if( (str.length) > 140){
		 f.note.value = str.substring(0, 140);
	} else {
		f.chars.value = 140 - str.length;
	}
}

function effectFunction(element)
{
   new Effect.Opacity(element, {duration:0,from:1.0,to:1.0});
}

function deleteNote(element){
	deleteElement(element);
	Effect.Fade(element,{duration:0.4});
	
}

function browserSize(wh) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if(wh == 'w') return myWidth;
  if(wh == 'h') return myHeight;
}

function MoveEffect(action){
	var bw = browserSize('w');
 	var bh = browserSize('h');

 	if(action == "1"){
		if(layout == "0"){
			savePosition();
		}

		layout = "1";
		var xvalue = bw/2 - 110;
		var yvalue = bh/2 - 150;

		for(var i = 0; i < sections.length; i++) {
			if(i == (sections.length) - 4) yvalue = yvalue-30;
			if(i == (sections.length) - 3) yvalue = yvalue+50;
			if(i == (sections.length) - 2) xvalue = xvalue+30;
			if(i == (sections.length) - 1) xvalue = xvalue-40;
	
			new Effect.Move(document.getElementById(sections[i]), {x: xvalue, y: yvalue, mode: 'absolute',duration:0.2})
		}
	 } else if(action == "2"){
	 	if(layout == "0"){
			savePosition();
		}
	 	layout = "2";
	 	var xvalue=0;
	 	var yvalue=50;

	 	for(var i = 0; i < sections.length; i++) {
			if(i != 0) xvalue = xvalue + 235;
			if(xvalue+235 > bw) { xvalue=0;yvalue=yvalue+235;}
			new Effect.Move(document.getElementById(sections[i]), {x: xvalue, y: yvalue, mode: 'absolute',duration:0.2})
	 	}
	} else if(action == "0"){
		layout = "0";
		var xvalue=0;
		var yvalue=0;

		for(var i = 0; i < sections.length; i++) {
			var position_array = (hashtable[sections[i]]).split(",");
			xvalue = position_array[0];
			yvalue = position_array[1];
			xvalue = xvalue.substring(0,(xvalue.length-2));
			yvalue = yvalue.substring(0,(yvalue.length-2));

			new Effect.Move(document.getElementById(sections[i]), {x: xvalue, y: yvalue, mode: 'absolute',duration:0.2})
		}
 	}
} // end MoveEffect

function savePosition(){
	for(var i = 0; i < sections.length; i++) {
		hashtable[sections[i]] = document.getElementById(sections[i]).style.left+","+document.getElementById(sections[i]).style.top;
	}
}

function saveSinglePosition(element){
	hashtable[element] = document.getElementById(element).style.left+","+document.getElementById(element).style.top;
}