/*TypeFace Settings*/

/*var tf=_typeface_js;

tf.configure({ disableSelection: true });

*/

/*Global Variables*/

var WinProps=null;

var Tweets=null;

var TweetsArr=null;

/*End Global Variables*/



function PageStart(){

	PageResize();

	News();

}



function PageResize(){

	$('all').hide();

	WinProps=document.viewport.getDimensions();

	if (WinProps['width']<800)

	{

		WinProps['width']=800;

	}

	if (WinProps['height']<600)

	{

		WinProps['height']=600;

	}

	$('all').show();

	$('all').style.width=WinProps['width']-5+"px";

	$('all').style.height=WinProps['height']-5+"px";

	$('all').style.top="0px";

	$('all').style.left="0px";

	$('top').style.width=WinProps['width']+"px";

	$('top').style.height=WinProps['height']+"px";

	$('top').style.top="0px";

	$('top').style.left="0px";

	

	$('main').style.left=($('menu').offsetLeft+$('menu').offsetWidth+35)+"px";

	$('main').style.top=($('menu').offsetTop+20)+"px";

	

	$('header').style.left=(WinProps['width']-$('header').offsetWidth)+"px";



	$('bottom').hide();

}



function UpdateHTML(UpdateObj,UpdateFunc){

	if (typeof(UpdateObj) != 'object')

	{

		if ($(UpdateObj))

		{

			CurObj=$(UpdateObj);

		}

		else

		{

			return 0;

		}

	}

	else

	{

		CurObj=UpdateObj;

	}

	$('main').fade({duration:5,after:function(){}});

}



function FadeOut(FaderIt,FTime,CallMe)

{

	if (typeof(FaderIt) != 'object')

	{

		if ($(FaderIt))

		{

			CurObj=$(FaderIt);

		}

		else

		{

			return 0;

		}

	}

	else

	{

		CurObj=FaderIt;

	}

	$('main').fade({duration:FTime,after:CallMe});

	

	return CurObj;

}



function FadeIn(FaderIt,FTime)

{

	if (typeof(FaderIt) != 'object')

	{

		if ($(FaderIt))

		{

			CurObj=$(FaderIt);

		}

		else

		{

			return 0;

		}

	}

	else

	{

		CurObj=FaderIt;

	}

	CurObj.appear({duration:FTime});

	return CurObj;

}





function KillChildren(ToRemove){

	if (typeof(ToRemove) != 'object')

	{

		if ($(ToRemove))

		{

			CurObj=$(ToRemove);

		}

		else

		{

			return 0;

		}

	}

	else

	{

		CurObj=ToRemove;

	}

	if (CurObj && CurObj.hasChildNodes && CurObj.removeChild) {

		while (CurObj.hasChildNodes()) {

			CurObj.removeChild(CurObj.firstChild);

		}

	}

	return CurObj;

}





