
function BookmarkItem( id, title, url, time, contentID)
{
	this.Id = id;
	this.Title = title;
	this.Url = url;
	this.Time = time;
	this.ContentID = contentID;
	this.ContainingElement = 0;
	this.EditMode = false;
	
	this.EscapeHTML = function (Str)
  /* returns Str with all characters with special HTML meanings converted to
    entity references. */
  {
    var Escaped = ""
    for (var i = 0; i < Str.length; ++i)
      {
        var ThisCh = Str.charAt(i)
        if (ThisCh == "&")
          {
            ThisCh = "&amp;"
          }
        else if (ThisCh == "<")
          {
            ThisCh = "&lt;"
          }
        else if (ThisCh == "\"")
          {
            ThisCh = "&quot;"
          }
        else if (ThisCh == ">")
          {
            ThisCh = "&gt;"
          } /*if*/
        Escaped += ThisCh
      } /*for*/
    return Escaped
  } /*EscapeHTML*/
  
	this.Draw = function()
	{
		if ( 0 == this.ContainingElement)
		{
			this.ContainingElement = document.createElement( "div");
		}
		var bc = this.ContainingElement;
		bc.innerHTML = '';
		bc.className = "BMItem";		
		
		var markup = '';
		markup += '<div style="float: right; position: relative;">';
		if ( this.EditMode)
		{
			markup += (Bookmarks.CanGoUp( this.Id)) 
				? '<a href="javascript:Bookmarks.MoveUp('+this.Id+')"><img src="/images/bookmarks/link_up_12x18.gif" alt="hore" border="0" /></a>'
				: '<img src="/images/bookmarks/link_up_off_12x18.gif" alt="hore" border="0" />';
			markup += (Bookmarks.CanGoDown( this.Id))
				? '<a href="javascript:Bookmarks.MoveDown('+this.Id+')"><img src="/images/bookmarks/link_down_12x18.gif" alt="dolu" border="0" style="margin-right: -7px;" /></a>'
				: '<img src="/images/bookmarks/link_down_off_12x18.gif" alt="dolu" border="0" style="margin-right: -7px;" />';
		}
		else
		{
      markup += '<div id="IBDelete'+this.Id+'" class="tooltip" style="position:absolute; right:-200px; top:-30px; display:none;"><img src="/images/soc_siete/soc_siete_top.png" alt="" /><br /><div class="inside_tooltip"><div style="width:200px;">Vymazať stránku zo zoznamu</div></div><img src="/images/soc_siete/soc_siete_bottom.png" alt="" /><br /></div>';
			markup += '<a href="javascript:Bookmarks.Remove('+this.Id+')"><img src="/images/bookmarks/link_delete_15x15.gif" alt="zmazať" border="0" style="margin: 3px 0 0 0;" onmouseout="Bookmarks.showHideIB(\'IBDelete'+this.Id+'\')" onmouseover="Bookmarks.showHideIB(\'IBDelete'+this.Id+'\')" /></a>';
			markup += '<div id="IBEdit'+this.Id+'" class="tooltip" style="position:absolute; right:-215px; top:-30px; display:none;"><img src="/images/soc_siete/soc_siete_top.png" alt="" /><br /><div class="inside_tooltip"><div style="width:200px;">Upraviť názov stránky v zozname</div></div><img src="/images/soc_siete/soc_siete_bottom.png" alt="" /><br /></div>';
			markup += '<a href="javascript:Bookmarks.EditMode('+this.Id+',true)"><img src="/images/bookmarks/link_edit_15x15.gif" alt="upraviť" border="0" style="margin: 3px -5px 0 1px;" onmouseout="Bookmarks.showHideIB(\'IBEdit'+this.Id+'\')" onmouseover="Bookmarks.showHideIB(\'IBEdit'+this.Id+'\')" /></a>';
		}
		markup += '</div>';

		markup += '<div style="float: left; width:250px; padding-top: 3px; position: relative;">';
		if ( this.EditMode)
		{
      markup += '<div id="IBEnter'+this.Id+'" class="tooltip" style="position:absolute; right:-230px; top:-33px; display:none; z-index: 999;"><img src="/images/soc_siete/soc_siete_top.png" alt="" /><br /><div class="inside_tooltip"><div style="width:200px;">Potvrdiť pridanie stránky</div></div><img src="/images/soc_siete/soc_siete_bottom.png" alt="" /><br /></div>';
      markup += '<input id="BMEditMode'+this.Id+'" type="text" value="'+this.EscapeHTML(this.Title)+'" onkeyup="Bookmarks.Keypress(this,event,'+this.Id+')" style="width: 200px;" />';
			markup += '<img src="/images/bookmarks/enter.gif" border="0" style="float: right; position: relative; right: -20px; cursor:pointer;" onclick="Bookmarks.EditTitle(document.getElementById(\'BMEditMode'+this.Id+'\'),'+this.Id+');" onmouseout="Bookmarks.showHideIB(\'IBEnter'+this.Id+'\')" onmouseover="Bookmarks.showHideIB(\'IBEnter'+this.Id+'\')" />';
		}
		else
		{
			markup += '<span>'+this.EscapeHTML(this.Time)+'</span>&nbsp;&nbsp;<a href="'+this.EscapeHTML(this.Url)+'" class="linkSlot" style="display: inline;">'+this.EscapeHTML(this.Title)+'</a>';
		}
		markup += '</div>';
		markup += '<div class="clear"><!-- --></div>';
		bc.innerHTML = markup
	}
}

