window.addEvent('domready', function() {

//menus
if(!window.ie6){
//clearing timeouts
menuTO = window.setTimeout(function(){}, 1);
function showMenus(el){
    menuTO = $clear(menuTO);
        el.fireEvent('openmenu');
        el.getParent().fireEvent('fadeup');
}
function clearMenus(el,i){
    menuTO = window.setTimeout(function(i){
        if(el.hasClass('link_menu')){
            el.fireEvent('closemenu');
            el.getParent().fireEvent('fadeback');
        }else{
            el.getPrevious().fireEvent('closemenu');
            el.getPrevious().getParent().fireEvent('fadeback');
        }
    },i);
}
function clearAll(){
    menuTO = $clear(menuTO);
    $$('.link_menu').each(function(el){
       el.fireEvent('closemenu');
       el.getParent().fireEvent('clearall');
    }); 
}

//trigger menus
$$('.reveal_menu', '.link_menu').each(function(el){
    el.addEvents({
        mouseenter : function(){ 
            menuTO = $clear(menuTO); 
            if(el.hasClass('link_menu')){
                showMenus(el);
            }
            },
        mouseleave : function(){ clearMenus(el, 500); },
        click : function(){ clearMenus(el, 100); }
    });
});

//events for actual menu icon
elements = $$('.link_menu');
elements.each(function(el){
    
});

//show menu link when you hover a section
elements = $$('.link_menu');
elements.each(function(el){
    var t0 = new Fx.Styles(el, {duration:250, wait:false});
    t0.set({
        'opacity': 0
    });
    el.getParent().addEvents({
        mouseenter : function(){ t0.start({ 'opacity': .65 });},
        fadeup : function(){ t0.start({ 'opacity': 1 }); },
        mouseleave : function(){ clearAll(); },
        fadeback : function(){ t0.start({ 'opacity': .65 });},
        clearall : function(){ t0.start({ 'opacity': 0 });}
    });
})


//fade menu ul when link is hovered
elements = $$('.reveal_menu');
elements.each(function(el){
    var m = new Fx.Styles(el, {transition:Fx.Transitions.Quad.easeOut, duration:200, wait:false});
    m.set({ 'top':'15px;' });
    el.getPrevious().addEvents({
        openmenu : function(){ m.start({ 'opacity': 1, 'top':'0px' }); },
        closemenu : function(){ m.start({ 'opacity': 0, 'top':'15px' }); }
    });
});

}//end ie6 exclusion

//scrolling
elements = $$('.link_menu','#nav li a', '.reveal_menu li a', '#topmorph1');
var s0 = new Fx.Scroll(window, {wait:false, duration:1500, transition:Fx.Transitions.Cubic.easeInOut});
elements.each(function(el){
    var h = el.href.indexOf('#')+1;
    var to = el.href.toString().substr(h);
    el.href='javascript:void(0);';
    el.addEvents({
        mousedown: function(){ s0.toElement(to); return false; }
    });
});


//top section
    //ask link
    var t1 = new Fx.Styles($('topmorph1'), {duration: 250, wait:false});
    $('top').addEvents({
        mouseenter : function(){ t1.start({ 'color': '#345976' }); },
        mouseleave : function(){ t1.start({ 'color': '#ced0cb' }); }
    });
    
    //nav box
    var t2 = new Fx.Styles($('nav'), {duration: 250, wait:false});
    $('nav').addEvents({
        mouseenter : function(){ t2.start({ 'border-top-color': '#345976','border-bottom-color': '#345976','border-left-color': '#345976','border-right-color': '#345976' }); },
        mouseleave : function(){ t2.start({ 'border-top-color': '#636462','border-bottom-color': '#636462','border-left-color': '#636462','border-right-color': '#636462' }); }
    });
    
    //individual nav items
    var list = $$('#nav li a');
    list.each(function(element){
        var t3 = new Fx.Styles(element, {duration:250, wait:false})
        element.addEvents({
            mouseenter : function(){ t3.start({ 'border-bottom-color': '#345976' }); },
            mouseleave : function(){ t3.start({ 'border-bottom-color': '#161616' }); }
        });
    });

//about section
    //twitter links?
    var elements = $$('#twitter h1 a', '#about_extras a');
    elements.each(function(el){
        var t4 = new Fx.Styles(el, {duration:250, wait:false});
        $('about').addEvents({
            mouseenter : function(){ t4.start({ 'color': '#e4e4da' }); },
            mouseleave : function(){ t4.start({ 'color': '#abb1a3' }); }
        });
    });

    //twitter feed, bio links
    var elements = $$('#twitter a.twitter_link', '#about_bio a');
    elements.each(function(el){
        var t5 = new Fx.Styles(el, {duration:250, wait:false});
        $('about').addEvents({
            mouseenter : function(){ t5.start({ 'color': '#a8b140' }); },
            mouseleave : function(){ t5.start({ 'color': '#abb1a3' }); }
        });
    });
    
//work section

//projects (kwiks)

currentkwick = 1;
slideFrame();
function refreshKwicks(){

var szNormal = 200, szSmall  = 100, szFull   = 500;
var kwicks1 = $$("#projects .set1");
var fx1 = new Fx.Elements(kwicks1, {wait: false, duration: 400, transition: Fx.Transitions.Cubic.easeOut});
kwicks1.each(function(kwick, i) {
	kwick.addEvent("mouseenter", function(event) {
	    
	    if(currentkwick == 1)
	    {
    		var o = {};
    		o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
    		kwicks1.each(function(other, j) {
    			if(i != j) {
    				var w = other.getStyle("width").toInt();
    				if(w != szSmall) o[j] = {width: [w, szSmall]};
    			}
    		});
    		fx1.start(o);
    		pauseBar();
	    }
	    
	});
});

$("projects").addEvent("mouseleave", function(event) {
    
    if(currentkwick == 1)
    {
    	var o = {};
    	kwicks1.each(function(kwick, i) {
    		o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
    	});
    	fx1.start(o);
    	startBar();
    }
    
});

var kwicks2 = $$("#projects .set2");
var fx2 = new Fx.Elements(kwicks2, {wait: false, duration: 400, transition: Fx.Transitions.Cubic.easeOut});
kwicks2.each(function(kwick, i) {
	kwick.addEvent("mouseenter", function(event) {
	    
	    if(currentkwick == 2)
	    {
    		var o = {};
    		o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
    		kwicks2.each(function(other, j) {
    			if(i != j) {
    				var w = other.getStyle("width").toInt();
    				if(w != szSmall) o[j] = {width: [w, szSmall]};
    			}
    		});
    		fx2.start(o);
    		pauseBar();
		}
		
	});
});

$("projects").addEvent("mouseleave", function(event) {
    
    if(currentkwick == 2)
    {
    	var o = {};
    	kwicks2.each(function(kwick, i) {
    		o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
    	});
    	fx2.start(o);
    	startBar();
    }
    
});

}

function incrementKwicks()
{
    if(currentkwick == 2)
    {
        currentkwick = 1;
    }else{
        currentkwick++;
    }
}

function slideFrame()
{
    var slider = $('pointlesscontainer');
    var myslide = new Fx.Scroll(slider, {duration:650, transition: Fx.Transitions.Cubic.easeOut});
    xpos = (currentkwick-1)*816;
    myslide.scrollTo(xpos);
}

function startBar(){
	var buttons = $$('#jump_buttons li');
	buttons[currentkwick-1].fireEvent('select');
    var el = $('prog_bar');
    dur_ratio = (810-$('prog_bar').clientWidth)/810;
    dur = 10000 * dur_ratio;
    barup = new Fx.Style($('prog_bar'), 'width', {duration:dur, transition: Fx.Transitions.linear, onComplete:resetBar});
    if(typeof(bar)!='undefined')
	{
		bar.stop();
	}
	bar = barup.start(810);
}
function pauseBar(){
	if(typeof(bar)!='undefined')
	{
		bar.stop();
	}
}

function resetBar(){
	if(typeof(bar)!='undefined')
	{
		bar.stop();
	}
    bardown = new Fx.Style($('prog_bar'), 'width', {duration:300, onComplete:startBar});
    bar = bardown.start(0);
	var buttons = $$('#jump_buttons li');
	buttons[currentkwick-1].fireEvent('deselect');
    incrementKwicks();
    slideFrame();
}
function jumpTo(which){
	if(typeof(bar)!='undefined')
	{
		bar.stop();
	}
	bardown = new Fx.Style($('prog_bar'), 'width', {duration:300, onComplete:startBar});
	bar = bardown.start(0);
	var buttons = $$('#jump_buttons li');
	buttons[currentkwick-1].fireEvent('deselect');
	currentkwick = which;
	slideFrame();
}
refreshKwicks();
$('work').addEvent("mouseenter", startBar);
$('work').addEvent("mouseleave", pauseBar);
window.addEvent('blur', pauseBar);

var elements = $$('.right .small');
elements.each(function(el){
    
    //opacity changes
    var mytrans = new Fx.Styles(el, {duration:300, wait:false});
    el.addEvents({
        mouseenter : function(){ mytrans.start({ 'opacity': 1 }); },
        mouseleave : function(){ mytrans.start({ 'opacity':.4 }); }
    });
    mytrans.set({ 'opacity': .4 });
    
    //change main image onclick....
    var bigimg = el.getParent().getParent().getPrevious().getChildren();
    bigimg = bigimg[0];
    el.addEvents({
        click : function(){ bigimg.src = el.src.replace(/thumbs/i, 'large'); }
    });

});

//bottom links
var elements = $$('#jump_buttons li');
elements.each(function(el){
	var myButton = new Fx.Styles(el, {duration:300, wait:false});
	el.addEvents({
		mouseenter : function(){ myButton.start({ 'background-color': '#3f4f63', 'color': '#abb1a3' }) },
		mouseleave : function(){ if(! el.className.match(/active/i)){ myButton.start({ 'background-color': '#495c73', 'color': '#3f4f63' }); } },
		select : function(){ el.className += ' active'; el.fireEvent('mouseenter'); },
		deselect : function(){ el.className = el.className.replace(/ active/gi, ''); el.fireEvent('mouseleave'); },
		click : function(){ var mykwick = el.className.substr(0,1); jumpTo(mykwick); }
	});
});


//thoughts section
    //swap cloud image
    // var elements = $$('#thoughts_title');
    // elements.each(function(el){
    //     $('thoughts_comments').addEvents({
    //         mouseenter : function(){ el.addClass('on'); },
    //         mouseleave : function(){ el.removeClass('on'); }
    //     });
    // });
    
    //green links
    var elements = $$('#thoughts_title h1 a', '#thoughts_body a'/*, '#thoughts_comments a.all_comments'*/);
    elements.each(function(el){
        var t10 = new Fx.Styles(el, {duration:250, wait:false});
        $('thoughts').addEvents({
            mouseenter : function(){ t10.start({ 'color': '#a8b140' }); },
            mouseleave : function(){ t10.start({ 'color': '#abb1a3' }) }
        });
    });
    
    // //yellow links
    // var elements = $$('#thoughts_comments ul.comments li.a a');
    // elements.each(function(el){
    //     var t11 = new Fx.Styles(el, {duration:250, wait:false});
    //     $('thoughts').addEvents({
    //         mouseenter : function(){ t11.start({ 'color': '#c1b179' }); },
    //         mouseleave : function(){ t11.start({ 'color': '#abb1a3' }); }
    //     });
    // });
    // 
    // //gray links
    // var elements = $$('#thoughts_comments ul.comments li.a a');
    // elements.each(function(el){
    //     var t12 = new Fx.Styles(el, {duration:250, wait:false});
    //     $('thoughts').addEvents({
    //         mouseenter : function(){ t12.start({ 'color': '#c1c0bb' }); },
    //         mouseleave : function(){ t12.start({ 'color': '#abb1a3' }); }
    //     });
    // });


//contact section
	//hover colors
	var elements = $$('#contact_options a, #contact_options span span');
	elements.each(function(el){
		var t13 = new Fx.Styles(el, {duration:250, wait:false});
		$('contact').addEvents({
			mouseenter : function(){ t13.start({ 'color': '#5d0d17' }); },
			mouseleave : function(){ t13.start({ 'color': '#000000'}); }
		});
	});
	
	//mad libs
	var elements = $$('#mad-copy span');
	elements.each(function(el){
		el.addEvents({
			click : function(){ 
				var type = el.className;
				var word = prompt('Enter your '+type+':');
				if(word){
					el.setText((word == '')? '['+type+']' : word);
					
				}
			},
			reset : function(){
				var type = el.className;
				el.setText('['+type+']');
			}
		});
	});
	
	//put the hurt on ie6
  // if(window.ie6 && (!window.location.href.match(/ieok/)))
  // {
  //  TB_show('', 'ie.html?keepThis=true&TB_iframe=true&height=365&width=520', 'upsell');
  // }
  
  //pull the plug
   if(typeof(systemrunning)!='undefined' && (!window.location.href.match(/ieok/)))
   {
    TB_show('', 'nap.html?keepThis=true&TB_iframe=true&height=365&width=520', 'upsell');
   }
	

//end domready wrapper
});
function copyMadLib(){
	var content = $('mad-copy').getText().clean();
	if($('Field3').value!='')
	{
		var go = confirm("This will replace your current message. Are you sure?");
	}else{
		go=true;
	}
	if(go)
	{
		$('Field3').value = content;
	}
}

