//cookie functions
function getCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)   {
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) {
			break;
		}
	}

	return -1;
};

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)  {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
};



function setCookie(NameOfCookie, value, expirehours) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expirehours * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expirehours == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/;" ;
};

function setCookieMinutes(NameOfCookie, value, expirehours) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expirehours * 60 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expirehours == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/;" ;
};

var myfontsize = 12;
var myfontstyle = 'Verdana';
var myfontwidth = '99';

var tmp_myfont = getCookie('myfont');
if(tmp_myfont.length >= 3) {
	var tmp_split = tmp_myfont.split(';', 3);
	
	if(tmp_split.length == 3) {		
		if(tmp_split[0].length > 0) {
			myfontstyle = tmp_split[0];
		}
		if(parseInt(tmp_split[1]) > 0) {
			myfontsize = parseInt(tmp_split[1]);
		}
		if(parseInt(tmp_split[2]) > 0 && parseInt(tmp_split[2]) <= 100) {
			myfontwidth = parseInt(tmp_split[2]);
			
			if(myfontwidth > 99) {
				myfontwidth = 99;
			}
		}
	}
}

function updateFontSize(direction) {
	if(direction == 'u' && myfontsize <= 30) {
		myfontsize=myfontsize+2;
	}
	else if(myfontsize >= 8) {
		myfontsize=myfontsize-2;
	}

	if(myfontsize <= 30 && myfontsize >= 8) {
		setCookie('myfont',myfontstyle + ';' + myfontsize + ';' + myfontwidth,3600);
		$('#storytext').css('font-Size',myfontsize+'px');		$('#storytext').css('font-Size',myfontsize+'px');
		$('#storytext').css('line-height',(myfontsize+7)+'px');
	}
};

function updateFontStyle(style) {
	if(style.length > 0) {
		myfontstyle = style;
		setCookie('myfont',myfontstyle + ';' + myfontsize + ';' + myfontwidth,3600);
		$('#storytext').css('font-Family',style);
	}
};

function updateFontWidth(width) {
	width = parseInt(width);
	
	if(width > 0 && width <= 100) {
		myfontwidth = width;
		setCookie('myfont',myfontstyle + ';' + myfontsize + ';' + myfontwidth,3600);
		$('#storytext').css('width',width + '%');
		$('#yazibasbasbolum').css('width',width + '%');
	}
};

function switchLayer(elementId, bolVisible) {
	if (bolVisible == true) {
		$('#'+elementId).show();
	} else {
		$('#'+elementId).hide();
	}
};

function switchPink(elementId, bolVisible) {
	if(bolVisible) {
		$('#l_'+elementId).css('textDecoration','underline');
		$('#l_'+elementId).css('color','darkgreen');
	}
	else  {
		$('#l_'+elementId).css('textDecoration','none');
		$('#l_'+elementId).css('color','blue');
	}

	switchLayer(elementId, bolVisible);
};

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
};

function formatDate(vDate, vFormat){
	var vDay              = vDate.getDate();
	var vMonth            = vDate.getMonth()+1;
	var vYearLong         = vDate.getFullYear();
	var vYearShort        = vDate.getFullYear().toString().substring(2,4);
	var vYear             = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort);
	var vHour             = vDate.getHours();
	var vMinute           = vDate.getMinutes();
	var vSecond           = vDate.getSeconds();
	var vDateString       = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{2,4}/g, vYear);
	vDateString           = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond);
	return vDateString;
};

var array_censors = new  Array( '0','Fiction Rated: K','Fiction Rated: K+','Fiction Rated: T','Fiction Rated: M','Fiction Rated: MA');
var array_genres = new Array( 'None','General','Romance','Humor','Drama','Poetry','Action/Adventure','Mystery','Horror','Parody','Angst','Supernatural','Suspense','Sci-Fi','Fantasy','Spiritual','Tragedy', 'Western', 'Crime', 'Family', 'Hurt/Comfort', 'Friendship');
var array_languages = new Array( '0','English','Spanish','French','German','Chinese','Japanese','Dutch','Portuguese','Scandinavian','Russian','Italian','Bulgarian','Polish','Hungarian','Hebrew','Arabic','Swedish', 'Norwegian', 'Danish','Finnish','Filipino', 'Esperanto', 'Hindi', 'Punjabi', 'Farsi', 'Greek', 'Romanian', 'Albanian', 'Serbian', 'Turkish', 'Czech','Indonesian','Croatian', 'Catalan', 'Latin', 'Korean');
var array_status = new Array( '0', 'In-Progress','Complete');

function story (stitle, stitleu, summary, userid, penname, pennameu, category, storyid, genreid, subgenreid, datesubmit, dateupdate, ratingtimes, chapters, languageid, censorid, wordcount, statusid, verse) {
	this.stitle = stitle;
	this.stitleu = stitleu;

	this.summary = summary;

	this.userid = userid;
	this.penname = penname;
	this.pennameu = pennameu;

	this.category = category;
	this.storyid = storyid;
	this.genreid = genreid;
	this.subgenreid = subgenreid;


	this.datesubmit = new Date();
	this.datesubmit.setTime(datesubmit);
	this.datesubmittext = formatDate(this.datesubmit,'MM-dd-yy');

	this.dateupdate = new Date();
	this.dateupdate.setTime(dateupdate);
	this.dateupdatetext = formatDate(this.dateupdate,'MM-dd-yy');


	this.ratingtimes = ratingtimes;
	this.chapters = chapters;
	this.languageid = languageid;
	this.censorid = censorid;

	this.wordcount = wordcount;

	this.statusid = statusid;
	this.verse = verse;

};

