$(document).ready(function() {
    $('.magnet').easydrag();
    stickWordsToPage();
    $('.rotten').each(function() {
        $(this).attr('href', rot13($(this).attr('href')));
    });
});

function rot13(text)
{
    return text.replace(/[a-zA-Z]/g, function(c) {
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
    });
}

function shutdown()
{
    $('.magnet').animate({ opacity: 0.0 }, 500);
    $('#navigation').animate({ opacity: 0.0 }, 500);
    $('body').animate({ backgroundColor: 'black' }, 1000);
}

function stickWordsToPage()
{
	var baseWords = 'the,of,and,a,to,in,is,you,that,it,he,for,was,on,are,as,with,his,they,at,be,this,from,I,have,or,by,one,had,not,but,what,all,were,when,we,there,can,an,your,which,their,said,if,do,will,each,about,how,up,out,them,then,she,many,some,so,these,would,other,into,has,more,her,two,like,him,see,time,could,no,make,than,first,been,its,who,now,people,my,made,over,did,down,only,way,find,use,may,water,long,little,very,after,words,called,just,where,most,know,get,through,back,much,go,before,good,new,write,our,used,me,man,too,any,day,same,right,look,think,also,around,another,came,come,work,three,word,must,because,does,part,even,plance,well,such,here,take,why,things,help,put,years,different,away,again,off,went,old,number,great,tell,men,say,small,every,found,still,between,name,should,Mr.,home,big,give,air,line,set,own,under,us,end,read,last,never,left,along,while,might,next,sound,below,saw,both,something,thought,few,those,always,looked,show,large,often,together,asked,house,don\'t,world,going,want,school,important,until,form,food,keep,children,feet,land,side,without,boy,once,animals,life,enough,took,Earth,sometimes,four,head,above,kind,began,almost,live,page,got,need,far,let,hand,high,year,monther,light,parts,country,father,night,following,picture,being,study,second,eyes,soon,times,story,boyes,since,white,days,ever,sure,paper,hard,near,sentence,better,best,across,during,today,others,sure,however,means,knew,it\'s,try,told,young,miles,sun,ways,thing,whole,hear,example,heard,several,change,answer,room,sea,against,top,turned,learn,point,city,play,toward,five,using,himself,usually';
	var funzies = 'Large Hadron Collider,txt,msg,Mac,dingo,awkward,undies,sex,poop,ninja,pirate,lol,wtf,stfu,brb,:),:(,;),:|,:D,1.21 jigawatts,lolcat,masticate,poppycock,scrumptious,delicious,pants,discombobulate,floozy,smorgasbord,floccinaucinihilipilification,caddywampus,tomfoolery,kumquat';
	var dupeThese = 's,es,ing,ly,d,ed,the,and,a,to,in,is,you,that,it,he,was,for,on,are,as,with,his,they,I,at,be,this,have,or,by,but,we,your,an,she,do,if,so,her,like,make,him,go,no,yes,my,who,its';
	
	var baseWordsArray = baseWords.split(',');
	var funziesArray = funzies.split(',');
	var dupeTheseArray = (new String(dupeThese + ',' + dupeThese)).split(',');
	
	jQuery.unique(baseWordsArray);
	jQuery.unique(funziesArray);
	jQuery.unique(dupeTheseArray);
	
	jQuery.each(dupeTheseArray, function() {
		stickWordToPage(this.toString(), false)
	});
		
	jQuery.each(baseWordsArray, function() {
		stickWordToPage(this.toString(), false)
	});
	
	jQuery.each(funziesArray, function() {
		stickWordToPage(this.toString(), true)
	});
}

function stickWordToPage(word, highlight)
{
	var width = $(document).width();
	var height = $(document).height();
	var offsetLeft = 50;
	var offsetRight = 100;
	var offsetTop = height / 2 > 230 ? height / 2 : 230;
	var offsetBottom = 50;
	var magnet = jQuery(document.createElement('div'));
	var randX = parseInt(Math.random((new Date()).getTime()) * (width - offsetLeft - offsetRight)) + offsetLeft;
	var randY = parseInt(Math.random((new Date()).getMilliseconds()) * (height - offsetTop - offsetBottom)) + offsetTop;
	var randFg = Math.floor(Math.random((new Date()).getSeconds()) * (0x33 - 0x5) + 0x10).toString(16);
	var randColorFg = '#' + randFg + randFg + randFg;
	var randBg, randColorBg, randBgR, randBgG, randBgB;
	
	if(!highlight)
	{
		randBg = Math.floor(Math.random((new Date()).getMilliseconds()) * (0xff - 0xde) + 0xde).toString(16);
		randColorBg = '#' + randBg + randBg + randBg;
	}
	else
	{
		randBgR = Math.floor(Math.random((new Date()).getTime()) * (0xff - 0xcc) + 0xcc).toString(16);
		randBgG = Math.floor(Math.random((new Date()).getMilliseconds()) * (0xff - 0xcc) + 0xcc).toString(16);
		randBgB = Math.floor(Math.random((new Date()).getSeconds()) * (0xff - 0xcc) + 0xcc).toString(16);
		randColorBg = '#' + randBgR + randBgG + randBgB;
	}
	
	magnet.text(word);
	magnet.addClass('magnet');
	magnet.css('left', randX + 'px');
	magnet.css('top', randY + 'px');
	magnet.css('background-color', randColorBg);
	magnet.css('color', randColorFg);

	$('#magnets').append(magnet);
	
	magnet.easydrag();
}