function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function fStart() {
	//var myTabs = $('myTabs');
	window.activeLink = $('liMain');
	window.activeTab = $('tabMain');
	var myTabs = ['liMain', 'liAbout', 'liStats', 'liTagcloud', 'liWebmasters', 'liLinks'];
	for	 (var i = 0; i < myTabs.length; i++) {
		var liObj = $(myTabs[i]);
		//alert(liObj.title);

		if (i > 0) {
			var divName = 'tab'+liObj.title;
			$(divName).style.display = 'none';
		}

		liObj.addEvent("click", function () {
			activateTab(this);
		});
	}

	// set current height (100%) in pixels
	onWindowResize();
	//window.addEvent('resize', onWindowResize);

	// after all is resized
	var preview = $('preview');
	//alert(preview.clientWidth);
	createCookie('preview', preview.clientWidth+'x'+preview.clientHeight, 365);
}

function activateTab(self) {
	if (window.activeTab != self && self.className != 'active') {
		self.className = 'active';
		window.activeLink.className = '';
		window.activeLink = self;

		var divName = 'tab'+self.title;
		//alert(divName);
		var divObj = $(divName);
		divObj.style.display = '';
		window.activeTab.style.display = 'none';
		window.activeTab = divObj;
	}

/*			if (divName == 'tabStats') {
		//alert("stats");
		new Request.HTML({
			url: "",
			update: divObj
		}).get({
			mod: 'stats'
		});
	}
*/
}


function onWindowResize() {
/*	$('tabMain').style.height = (
		parseInt(window.getHeight())-
		parseInt($('title').getHeight())-
		parseInt($('myTabs').getHeight()+14)
	)+'px';
	$('results').style.height = ($('results').getHeight()-4)+'px';
	$('strip').style.height = ($('strip').getHeight()-4)+'px';
	$('preview').style.height = ($('preview').getHeight()-4)+'px';
*/
	//alert($('widestTable').getHeight());
/*
	$('body').style.height = '100%';
	$('widestTable').style.height = '100%';
	$('tabMain').style.height = '100%';
	// sleep(1);
	$('body').style.height = $('body').getHeight()+'px';
	$('widestTable').style.height = $('widestTable').getHeight()+'px';
	$('tabMain').style.height = $('tabMain').getHeight()+'px';
*/
/*	alert(Json.toString({
		body: $('body').style.height,
		widestTable: $('widestTable').style.height,
		tabMain: $('tabMain').style.height
	}));
*/
	$('widestTable').style.height = window.innerHeight+'px';
	var height = window.innerHeight-$('title').getHeight()-$('menu').getHeight()-10;
/*	console.log([
		window.innerHeight,
		$('title').getHeight(),
		$('menu').getHeight(),
		-10,
		height
	]);
*/	$('results').style.height = height+'px';
	$('strip').style.height = height+'px';
	$('preview').style.height = height+'px';
	height -= 15;
	$('tabAbout').style.height = height+'px';
	$('tabStats').style.height = height+'px';
	$('tabTagcloud').style.height = height+'px';
	$('tabWebmasters').style.height = height+'px';
	$('tabLinks').style.height = height+'px';
}

function addTagQuestion(fid) {
	var ask = prompt('Enter new tag:');
	if (ask) {
		new Request.HTML({
			url: '',
			update: $('strip')
		}).get({
			mod: 'showStrip',
			fid: fid,
			addTag: ask
		});
	}
	return false;
}

function startPlay(fid, index) {
	createCookie('play', true, 365);
	window.g_play = setTimeout(function () {
		new Request.HTML({
			url: '',
			update: $('preview')
		}).get({
			mod: 'showPreview',
			fid: fid,
			index: index
		});
	}, 5000);
}

function stopPlay() {
	eraseCookie('play');
	if (window.g_play) {
		clearTimeout(window.g_play);
	}
}

function continuePlay(fid, index) {
	if (readCookie('play')) {
		startPlay(fid, index);
	}
}

function ajaxRequest(request, params) {
	var div = request.update;
	div.className += " transparent";
	new Request.HTML(request).get(params).addEvent('onSuccess', function(tree, elements, html, javascript) {
		div.innerHTML = html;
		div.className = div.className.replace('transparent', '');
		//alert(div.className);
	});
}

function bookmarkMe(self, fid) {
	var bookmarks = readCookie('bookmarks');
	if (bookmarks) {
		bookmarks += ',';
	} else {
		bookmarks = '';
	}
	bookmarks += fid;
	createCookie('bookmarks', bookmarks, 365);
	self.className = 'staron';
	self.onclick = function () {
		unBookmarkMe(self, fid);
	}
}

function unBookmarkMe(self, fid) {
	var bookmarks = readCookie('bookmarks');
	if (bookmarks) {
		bookmarks = bookmarks.split(',');
		var pos = bookmarks.indexOf(''+fid+'');
		if (pos > -1) {
			/*delete bookmarks[pos];
			bookmarks.filter(function (e) {
				return e ? true : false;
			});*/
			bookmarks.splice(pos, 1);
			bookmarks = bookmarks.join(',');
			createCookie('bookmarks', bookmarks, 365);
			self.className = 'staroff';
			self.onclick = function () {
				bookmarkMe(self, fid);
			}
		}
	}
}

var replaceBanner = function () {
	var ifrm = $('bannerFrame');
	//alert(ifrm);
	//.write('<scr'+'ipt src="../js/includeTop.js"></scr'+'ipt>');
	ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
	ifrm.document.open();
	ifrm.document.write($('bannerCode').innerHTML.replace(/scr\+ipt/g, 'script'));
	ifrm.document.close();
	//alert($('bannerCode').innerHTML);
	//alert($('bannerCode').innerHTML.replace(/scr\+ipt/g, 'script'));
}
