var DOMUtilities;

(function(jQuery){
    DOMUtilities = {
    	path: "",
    	images: [],
    	getPath: function(){
    		for(var i=0,path,scripts=jQuery('head script'); i<scripts.length; i++){
    			var s = scripts[i];
    			if(s.src.match('application.js')){
    				path = s.src.replace("/javascripts/application.js","");
    			}
    		}
    		return path;
    	},
    	linkTargets: function(c){
    		this.setContext(c).context.find('a[rel*="external"]').attr('target','_blank');
    		return this;
    	},
    	rollOvers: function(c){
    		this.setContext(c).context.find('img.rollOver, input[type="image"].rollOver').live("mouseenter", function(e){
    			if (!this.className.match(/active/)) {
    				this.src = this.src.replace("_i.", "_o.");
    			}
    		},function(e){
    			if (!this.className.match(/active/)) {
    				if (this.src.indexOf("_o.") != -1) {
    					this.src = this.src.replace("_o.", "_i.");
    				}
    				else 
    					if (this.src.indexOf("_a.") != -1) {
    						this.src = this.src.replace("_a.", "_i.");
    					}
    			}
    		}).filter('input[type="image"]').live("mouseleave", function(e){
    			this.src = this.src.replace("_o.","_a.");
    		});
    		return this;
    	},
    	autoReplaceInputs: function(c){
    		this.setContext(c).context.find('input.autoReplace, textarea.autoReplace').addClass('empty').live("focus",function(e){
    			if(this.value == this.defaultValue){
    				this.value = "";
    			}
    			jQuery(this).addClass('focus').removeClass('empty');
    		}).live("blur", function(e){
    			if(jQuery.trim(this.value) === ""){
    				this.value = this.defaultValue;
    				jQuery(this).addClass('empty');
    			}
    			jQuery(this).removeClass('focus');
    		});
    		return this;
    	},
    	defaultSelects: function(c){
    		this.setContext(c).context.find('select.formfield').addClass('empty').live("focus", function(e){
    			if(this.defaultValue == undefined){
    				this.defaultValue = this.value;
    			}
    			jQuery(this).addClass('focus').removeClass('empty');
    		}).live("blur", function(e){
    			if(jQuery.trim(this.value) == this.defaultValue){
    				jQuery(this).addClass('empty');
    			}
    			jQuery(this).removeClass('focus');
    		});
    		return this;
    	},
    	setContext: function(c){
    		if(typeof(c) != "undefined"){
    			this.context = jQuery(c);
    		}
    		return this;
    	},
    	imgPreload: function(){
    		var preloader=new Image();
    		for(var i=0; i<this.images.length; i++){
    			preloader.src = this.path + this.images[i];
    		}
    		return this;
    	},
    	initialize: function(c){
    		this.path = this.getPath();
    		if(typeof(c) == "undefined"){
    			c = jQuery(document.body);
    		}
    		this.setContext(c).linkTargets().rollOvers().autoReplaceInputs().defaultSelects();
    	}
    };
    
    jQuery(document).ready(function(){
		
        DOMUtilities.initialize();
		
		jQuery('.container.slidedeck .prev').click(function(e){
			e.preventDefault();
			myDeck.prev();
		});
		
		jQuery('.container.slidedeck .next').click(function(e){
			e.preventDefault();
			myDeck.next();
		});
		
		jQuery('span.close').live("click", function(e){
			e.preventDefault();	
			jQuery('#contact').animate({ marginTop: '-=70' }, 100, 'swing');
			jQuery('#contact').delay(125).animate({	marginTop: '+=1000' }, 250, 'swing');
			jQuery('#contact').fadeOut(750);
			jQuery('#dribbble').animate({ marginTop: '-=70' }, 100, 'swing');
			jQuery('#dribbble').delay(125).animate({	marginTop: '+=1000' }, 250, 'swing');
			jQuery('#dribbble').fadeOut(750);
			jQuery('#modal').delay(150).fadeOut(700);
		});
		
		jQuery("#nav .connect, .about .msg, .getInTouch a, #footer .contact a").click(function(e){
			e.preventDefault();
			if(jQuery('#contact').length){
					jQuery('#contact').css('marginTop', '-280px');
					jQuery('#dribbble').css('marginTop', '-132px');
					jQuery('#modal').fadeIn('slow');
					jQuery('#contact').delay(150).fadeIn('slow');
			}else{
				jQuery('#modal').append('<div id="contact"></div>');
				jQuery('#contact').load('./contact.htm', function(){
					jQuery('#contact').css('marginTop', '-280px');
					jQuery('#dribbble').css('marginTop', '-132px');
					jQuery('#modal').fadeIn('slow');
					jQuery('#contact').delay(150).fadeIn('slow');
				});
			}
		});
			
		/*jQuery('#contact .dribbble a').live("click", function(e){
			e.preventDefault();
			if(jQuery('#dribbble').length){
				jQuery('#contact').fadeOut(300);
				jQuery('#dribbble').delay(300).fadeIn(300);
			}else{
				jQuery('#modal').append('<div id="dribbble"></div>');
				jQuery('#dribbble').load('./dribbble.htm', function(){
					jQuery('#contact').css('marginTop', '-280px');
					jQuery('#dribbble').css('marginTop', '-132px');
					jQuery('#contact').fadeOut(300);
					jQuery('#dribbble').delay(300).fadeIn(300);
				});
			}
		});*/
		
		/*jQuery('h2.home .dribbble').click(function(e){
			e.preventDefault();
			if(jQuery('#dribbble').length){
				jQuery('#contact').css('marginTop', '-280px');
				jQuery('#dribbble').css('marginTop', '-132px');
				jQuery('#modal').fadeIn('slow');
				jQuery('#dribbble').delay(150).fadeIn('slow');
			}else{
				jQuery('#modal').append('<div id="dribbble"></div>');
				jQuery('#dribbble').load('./dribbble.htm', function(){
					jQuery('#contact').css('marginTop', '-280px');
					jQuery('#dribbble').css('marginTop', '-132px');
				jQuery('#contact').css('marginTop', '-280px');
				jQuery('#dribbble').css('marginTop', '-132px');
				jQuery('#modal').fadeIn('slow');
				jQuery('#dribbble').delay(150).fadeIn('slow');
				});
			}
		});*/
		
		jQuery('#nav .creations').click(function(e){
			e.preventDefault();
			if(jQuery('#creatios').length){
				return false;
			}else{
				jQuery('#featuredprojects').append('<div id="creations"></div>');
				jQuery('#creations').load('./creations.htm', function(){
					jQuery.scrollTo('#featuredprojects',500,{
						easing: self.easing,
						offset: {
							left: 0,
							top: 10
						},
						onAfter: function(){
							jQuery('#creations').animate({	height: '708' }, 250, 'swing',function(){ jQuery('#creations ul').fadeIn('400') });
						}
					});
				});
			}
		});
		
		jQuery('#featuredprojects li img').live("mouseover", function(){
			jQuery(this).siblings('span.overlay').fadeIn(150, 'swing').css('display', 'block');
		});
		
		jQuery('span.overlay').live("mouseleave", function(){
			jQuery(this).fadeOut(300, 'swing');
		});
		
		jQuery('span.overlay .preview').live("mouseenter", function(){
			$(this).siblings('span.label').html('Preview');
		});
		
		jQuery('span.overlay .link').live("mouseenter", function(){
			$(this).siblings('span.label').html('Launch');
		});
		
		jQuery('span.overlay .link, span.overlay .preview').live("mouseleave", function(){
			$(this).siblings('span.label').html('&nbsp;');
		});
		
		// set your twitter id
		var user = 'dairien';
		  
		// using jquery built in get json method with twitter api, return only one result
		jQuery.getJSON('http://twitter.com/statuses/user_timeline.json?screen_name=' + user + '&count=1&callback=?', function(data)      {
	
			var tweet = data[0].text;
			var timeago = relTime(data[0].created_at);
		  
			// process links and reply
			tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, function(url) {
				return '<a href="'+url+'">'+url+'</a>';
			}).replace(/B@([_a-z0-9]+)/ig, function(reply) {
				return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
			});
		  
			// output the result
			jQuery(".twitter p").html('<a target="_blank" href="http://twitter.com/dairien">@Dairien</a> ' + tweet + ' <span class="timestamp">' +timeago+ '</span>');
		});
		
		$('#contactform').live('submit', function(){
		
			var action = $(this).attr('action');
			
			$('#submit').attr('disabled','disabled').after('<img src="./images/ajax-loader.gif" class="loader" />');
			
			$.post(action, { 
				name: $('#name').val(),
				email: $('#email').val(),
				message: $('#message').val()
			},
				function(data){
					$('#notice').html(data);
					$('#contactform img.loader').fadeOut('fast',function(){$(this).remove()});
					$('#submit').removeAttr('disabled'); 
					if(data.match('Success! Message sent.') != null) {
						$('#contact .close').trigger('click');
					}
				}
			);
			
			return false; 
		
		});
		
		var myHash = window.location.hash.substring(0);
		
		if(myHash == '#dribbble'){	
			$('h2.home .dribbble').delay(750).trigger('click');
		}
		
    });
	
})(jQuery);

function relTime(time_value) {
	time_value = time_value.replace(/(\+[0-9]{4}\s)/ig,"");
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var timeago = parseInt((relative_to.getTime() - parsed_date) / 1000);
	if (timeago < 60) return 'less than a minute ago';
	else if(timeago < 120) return 'about a minute ago';
	else if(timeago < (45*60)) return (parseInt(timeago / 60)).toString() + ' minutes ago';
	else if(timeago < (90*60)) return 'about an hour ago';
	else if(timeago < (24*60*60)) return 'about ' + (parseInt(timeago / 3600)).toString() + ' hours ago';
	else if(timeago < (48*60*60)) return '1 day ago';
	  else return (parseInt(timeago / 86400)).toString() + ' days ago';
	}
