/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// 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 switch_aboutme(master)
{
	var divsToChange = new Array();
	divsToChange[0] = "aboutme_bio";
	divsToChange[1] = "aboutme_stats";
	divsToChange[2] = "aboutme_music";
	divsToChange[3] = "aboutme_film";
	divsToChange[4] = "aboutme_books";
	divsToChange[5] = "aboutme_tv";
	divsToChange[6] = "aboutme_interests";
	divsToChange[7] = "aboutme_heroes";
	divsToChange[8] = "aboutme_school";
	divsToChange[9] = "aboutme_work";
	
	for(var i=0; i < 10; i++)
	{
		if(divsToChange[i]==master)
		{
			var makeVisible = document.getElementById(master);
			makeVisible.className = 'module_content visible';
		}
		else
		{
			var makeHidden = document.getElementById(divsToChange[i]);
			makeHidden.className = 'module_content hidden';
		}
	}	
}

/*
################################################*/
function switch_artist_bio(master)
{
	var divsToChange = new Array();
	divsToChange[0] = "bio_first_part";
	divsToChange[1] = "bio_second_part";	
	
	for(var i=0; i < 10; i++)
	{
		if(divsToChange[i]==master)
		{
			var makeVisible = document.getElementById(master);
			makeVisible.className = 'visible';
		}
		else
		{
			var makeHidden = document.getElementById(divsToChange[i]);
			makeHidden.className = 'hidden';
		}
	}	
}

/*
################################################*/
function message_fade(type)
{
	var color = '#00CCFF';
	
	if(type == 'notification_error')
	{
		color = "#FF0000";
	}
	if(type == 'notification_success')
	{
		color = "#00FF99";	
	}
	
	new Effect.Highlight('top_message', {startcolor: color, restorecolor: 'true'});
}

/*
################################################*/
function check_urlname(urlname)
{
	var url = 'http://www.thedeathof.com/vm_inc/site_php/usernamecheck.php?urlcheck=' + urlname;
	new Ajax.Updater('urlname_result', url);
}

/*
################################################*/
function change_little_things(data, type, member_key)
{	
	var b64data = Base64.encode(data);
	var url = 'http://www.thedeathof.com/vm_inc/site_php/php_ajax.php?type=' + type + '&data=' + b64data + '&member_key=' + member_key;
	new Ajax.Updater( { success: type + '_result', failure: type + '_result' }, url, { method: 'get' });
}

/*
################################################*/
function showStates(country)
{
	if(country=='United States' || country == 'Canada')
	{
		document.getElementById('state_select').className = 'visible';
		document.getElementById('zip_code').className = 'visible';
	}
	else
	{
		document.getElementById('state_select').className = 'hidden';
		document.getElementById('zip_code').className = 'hidden';
	}
}

/*
################################################*/
function showSchoolDegrees(type)
{
	if(type == 'College/University' || type == 'Trade School' || type == 'Graduate School')
	{
		document.getElementById('degree_field').className = 'divider visible';
	}
	else
	{
		document.getElementById('degree_field').className = 'divider hidden';
		document.getElementById('degree_text').value = '';
	}
}

/*
################################################*/
function miscellaneous_fade(id, color)
{
	new Effect.Highlight(id, {startcolor: color, restorecolor: 'true'});
}

