var timer = new Timer();
var bookmarkSet = 0;
var i;
var params;

$(document).load(function() {
});

function HandleGoogleSearch(visible)
{
	if(visible)
	{
		$('.searchinput').fadeIn(500);
		$('.googleicon').fadeIn(500);
	}
	else
	{
		$('.searchinput').fadeOut(1000);
		$('.googleicon').fadeOut(1000);
	}
}

function HandleContentFrame(frameClass, visible)
{
	if(visible)
	{
		timer = setTimeout(function() { $('.'+frameClass).fadeIn(500); }, 250);
	}
	else
	{
		clearTimeout(timer);
		$('.'+frameClass).fadeOut(125);
	}
}

function HandleInputFrame(frameId, visible, bookmarkSetId)
{
	bookmarkSet = bookmarkSetId; // needed for add bookmarks to correct bookmark set
	if(visible)
	{
		$("#"+frameId).fadeIn(500);
	}	
	else
	{
		$("#"+frameId).fadeOut(250);
		$("#thumbframearea").load("bookmarkarea.php?editmode=1");
	}
}


function ActivateBox(searchbox, value)
{
	document.getElementById(searchbox).style.color = '#333';
	if(document.getElementById(searchbox).value == value)
	{
		document.getElementById(searchbox).value = '';
	}
}
function DeactivateBox(searchbox, value)
{
	if(document.getElementById(searchbox).value == '')
	{
		document.getElementById(searchbox).style.color = '#AAA';
		document.getElementById(searchbox).value = value;
	}
}

// ajax
function AddBookmark()
{
	if(document.getElementById('inputmain1').value != "http://")
	{
		params = 'url='+document.getElementById('inputmain1').value;
		params += '&title='+document.getElementById('inputmain11').value;
		params += '&bookmarkSet='+bookmarkSet;
		$.post("add.php?add=bookmark&"+params);
		$("#thumbframearea").load("bookmarkarea.php?editmode=1");
		IndicateSuccess(1);
	}
}
function AddBookmarkSet()
{
	if(document.getElementById('inputmain2').value.length > 0)
	{
		params = 'nameofset=';
		params += document.getElementById('inputmain2').value;
		$.post("add.php?add=bookmarkset&"+params);
		$("#thumbframearea").load("bookmarkarea.php?editmode=1");
		IndicateSuccess(2);
	}
}

function RemoveBookmark(bookmarkId)
{
	$('.bookmark'+bookmarkId).fadeTo(500, 0.01);
	$.post("add.php?removeBookmark="+bookmarkId);
}
function RemoveBookmarkSet(bookmarkSetId)
{
	if(confirm("Really remove BookmarkSet"))
	{
		$('#bookmarkSet'+bookmarkSetId).fadeTo(500, 0.01);
		$.post("add.php?removeBookmarkSet="+bookmarkSetId);
	}
}
function EditFramedBookmarks()
{
	HandleInputFrame('inputframe3', 1, 0);
}
function UpdateFramedBookmarks()
{
	params = '';
	for(i = 1; i <= 6; i++)
	{
		params += 'frame'+i+'='+document.getElementById('frameUrl'+i).value+'&';
		params += 'frameTitle'+i+'='+document.getElementById('frameTitle'+i).value+'&';
	}
	$.post("add.php?updateframes=1&"+params);
	$("#thumbframearea").load("bookmarkarea.php?editmode=1");
	
	IndicateSuccess(3);
	setTimeout(function() { HandleInputFrame('inputframe3', 0, 0); }, 1500);
}
// *ajax

function IndicateSuccess(id)
{
	$('#inputbutton'+id).fadeOut(250);
	$('#inputbutton'+id).fadeIn(250);
	$('#tick'+id).delay(500);
	$('#tick'+id).fadeTo(500, 1);
	$('#tick'+id).delay(500);
	$('#tick'+id).fadeTo(500, 0.01);
	if(id == 1)
	{
		setTimeout(function() { document.getElementById('inputmain1').value = 'http://'; }, 1500);
		setTimeout(function() { document.getElementById('inputmain11').value = 'Title (Optional)'; }, 1500);
		setTimeout(function() { document.getElementById('inputmain11').style.color = '#AAA'; }, 1500);
	}
	else
	{
		setTimeout(function() { document.getElementById('inputmain2').value = 'My bookmarks'; }, 2000);
		setTimeout(function() { HandleInputFrame('inputframe2', 0, 0); }, 1500);
	}
}

function HandleImage(view, imgID)
{
	if(view)
	{
		document.getElementById(imgID).style.visibility = "visible";
	}
	else
	{
		document.getElementById(imgID).style.visibility = "hidden";
	}
}

function facebook_logout()
{
	FB.logout(function(response) 
	{
		alert('You are logged out');
	});
}

/* Counter */

var Timer = 0;
function RunLoop()
{
  	clearInterval(Timer);	
  	Timer = setInterval("CounterReload()", 3000);
}

function CounterReload()
{
	$("#counter").fadeTo(1, 0.01);
	$("#counter").load("counter.php");
	$("#counter").fadeTo(500, 1);
}