function Mail(){

	KillChildren('main');

	

	var Form=new Element('form',{id:'ContactForm'});

	var EmailH=new Element('div',{id:'CForm_Email_Holder','class':'InputHolder','className':'InputHolder'}).update("Email: ");

	var Email=new Element('input',{type:'text',id:'CForm_Email','class':'Inputs','className':'Inputs'});

	EmailH.appendChild(Email);



	var SubjectH=new Element('div',{id:'CForm_Subject_Holder','class':'InputHolder','className':'InputHolder'}).update("Subject:");

	var Subject=new Element('input',{type:'text',id:'CForm_Subject','class':'Inputs','className':'Inputs'});

	SubjectH.appendChild(Subject);

	

	var MessageH=new Element('div',{id:'CForm_Message_Holder','class':'InputHolder','className':'InputHolder'}).update("Message:<BR>");

	var Message=new Element('textarea',{id:'CForm_Message','class':'Inputs','className':'Inputs'});

	MessageH.appendChild(Message);

	

	var CaptchaH=new Element('div',{id:'CForm_Captcha_Holder','class':'InputHolder','className':'InputHolder'}).update("<BR>Human Check: ");

	var Captcha=new Element('img',{id:'CForm_Image',src:'http://www.duncanbell.ca/sec/CaptchaSecurityImages.php'});

	var CaptchaInput=new Element('input',{type:'text',id:'CForm_Captcha','class':'Inputs','className':'Inputs',style:'width:75px;'});

	var Break=new Element('br',{});

	var Break2=new Element('br',{});

	var Break3=new Element('br',{});	

	var Break4=new Element('br',{});

	CaptchaH.appendChild(CaptchaInput);

	CaptchaH.appendChild(Break);

	CaptchaH.appendChild(Break2);

	CaptchaH.appendChild(Captcha);

	CaptchaH.appendChild(Break3);

	CaptchaH.appendChild(Break4);

	

	var Send=new Element('input',{type:'button',id:'CForm_Submit','class':'Inputs','className':'Inputs Fonted',style:'width:75px;'});

	Send.value="Send";

	Send['onclick']=SendEmail;

	Send['onClick']=SendEmail;

	

	Form.appendChild(EmailH);

	Form.appendChild(SubjectH);

	Form.appendChild(MessageH);

	Form.appendChild(CaptchaH);

	Form.appendChild(Send);

	var Header=new Element("h1",{}).update("Contact");

	$('main').appendChild(Header);

	$('main').appendChild(Form);

	FadeIn('main',0.5);

}



function SendEmail(){

	if ($('CForm_Email').value!="" | $('CForm_Captcha').value!="" | $('CForm_Subject').value!="" | $('CForm_Message').value!="")

	{

		var request=new Ajax.Request('/mailer.php', {

			method: 'get',

			parameters: {

				'security_code':$('CForm_Captcha').value,

				'email':$('CForm_Email').value,

				'subject':$('CForm_Subject').value,

				'message':$('CForm_Message').value

			},

			onSuccess: function(transport, json){

				//alert(transport['responseText']);

				if (transport['responseText']==1){

					KillChildren('main');

					var Message=new Element('p',{id:'Email_Message',style:'font-size:16px;padding-top:50px;'});

					$('main').appendChild(Message);
					$('Email_Message').update("Your message has been sent.");

				}

				else{

					alert("Something has gone horribly wrong.\nTry the Human Check again, then click Send.");

				}

			}

		});

	}

	else

	{

		alert("Please fill out ALL the fields.");

	}

}



function News(){

	KillChildren('main');

	var NewDiv=new Element('div',{id:'News',style:'font-size:14px;'});

	NewDiv.update("<h1>News</h1>"+
	"<p><h2>Dec. 21, 2010 - Problems Fixed</h2>Fixed the contact form, changed the layout, probably have a lot more changes coming.</p>"+

	"<p><h2>May,  25, 2010 - Cyli.Me</h2>New website, <a href='http://cyli.me'>Cyli.Me</a>, added the WFB Reader to the downloads.</p>"+

	"<p><h2>Feb. 24, 2010 - New Server</h2>Move everything to a new server, Bullfrog powered.</p>"+

	"<p><h2>Jan. 22, 2010 - More Stuff</h2>More work is being done, I added a feed from my twitter, to get the appearance of updates. I have a little PHP script for the CIA World Fact Book that I'm going to stick up here when it's a little more polished as the first download.</p>"+

	"<!--<p><h2>Dec. 21, 2009 - Update</h2>One update a month. W00T, I'm on fire. The downloads will hopefully have something soon. I've started work on a directory module for SiteX, but due to the holidays I haven't been able to really get it going. But it's started. I added some links, including one to my twitter feed. Which I might update more often than this site, we'll see.<BR>Just turned 25, I'm getting pretty old....</p>--"+

	"<!--<p><h2>Nov. 23, 2009 - Website</h2>I've started getting this website together, finally. More coming... some time.</p>"+"<p>This page is best viewed with <a href='http://mozilla.com'>Firefox</a> or <a href='http://www.apple.com/safari/'>Safari</a>. <a href='http://www.google.com/chrome'>Chrome</a> is good, but doesn't support embedded fonts, yet.</p>-->");	

	$('main').appendChild(NewDiv);

	FadeIn('main',0.5);

}