function resetMadLib(){
	var elements = $$('#mad-copy span');
	elements.each(function(el){
		el.fireEvent('reset');
	});
}
function submitContact(){
	$('saveForm').src = 'assets/images/contact_fired.gif';
	$('saveForm').disabled = 'disabled';
	var d = Array();
	elements = $$('#form7 .input');
	elements.each(function(el){
		d[el.tagName] = el.getText();
	});
	var postForm = new Ajax('assets/php/contact-submit.php', {method: 'post', data: $('form7'), onComplete: function(t){ alertResponse(t);} }).request();
	
}
function alertResponse(r){
	
	if($defined($('notify')))
	{
		$('notify').remove();
	}
	
	p = new Element('p', {
		'styles': {
			'opacity':0,
			'color':'#a8b140',
			'font':'italic 12px verdana',
			'background-color':'#fff',
			'padding':'2px',
			'display':'inline',
			'margin-bottom':'10px'
		},
		'events': {
			destroy: function(){
				var kill = new Fx.Styles($('notify'), {duration:1500, wait:false, onComplete:function(){ $('notify').remove(); } });
				kill.start({ 'opacity':0 });
			},
			reveal: function(){
				var show = new Fx.Styles($('notify'), {duration:450, wait:false, onComplete:function(){ poo = removeNotify.delay(3000);} });
				show.start({ 'opacity':1 });
			}
		},
		'id': 'notify'
	});
	p.setHTML(r);
	el = $$('#form7 h2');
	el=el[0];
	p.injectBefore(el);
	p.fireEvent('reveal');
	$('saveForm').src = 'assets/images/contact_fire.gif';
	$('saveForm').disabled = '';
}
function removeNotify(){
	$('notify').fireEvent('destroy');
}
//attempting to fix the bg flicker in ie6
(function(){ /*Use Object Detection to detect IE6*/ var m = document.uniqueID /*IE*/ && document.compatMode /*>=IE6*/ && !window.XMLHttpRequest /*<=IE6*/ && document.execCommand ; try{ if(!!m){ m("BackgroundImageCache", false, true) /* = IE6 only */ } }catch(oh){}; })();