function story_set(myarray, stitle, stitleu, summary, userid, penname, pennameu, category, storyid, genreid, subgenreid, datesubmit, dateupdate, ratingtimes, chapters, languageid, censorid, wordcount, statusid, verse) {
	myarray[myarray.length] = new story(stitle, stitleu, summary, userid, penname, pennameu, category, storyid, genreid, subgenreid, datesubmit, dateupdate, ratingtimes, chapters, languageid, censorid, wordcount, statusid, verse);

};

function sortByCategory(a, b) {
	var x = a.category.toLowerCase();
	var y = b.category.toLowerCase();
	return ((x < y) ? 1 : ((x > y) ? -1 : sortByStitle(a,b)));
};

function sortByStatusid(a, b) {
	var x = a.statusid;
	var y = b.statusid;
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByStitle(a,b)));
};

function sortByVerse(a, b) {
	var x = a.verse.toLowerCase();
	var y = b.verse.toLowerCase();
	return ((x < y) ? 1 : ((x > y) ? -1 : sortByStitle(a,b)));
};

function sortByStitle(a, b) {
	var x = a.stitle.toLowerCase();
	var y = b.stitle.toLowerCase();
	return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};

function sortByDateSubmit(a, b) {
	var x = a.datesubmit.getTime();
	var y = b.datesubmit.getTime();
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByStitle(a,b)));
};

function sortByDateUpdate(a, b) {
	var x = a.dateupdate.getTime();
	var y = b.dateupdate.getTime();
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByStitle(a,b)));
};

function sortByChapters(a, b) {
	var x = a.chapters;
	var y = b.chapters;
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByStitle(a,b)));
};

function sortByReviews(a,b) {
	var x = a.reviews;
	var y = b.reviews;
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByStitle(a,b)));
};

function sortByWords(a,b) {
	var x = a.wordcount;
	var y = b.wordcount;
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByStitle(a,b)));
};

function sortByReviews(a,b) {
	var x = a.ratingtimes;
	var y = b.ratingtimes;
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByStitle(a,b)));
};

function storylist_render(story_array, startrow, show_cat, show_author) {
	var buffer = new Array();
	var story;

	for(i= story_array.length, c = startrow; i > 0; i--, c++) {
		story = story_array[i-1];

		buffer[buffer.length] = c + ". <a  href='/s/" + story.storyid + "/1/" + story.stitleu + "'>";
		buffer[buffer.length] = story.stitle + "</a> ";

		if(story.chapters > 1) {
			buffer[buffer.length] = " <a  style='text-decoration:none;' href='/s/" + story.storyid + "/"+ story.chapters +"/" + story.stitleu + "'>&#187;</a> ";
		}

		if(show_author == 1) buffer[buffer.length] = "by <a href='/u/" + story.userid + "/" + story.pennameu + "'>" + story.penname + "</a> ";

		if(story.ratingtimes > 0)
		buffer[buffer.length] = "<a class=reviews href='/r/" + story.storyid + "/'>reviews</a>";
		buffer[buffer.length] = "<blockquote>" + story.summary + "<br><span class='gray' style='font-size=11px'>";

		if(story.statusid == 2) buffer[buffer.length] =  "Complete - ";

		if(show_cat == 1) buffer[buffer.length] = story.category + " - ";

		buffer[buffer.length] = array_censors[story.censorid] + " - ";
		buffer[buffer.length] = array_languages[story.languageid] + " - ";

		if(story.genreid > 0) {
			buffer[buffer.length] = array_genres[story.genreid];
			if(story.subgenreid > 0) {
				buffer[buffer.length] =  "/" + array_genres[story.subgenreid];
			}
		}
		else if(story.subgenreid > 0) {
			buffer[buffer.length] = array_genres[story.subgenreid] + " - ";
		}

		buffer[buffer.length] = " - Chapters: " + story.chapters + " - Words: " + addCommas(story.wordcount) + " - Reviews: " + story.ratingtimes + " - Updated: " + story.dateupdatetext + " - Published: " + story.datesubmittext + "</span></blockquote>";
	}

	return buffer.join("");

};

function get_html_obj(htmlid) {
	if(document.getElementById) {
		return document.getElementById(htmlid);
	}
	//ie 4
	else if(document.all) {
		return document.all[htmlid];
	}
	else {
		return null;
	}
};

function textCounter(field, maxlimit, htmlid) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}

	$('#'+htmlid).html(maxlimit - field.value.length);
	
};

function storylist_draw ( htmlid, story_array, startrow, show_cat, show_author) {

	var result = storylist_render(story_array, startrow, show_cat, show_author);
	if(result.length <= 0)  return 0;
	$('#'+htmlid).html(result);

};

var bio = true;
function toggleBio(htmlid) {

	if(bio) {
		switchLayer('bio',false);
		bio = false;
		$('#'+htmlid).html("<a href='javascript:toggleBio(\"bio_text\");'>show bio</a>");
	}
	else {
		switchLayer('bio',true);
		bio = true;
		$('#'+htmlid).html("<a href='javascript:toggleBio(\"bio_text\");'>hide bio</a>");
	}
};