function AboutMe(){

	KillChildren('main');

	var NewDiv=new Element('div',{id:'About Me',style:'font-size:14px;'});

	NewDiv.update("<h1>About Me</h1>"+"<p>Presumably, you've clicked on this link to find out more information about me. Unfortunately, there's not a terribly huge amount of information that's interesting about me.</p>"+"<p>I'm a web programmer, working with PHP, HTML, CSS, MySQL and Javascript.</p>"+"<p>I'm a musician, I play bass in a small folk group, <a href='http://kickptarmigan.com'>Kick Ptarmigan</a>.</p>"+"<p>I'm a boyfriend, my girlfriend runs <a href='http://hemptent.com'>HempTent.com</a>.</p>"+"<p>I'm literate, I read books by Chuck Palahniuk, Clive Barker, Aldous Huxley, George Orwell, Kurt Vonnegut and Geoff Dyer.</p>");	

	$('main').appendChild(NewDiv);

	FadeIn('main',0.5);

}



function ShowLinks(){

	KillChildren('main');

	var NewDiv=new Element('div',{id:'Links',style:'font-size:14px;'});

	NewDiv.update("<h1>Links</h1>"+"<p><ul><li>Interesting Websites<ul>"+

	"<li><a href='http://KickPtarmigan.com'>Kick Ptarmigan</a> - the homepage of my band.</li>"+

	"<li><a href='http://HempTent.com'>HempTent</a> - great fair trade/environmentally friendly store.</li>"+

	"</ul></li>"+

	"<li>Other Stuff:<ul>"+

	"<li><a href='http://cyli.me'>Cyli.Me</a> - a tool to search iTunes, eMusic and other online music retailers.</li>"+

	"<li><a href='http://twitter.com/duncan84'>Twitter</a> - my tweets?</li>"+

	"</ul></li>"+

	"</ul></p>");	

	$('main').appendChild(NewDiv);

	FadeIn('main',0.5);

}



function ShowDownloads(){

	KillChildren('main');

	var NewDiv=new Element('div',{id:'Downloads',style:'font-size:14px;'});

	NewDiv.update("<h1>Downloads</h1>"+"<p><ul><li>PHP Scripts<ul>"+

	"<li><a href='/files/wfbreader_01.zip'>WFB Reader</a> - a quick script to grab data from the CIA World Fact Book.</li>"+

	"</ul></li>"+

	"</ul></p>");	

	$('main').appendChild(NewDiv);

	FadeIn('main',0.5);

}



function GetTweets(){



	var tmp=new Ajax.Request('./RSS2JSON.php', {

		onSuccess: function(transport) {

			Tweets=transport['responseText'].evalJSON();

			ShowTweets();		

		}

	});

}



function ShowTweets(){

	KillChildren('main');

	var total=Tweets['links'].size();

	if (total>5)

	{

		total=5;

	}

	var i=0;

	var TweetsDiv=new Element("div",{id:'Tweets'}).update("<h1>Duncan's Tweets</h1>");

	while (i<total)

	{

		var t=Tweets['links'][i];

		var string=t['date']+":<BR><a href='"+t['link']+"' target=_blank>"+t['title'].replace('Duncan84: ','')+"</a><BR><BR>";

		var tmp=new Element("div",{id:'Tweet_'+i}).update(string);

		TweetsDiv.appendChild(tmp);

		i++;

	}

	$('main').appendChild(TweetsDiv);

	FadeIn('main',0.5);

}



function addEvent(obj, evType, fn){ 

 if (obj.addEventListener){ 

   obj.addEventListener(evType, fn, false); 

   return true; 

 } else if (obj.attachEvent){ 

   var r = obj.attachEvent("on"+evType, fn); 

   return r; 

 } else { 

   return false; 

 } 

}



addEvent(window, 'load', PageStart);



addEvent(window, 'resize', PageResize);