/*
################################################*/
function setMaxLength() 
{
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) 
	{
		if (x[i].getAttribute('maxlength')) 
		{
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

/*
################################################*/
function checkMaxLength() 
{
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

/*
################################################*/
function changeCalendarURL(month, year, urlname)
{
	window.location = "/user/" + urlname + "/admin/calendar/" + month + "/" + year;
}

/*
################################################*/
function changeBlogURL(month, year, urlname)
{
	window.location = "/user/" + urlname + "/admin/blog/" + month + "/" + year;
}

/*
################################################*/
function changeBlogProfileURL(month, year, urlname)
{
	window.location = "/user/" + urlname + "/blog/archives/" + month + "/" + year;
}

/*
################################################*/
function changeCalProfileURL(month, year, urlname)
{
	window.location = "/user/" + urlname + "/calendar/archives/" + month + "/" + year;
}

/*
################################################*/
function changeCalDayProfileURL(month, day, year, urlname)
{
	window.location = "/user/" + urlname + "/calendar_events/" + month + "/" + day + "/" + year;
}

/*
################################################*/
function generic_URL_switch(url)
{
	window.location = "/overlord/advertising/manageclient/" + url.value;
}

/*
################################################*/
function showEndType(div)
{
	if(div == 0)
	{
		document.getElementById('event_end').className = 'visible';
		document.getElementById('event_end_multi').className = 'hidden';
	}
	else
	{
		document.getElementById('event_end').className = 'hidden';
		document.getElementById('event_end_multi').className = 'visible';
	}
}

/*
################################################*/
function confirm_action(action, id, urlname)
{
	if(action == 'deletephoto')
	{
		var answer = confirm("Are you sure you want to delete this photo? THIS CANNOT BE UNDONE!");
		if (answer)
		{
			window.location = "/user/" + urlname + "/admin/photos/delete/" +id;
		}
		else
		{
			//
		}
	}
	if(action == 'deletefriend')
	{
		var answer = confirm("Are you sure you want to delete this friend? THIS CANNOT BE UNDONE!");
		if (answer)
		{
			window.location = "/user/" + urlname + "/admin/friends/delete/" +id;
		}
		else
		{
			//
		}
	}
	if(action == 'delete_message')
	{
		var answer = confirm("Are you sure you want to delete this message? THIS CANNOT BE UNDONE!");
		if (answer)
		{
			window.location = "/user/" + urlname + "/delete/" +id;
		}
		else
		{
			//
		}
	}
	if(action == 'deleteplaylist')
	{
		var answer = confirm("Are you sure you want to delete this playlist? THIS CANNOT BE UNDONE!");
		if (answer)
		{
			window.location = "/user/" + urlname + "/admin/victorstream/delete/" +id;
		}
		else
		{
			//
		}
	}
}

/*
################################################*/
function show_instructions(value)
{
	if(value == "Bug")
	{
		document.getElementById("complaint_instruct").className = "visible";	
	}
	else
	{
		document.getElementById("complaint_instruct").className = "hidden";
	}
}

/*
################################################*/
function change_file_requirements(which)
{
	var requirements = new Array();
	requirements[1] = "Dimensions: <span style='color:#00ccff;'>405px by 237px</span><br>Format: <span style='color:#00ccff;'>Flash (.swf) or GIF (.gif)</span><br>Size Limit: <span style='color:#00ccff;'>70 kb</span>";
	requirements[2] = "Dimensions: <span style='color:#00ccff;'>419px by 90px</span><br>Format: <span style='color:#00ccff;'>Flash (.swf) or GIF (.gif) or JPEG (.jpg)</span><br>Size Limit: <span style='color:#00ccff;'>60 kb</span>";
	requirements[3] = "Dimensions: <span style='color:#00ccff;'>200px by 90px</span><br>Format: <span style='color:#00ccff;'>JPEG (.jpg) or GIF (.gif)</span><br>Size Limit: <span style='color:#00ccff;'>45 kb</span>";
	requirements[4] = "Dimensions: <span style='color:#00ccff;'>405px by 237px</span><br>Format: <span style='color:#00ccff;'>Flash (.swf) or GIF (.gif)</span><br>Size Limit: <span style='color:#00ccff;'>70 kb</span>";
	requirements[5] = "Dimensions: <span style='color:#00ccff;'>200px by 90px</span><br>Format: <span style='color:#00ccff;'>Flash (.swf) or GIF (.gif)</span><br>Size Limit: <span style='color:#00ccff;'>45 kb</span>";
	
	var changediv = document.getElementById('media_requirements');
	changediv.innerHTML = requirements[which];
	document.getElementById('main_file').disabled = '';
}

/*
################################################*/
function reveal_duration(val)
{
	if(val == 1)
	{
		document.getElementById('duration_set').className = 'visible';	
	}
	else
	{
		document.getElementById('duration_set').className = 'hidden';	
	}
}

/*
################################################*/
function check_flash(filename)
{
	var filecheck = file_parse(filename);
	if(filecheck == '.swf')
	{
		document.getElementById('backup').className = "visible";	
	}
	else
	{
		document.getElementById('backup').className = "hidden";
	}
}

/*
################################################*/
function file_parse(file)
{
	var ext = file.slice(file.indexOf(".")).toLowerCase();
	return ext;
}