function BookmarkControl( dst_element, default_title)
{
	this.Bookmarks = new Array();
	this.ContainingElement = dst_element;
	this.DefaultTitle = default_title;
	this.VisibleItems = 5;
	this.Offset = 0;
	this.Loading = false;
	this.GetNewId = function()
	{
		var new_id = 0;
		do 
		{
			new_id ++;
			var taken = false;
			for ( var i = 0; i < this.Bookmarks.length; i++)
			{
				if ( this.Bookmarks[i].Id == new_id)
				{
					taken = true;
				}
			}
		}
		while (true == taken);
		return new_id;
	}
	this.Add = function( title, url, time, contentID)
	{
    if (!time)
    {
      var currentTime = new Date();
      var month = currentTime.getMonth() + 1;
      var day = currentTime.getDate();
      var year = currentTime.getFullYear();
      var hours = currentTime.getHours();
      var minutes = currentTime.getMinutes();
      if (minutes < 10) var minutes = "0" + minutes;
      var time = day+'. '+month+'. '+year+' - '+hours+':'+minutes;
    }
    
		var item = new BookmarkItem( this.GetNewId(), title, url, time, contentID);
		this.Bookmarks[this.Bookmarks.length] = item;
		this.OffsetFocusViewportOn( item.Id);
		if ( false == this.Loading)
		{
			this.Draw();
			this.SaveToCookie();
		}
	}
	this.Remove = function( id)
	{
		var pom = new Array();
		for ( var i = 0; i < this.Bookmarks.length; i++)
		{
			if ( id != this.Bookmarks[i].Id)
			{
				pom[pom.length] = this.Bookmarks[i];
			}
		}
		this.Bookmarks = pom;
		this.Draw();
		this.SaveToCookie();
	}
	this.EditMode = function( id, option)
	{
		for ( var i = 0; i < this.Bookmarks.length; i++)
		{
			if ( id == this.Bookmarks[i].Id)
			{
				this.Bookmarks[i].EditMode = option;

			}
			else
			{
				if ( this.Bookmarks[i].EditMode)
					this.Bookmarks[i].EditMode = false;
			}
		}
		this.Draw();
	}
	this.showHideIB = function(el)
	{
    if (document.getElementById(el))
      document.getElementById(el).style.display = (document.getElementById(el).style.display == 'block') ? 'none' : 'block';
  }
	this.Draw = function()
	{
		var markup = '';
		markup += '<img src="/images/bookmarks/navigation_add.gif" alt="+" border="0" />&nbsp;<a href="javascript:Bookmarks.ToggleAdd()">Pridať stránku</a>';
		markup += '<div id="BMAddBox" style="display:none; position:relative;"><input type="text" value="" id="BMAddText" onkeypress="Bookmarks.KeypressAdd(this,event)" style="width: 250px;" />';
    markup += '<div id="IBEnter'+this.Id+'" class="tooltip" style="position:absolute; right:-220px; top:-33px; display:none; z-index: 999;"><img src="/images/soc_siete/soc_siete_top.png" alt="" /><br /><div class="inside_tooltip"><div style="width:200px;">Potvrdiť pridanie stránky</div></div><img src="/images/soc_siete/soc_siete_bottom.png" alt="" /><br /></div>';
    markup += '<img src="/images/bookmarks/enter.gif" border="0" style="float: right; position: relative; right: -5px; cursor:pointer;" onclick="Bookmarks.ToggleAdd();Bookmarks.Add( document.getElementById(\'BMAddText\').value, document.location.href, false, IDcontent);" onmouseout="Bookmarks.showHideIB(\'IBEnter'+this.Id+'\')" onmouseover="Bookmarks.showHideIB(\'IBEnter'+this.Id+'\')" />';

    markup += '<div class="clear"><!-- --></div>';
    markup += '</div>';
		this.ContainingElement.innerHTML = markup;		

    markup = '';
		markup += '<div class="serviceTopDottedSpacer" ></div>';

		if ( 0 < this.Bookmarks.length)
		{
			if ( this.OffsetCanGoUp())
				markup += '<div style="float: right; clear: both; position: relative; right: -7px;"><a href="javascript:Bookmarks.OffsetMoveUp();"><img src="/images/bookmarks/link_up.gif" border="0" alt="" /></a></div>';
			else
				markup += '<div style="float: right; clear: both; position: relative; right: -7px;"><img src="/images/bookmarks/link_up_off.gif" border="0" alt="" /></div>';
			this.ContainingElement.innerHTML += markup;
			for ( var i = this.Offset; i < Math.min(this.Bookmarks.length,this.Offset+this.VisibleItems); i++)
			{
				this.Bookmarks[i].Draw();
				this.ContainingElement.appendChild( this.Bookmarks[i].ContainingElement);
			}
			markup = '';
			if ( this.OffsetCanGoDown())
				markup += '<div style="float: right; clear: both; position: relative; right: -7px;"><a href="javascript:Bookmarks.OffsetMoveDown();"><img src="/images/bookmarks/link_down.gif" border="0" alt="" /></a></div>';
			else
				markup += '<div style="float: right; clear: both; position: relative; right: -7px;"><img src="/images/bookmarks/link_down_off.gif" border="0" alt="" /></div>';
			this.ContainingElement.innerHTML += markup;

      this.ContainingElement.innerHTML += '<div style="height: 5px;"></div><a href="/zapamatane-stranky" class="closerLink" rel="nofollow">Zobraziť všetky</a>';
		}
		else
		{
      markup += '<div style="padding-top: 8px;">Momentálne nemáte v zozname žiadne stránky.</div>';
			this.ContainingElement.innerHTML += markup;    
    }
		
		setTimeout( "Bookmarks.DelayedFocus()", 100);
	}
	this.DelayedFocus = function()
	{
		for ( var i = this.Offset; i < Math.min(this.Bookmarks.length,this.Offset+this.VisibleItems); i++)
		{
			if ( this.Bookmarks[i].EditMode)
			{
				var ele = document.getElementById( "BMEditMode"+this.Bookmarks[i].Id);
				ele.focus();
				this.SetSelection( ele);
			}
		}
	}
	this.SetSelection = function( ele)
	{
		if (ele.createTextRange)
		{
			var range = ele.createTextRange();
			range.collapse(true);
			range.moveStart('character', 0);
			range.moveEnd('character', ele.value.length);
			range.select();
		}
		else if (ele.selectionEnd)
		{
			ele.selectionStart = 0;
			ele.selectionEnd = 0 + ele.value.length;
		}				
	}
	this.Toggle = function()
	{
		this.ContainingElement.style.display = (this.ContainingElement.style.display == 'none')?'block':'none';
		this.SaveToCookie();
	}
	this.CanGoUp = function( id)
	{
		var result = false;
		if ( 0 < this.Bookmarks.length)
		{
			result = (id != this.Bookmarks[0].Id);
		}
		return result;
	}
	this.CanGoDown = function( id)
	{
		var result = false;
		if ( 0 < this.Bookmarks.length)
		{
			result = (id != this.Bookmarks[this.Bookmarks.length-1].Id);
		}
		return result; 
	}
	this.FindIndex = function ( id)
	{
		var result = -1;
		for ( var i = 0; i < this.Bookmarks.length; i++)
		{
			if ( id == this.Bookmarks[i].Id)
			{
				result = i;
			}
		}
		return result;
	}
	this.MoveUp = function( id)
	{
		if ( this.CanGoUp( id))
		{
			var i = this.FindIndex( id);
			if ( -1 != i)
			{
				var pom = this.Bookmarks[i-1];
				this.Bookmarks[i-1] = this.Bookmarks[i];
				this.Bookmarks[i] = pom;
				this.OffsetFocusViewportOn( id);
				this.Draw();
			}
		}
	}
	this.MoveDown = function( id)
	{
		if ( this.CanGoDown( id))
		{
			var i = this.FindIndex( id);
			if ( -1 != i)
			{
				var pom = this.Bookmarks[i+1];
				this.Bookmarks[i+1] = this.Bookmarks[i];
				this.Bookmarks[i] = pom;
				this.OffsetFocusViewportOn( id);
				this.Draw();
			}
		}
	}
	this.Keypress = function( ele, e, id)
	{
		var key = 0;
		var title = ele.value;
		if ( window.event)
			key = window.event.keyCode;
		else
			key = e.which;
		switch ( key)
		{
		case 13:
			i = this.FindIndex( id);
			if ( -1 != i)
				this.Bookmarks[i].Title = title;
			this.EditMode( id, false);
			this.SaveToCookie();
			return false;
			break;
		case 27:
			this.EditMode( id, false);
			return false;
			break;
		case 38:
			this.MoveUp( id);
			return false;
			break;
		case 40:
			this.MoveDown( id);
			return false;
			break;
		}
		return true;
	}
	this.EditTitle = function(ele, id)
	{
    var title = ele.value;
    i = this.FindIndex( id);
		if ( -1 != i)
			this.Bookmarks[i].Title = title;
		this.EditMode( id, false);
		this.SaveToCookie();
		return false;
  }
	this.ToggleAdd = function()
	{
		var ele = document.getElementById("BMAddBox");
		var pom = ele.getElementsByTagName( "input");
		var ib = pom[0];
		if ( "none" == ele.style.display)
		{
			ele.style.display = "block";
			ib.value = this.DefaultTitle;
			ib.focus();
			this.SetSelection( ib);
		} 
		else 
		{
			ele.style.display = "none";
		}
	}
	this.KeypressAdd = function( ele, e)
	{
		var key = 0;
		if ( window.event)
			key = window.event.keyCode;
		else
			key = e.which;
		switch ( key)
		{
		case 13:
			this.ToggleAdd();
			this.Add( ele.value, document.location.href, false, IDcontent);
			return false;
			break;
		case 27:
			this.ToggleAdd();
			return false;
			break;
		}
		return true;
	}
	this.OffsetCanGoUp = function()
	{
		var result = 0 < this.Offset;
		return result;
	}
	this.OffsetCanGoDown = function()
	{
		var result = this.Offset < Math.max( 0, this.Bookmarks.length-this.VisibleItems);
		return result;
	}
	this.OffsetFocusViewportOn = function( id)
	{
		var i = this.FindIndex( id);
		if ( -1 != i)
		{
			if ( i < this.Offset)
			{
				this.Offset = i;
			}
			else if ( (this.Offset + this.VisibleItems) <= i)
			{
				this.Offset = i - this.VisibleItems + 1;
			}
		}
	}
	this.OffsetMoveUp = function()
	{
		if ( this.OffsetCanGoUp())
		{
			this.Offset --;
			this.Draw();
			this.SaveToCookie();
		}
	}
	this.OffsetMoveDown = function()
	{
		if ( this.OffsetCanGoDown())
		{
			this.Offset ++;
			this.Draw();
			this.SaveToCookie();
		}
	}
	this.SaveToCookie = function()
	{
		var val = '';
		for ( var i = 0; i < this.Bookmarks.length; i++)
		{
			if ( i != 0)
				val += ';';
			var b = this.Bookmarks[i];
			val += encodeURIComponent(b.Title)+','+encodeURIComponent(b.Url)+','+encodeURIComponent(b.Time)+','+b.ContentID;
		}
		setCookie( "BMBookmarks", val, new Date( 2050, 5, 28));
		var val2 = '';
		val2 = this.ContainingElement.style.display + ',' + this.Offset;
		setCookie( "BMState", val2, new Date( 2050, 5, 28));
	}
	this.LoadFromCookie = function()
	{
		var val = getCookie( "BMBookmarks");
		if ( 0 < val.length)
		{
			this.Loading = true;
			var bookmarks = val.split( ";");
			for ( var i = 0; i < bookmarks.length; i++)
			{
				if ( 0 < bookmarks[i].length)
				{
					var bookmark = bookmarks[i].split( ",");
					this.Add( decodeURIComponent(bookmark[0]), decodeURIComponent(bookmark[1]), decodeURIComponent(bookmark[2]), bookmark[3]);
				}
			}
			var val2 = getCookie( "BMState");
			var state = val2.split( ",");
			if ( "block" == state[0])
			{
				this.ContainingElement.style.display = "block";
        element = document.getElementById('flap_rychla_navigacia');
        theDiv = element.childNodes;
        theDiv[0].style.background = 'url(/images/default/double-arrow-top.gif) no-repeat left top';
        theDiv[0].style.backgroundPosition = '7px 7px';
			}
			else
			{
				this.ContainingElement.style.display = "none";
			}
			if ( (0 < state[1]) && (state[1] < Math.max(0, this.Bookmarks.length-this.VisibleItems)))
				this.Offset = state[1];
			this.Loading = false;
		}
	}
}

