var showUp = 0;



Array.prototype.contains = function ( needle ) {
   for (i in this) {
       if (this[i] == needle) return true;
   }
   return false;
}

var checkUrl = document.URL;
if(checkUrl.match('#')){
    	var toReplace = checkUrl.substring(checkUrl.indexOf('#'),checkUrl.length);
    	checkUrl = checkUrl.replace(toReplace, "");
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    if(vars.contains('printAll')){
    	print();
    }
}

function getContents(url){
	
	$.ajax({ 
	    method: "get",
	    url: checkUrl,
	    data: "method=ajax&action=getList",
	    beforeSend: function(){
	    	/* kommentar */
	    	
	    },
	  	complete: function(){ 
	  		/* kommentar */
	  	},
		success: function(html){
			$('#memorizeOverlay').empty();
			html = $.trim(html);
			$("#memorizeOverlay").html(html);
			
	    }
   	});
}

function clickOverlay(){
      	if(showUp == "0"){
      		showOverlay();
      	}else{
      		closeOverlay();
      	}
      	return false;
}

function showOverlay(){
	
	var footerHeight = $('.footerWrap').outerHeight();
	var pageHeight = $(".wrap").eq(0).outerHeight();
	var overlayHeight = $('#memorizeOverlay').outerHeight();
	var overlayPosition = pageHeight - footerHeight - overlayHeight + 4;
	$('#memorizeOverlay').css('top',overlayPosition);
	$('#memorizeOverlay').fadeIn('fast', function() {
		//performing animation
    });
    showUp = 1;
}

function closeOverlay(){
	$('#memorizeOverlay').fadeOut('fast', function() {
		//performing animation
    });
    showUp = 0;
}

function sortItems(position,direction){
	$.ajax({ 
	    method: "get",
	    url: checkUrl,
	    data: 'method=ajax&action=sort&position='+ position +'&direction='+ direction+'',
	    beforeSend: function(){
	    	
	    },
	  	complete: function(){ 
	  	},
		success: function(html){
			$("#memorizeOverlay").empty();
			$("#memorizeOverlay").html(html);
			
	    }
   	});
}

function addToList(url, id, title){
	if(url == "alreadyAdded"){
		// Do nothing
	}else{
		$.ajax({ 
		    method: "get",
	    	url: checkUrl,
	    	data: "method=ajax&action=add&url="+ url +"&pageid="+ id +"&title="+ title,
			success: function(html){
				adjustPosition('add', html, id);
				$("#memorizeOverlay").empty();
				$("#memorizeOverlay").html(html);
				
		    }
		});
	}
}

function deleteFromList(id){
	$.ajax({ 
	    method: "get",
	    url: checkUrl,
	    data: 'method=ajax&action=delete&did='+ id +'',
	    beforeSend: function(){
	    },
	  	complete: function(){ 
	  	},
		success: function(html){
			adjustPosition('delete',html);
			$("#memorizeOverlay").empty();
			$("#memorizeOverlay").html(html);
			
	    }
   	});
}

function adjustPosition(mode, html, id){
	var count = parseInt(count);
	if(html.search(/overlayPageLinks/gi) == -1){
		count = 0;
	}else{
		count = html.match(/overlayPageLinks/gi).length;
	}
	
	var footerHeight = $('.footerWrap').height();
	var pageHeight = $(".wrap").eq(0).height();
	var overlayHeight = $('#memorizeOverlay').height();;
	var overlayPosition = 0;
	
	switch(mode){
		case 'add':
			var oldPosition = $('#memorizeOverlay').css("top");
			var linkHeight = $(".overlayPageLinks").eq(0).height();
			
			if(linkHeight == null){
				linkHeight = 16;
			}
			linkHeight = parseInt(linkHeight) + 3;
			oldPosition = parseInt(oldPosition.replace(/px/g, ""));
			overlayPosition = oldPosition - linkHeight;
			$('#memorizeOverlay').css('top',overlayPosition);
			break;
		
		case 'delete':
			var oldPosition = $('#memorizeOverlay').css("top");
			oldPosition = parseInt(oldPosition.replace(/px/g, ""));
			var linkHeight = parseInt($(".overlayPageLinks").eq(0).height()) + 3;
			if(count == '0'){
				overlayPosition = oldPosition + linkHeight + 2;
			}else{
				overlayPosition = oldPosition + linkHeight;
			}
			
			$('#memorizeOverlay').css('top',overlayPosition);
			break;
	}
}
