var g_nIntervalTooltip = 0;
// TODO: prevent ajax form submit from clicking too many times
var g_nAjaxLoading = 0;

$(document).ready(function() {
	try {
		$("a[rel^='prettyPopin']").prettyPopin({modal:true});
	}catch(e){}
	
	setTimeout(updateFooter, 1000);
	$(window).scroll(updateFooter);
	$(window).resize(updateFooter);
	//setTimeout(function() {
		//swfobject.getObjectById('wws_flash').updateBookmark();
	//}, 2000);
});
function updateFooter()
{
	// set footer
	var nTmp = $(window).height()-$('#footer').height();
	nTmp+=$(window).scrollTop();
	$('#footer').css('top', nTmp+'px');
}
function loadSearch(szSearch, nType)
{
	if(szSearch==''||szSearch=='null') {alert('Nothing to search'); return false; }
	szSearch = myURL.encode(szSearch);
	$('#iframe_result').css('display', 'block');
	if(nType==0) {
		if(szSearch.indexOf('search.php')==0) {
			$('#iframe_result').attr('src', myURL.decode(szSearch));
		}else {
			$('#iframe_result').attr('src', 'search.php?rnow=1&q='+szSearch);
		}
	}else if(nType==1) {
		$('#iframe_result').attr('src', 'search.php?rnow=1&my=1&q='+szSearch);
	}else if(nType==2) {
		$('#iframe_result').height(1600);
		$('#iframe_result').attr('src', 'http://www.google.com/search?q='+szSearch);
	}else if(nType==3) {
		$('#iframe_result').height(1600);
		$('#iframe_result').attr('src', 'http://search.yahoo.com/search?p='+szSearch);
	}else
		$('#iframe_result').attr('src', 'search.php');
	
	$('#iframe_result').unbind();
	$.scrollTo('#iframe_result');
}
function loadSearch2(szSearch)
{
	$('#iframe_result').css('display', 'block');
	$('#iframe_result').attr('src', szSearch);
	$('#iframe_result').unbind();
	$.scrollTo('#iframe_result');
}
function openAllFav()
{
	$('#footer_icons').find('a').each(function() {
		var szHREF = $(this).attr('href');
		if(szHREF.indexOf('javascript:')==0) return;
		
		window.open($(this).attr('href'), '_blank');
	});
}

function addBookmark(nID)
{
	var szHREF = $('#popinUpdateURL').attr('baseurl');
	szHREF += '?bkid='+nID;
	$('#popinUpdateURL').attr('href', szHREF);
	$('#popinUpdateURL').click();
}

var myURL = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
function loadPopin(szLink)
{
	$('#'+szLink).click();
}
function readjustIFrame(nHeight)
{
	if(nHeight<=800) return;
	$('#iframe_result').height(nHeight);
}
function scrollToTop()
{
	$.scrollTo('#iframe_result');
}

// flash functions
function flash_updateCategory(nIndex, nState)
{
	if(nState==0) {
		alert('Please click "Edit" Button on your right for adding and editing your bookmarks. Thank you.');
		return;
	}
	var szHREF = $('#popinUpdateCategory').attr('baseurl');
	szHREF += '?id='+nIndex;
	$('#popinUpdateCategory').attr('href', szHREF);
	$('#popinUpdateCategory').click();
}
function flash_updateURL(nCatID, nIndex, nState, szURL, nBKID)
{
	if(nState==0) {
		alert('Please click "Edit" Button on your right for adding and editing your bookmarks. Thank you.');
		return;
	}
	var szHREF = $('#popinUpdateURL').attr('baseurl');
	szHREF += '?cid='+nCatID+'&oid='+nIndex;
	if(szURL!=undefined) szHREF += '&url='+szURL;
	if(nBKID!=undefined) szHREF += '&bkid='+nBKID;
	$('#popinUpdateURL').attr('href', szHREF);
	$('#popinUpdateURL').click();
}
function flash_deleteURL(nCatID, nIndex, nState, szURL)
{
	if(nState==0) {
		alert('Please click "Edit" Button on your right for adding and editing your bookmarks. Thank you.');
		return;
	}
	
	if(confirm('Are you sure you want to remove this bookmark?')==false) return;
	var szHREF = $('#popinUpdateURL').attr('baseurl');
	szHREF += '?manid=2&cid='+nCatID+'&oid='+nIndex;
	if(szURL!=undefined) szHREF += '&url='+szURL;
	$('#popinUpdateURL').attr('href', szHREF);
	$('#popinUpdateURL').click();
}
function flash_updateFav(nCatID, nIndex, szURL)
{
	var szHREF = $('#popinUpdateFav').attr('baseurl');
	szHREF += '?cid='+nCatID+'&oid='+nIndex;
	if(szURL!=undefined) szHREF += '&url='+szURL;
	$('#popinUpdateFav').attr('href', szHREF);
	$('#popinUpdateFav').click();
}
function flash_updateBookmark()
{
	$('#innerPopup').css('display', 'none');
	swfobject.getObjectById('wws_flash').wws_updateBookmark();
}
function flash_addToFavourite(szUser)
{
	var myTitle = "Where Web Start";
	var myURL = "http://www.wherewebstart.com"+szUser;
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(myTitle, myURL, "");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( myURL, myTitle );
	}	else {
		alert("Please use Ctrl-D to bookmark your website. Thank you.");
	}
}
function flash_updateBG(nIdx)
{
	if(nIdx==0||nIdx==undefined) nIdx = 7;
	$('body').css('background-image', "url('images/ba"+nIdx+".jpg')");
}
function flash_showFooter(nShow)
{
	if(nShow==1) {
		// load in favourites
		$('#footer_icons').html('');
		$.ajax({url: 'showfavourites.php',
			type: 'GET',
			cache: false,
			dataType: 'html',
			timeout: 20000,
			error: function() {
			},
			success: function(data){
				$('#footer_icons').html(data);
				$('#footer_icons').find('img').each(function() {
					$(this).unbind();
					$(this).hover(function() {
						var nPos = $(this).offset();
						var nLeftAdj = -1*50;
						$('#tooltip').css('left', (nLeftAdj+nPos.left)+'px');
						$('#tooltip').css('top', (nPos.top-25)+'px');
						$('#tooltip').html($(this).attr('mytitle'));
						clearTimeout(g_nIntervalTooltip);
						$('#tooltip').fadeIn();
					},
					function() {
						clearTimeout(g_nIntervalTooltip);
						g_nIntervalTooltip = setTimeout(function() {$('#tooltip').fadeOut();}, 3000);
					});
				});
				updateFooter();
			}
		});
		
		$('#footer').fadeIn();
	}else $('#footer').fadeOut();
}
function flash_loadMenu()
{
	$('#container_menu').css('display', 'block');
	initsidebarmenu();
	$('#adbrite').css('display', 'block');
}
function flash_updateBookmarkOffset(nOffset)
{
	$.ajax({url: 'boffset.php?os='+nOffset,
		type: 'GET',
		cache: false,
		dataType: 'html',
		timeout: 1000,
		error: function() {
		},
		success: function(data){
		}
	});
}
function flash_addCategory(nCID)
{
	var szHREF = $('#popinAddAllCat').attr('baseurl');
	szHREF += '?cid='+nCID;
	$('#popinAddAllCat').attr('href', szHREF);
	$('#popinAddAllCat').click();
}
function flash_openInnerFrame(szLink)
{
	$('#innerPopup').css('display', 'block');
	$('#innerPopup').attr('src', szLink);
}
function flash_closeInnerFrame()
{
	$('#innerPopup').css('display', 'none');
	swfobject.getObjectById('wws_flash').wws_showCategory();
}
function loadPopular(nID)
{
	swfobject.getObjectById('wws_flash').wws_hideCategory();
	$('#innerPopup').css('display', 'block');
	var szHREF = 'popular.php?rnow=1';
	if(nID!=undefined)
		szHREF += '&catid='+nID;
	
	$('#innerPopup').attr('src', szHREF);
}
function getIFrameLeftPos()
{
	return $('#topcontainer').offset().left;
}
