/*////////////////////////////////////////////////////////////////////////////////////////////////////
/// JAVASCRIPT FOR bekinetic.ca //////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////*/

/*/ VARIABLES //////////////////////////////////////////////////////////////////////////////////////*/
var KINETIC = {};
KINETIC.navOverArray = new Array();
KINETIC.usr = '';
/*/ END VARIABLES //////////////////////////////////////////////////////////////////////////////////*/

/*/ CLASSES ////////////////////////////////////////////////////////////////////////////////////////*/
Request.Twitter = new Class({

  Extends: Request.JSONP,

  options: {
    linkify: true,
    url: 'http://twitter.com/statuses/user_timeline/{term}.json',
    data: {
      count: 5
    }
  },
  
  initialize: function(term, options){
    this.parent(options);
    this.options.url = this.options.url.substitute({term: term});
  },
  
  success: function(data, script){
    if (this.options.linkify) data.each(function(tweet){
      tweet.text = this.linkify(tweet.text);
    }, this);
    
    // keep subsequent calls newer
    if (data[0]) this.options.data.since_id = data[0].id;
    
    this.parent(data, script);
  },
  
  linkify: function(text){
    // modified from TwitterGitter by David Walsh (davidwalsh.name)
    // courtesy of Jeremy Parrish (rrish.org)
    return text.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>')
               .replace(/(^|\W)@(\w+)/g, '$1<a href="http://twitter.com/$2">@$2</a>')
               .replace(/(^|\W)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
  }
  
});
/*/ END CLASSES ////////////////////////////////////////////////////////////////////////////////////*/

/*/ OPERATIONS /////////////////////////////////////////////////////////////////////////////////////*/
KINETIC.setInput = function(input){
	var inputArray = ['Name', 'Email', 'Message', 'Captcha'];
	
	if(inputArray.contains(input.get('value'))){
		input.set('value', '');
		input.setStyle('color', '#414042');
	}else if(input.get('value') == ''){
		input.setStyle('color', '#b1b1b1');
		input.set('value', input.getProperty('name'));
	}
}

KINETIC.setSearch = function(){
	if($('s').get('value') == ''){
		$('s').setStyle('color', '#8a8a8a');
		$('s').set('value', 'Search');
	}else if($('s').get('value') == 'Search'){
		$('s').setStyle('color', '#ddd');
		$('s').set('value', '');
	}
}

KINETIC.erMsg = function(ers){
	var ii = 0;
	
	var erFX = new Fx.Morph('contact-error', {
		duration: 250,
		transition: Fx.Transitions.Sine.easeOut,
		onComplete: function(){
			fadeOut.delay(1500);
		}
	});
	
	var erFX2 = new Fx.Morph('contact-error', {
		duration: 250,
		transition: Fx.Transitions.Sine.easeOut,
		onComplete: function(){
			startEr();
		}	
	});
	
	var fadeOut = function(){
		erFX2.start({
			'opacity': [1, 0]
		});
	}
	
	var startEr = function(){
		$('contact-error').setStyles({'opacity': 1, 'margin-top': '0px', 'height': '0px'});
		$('contact-error').setStyle('visibility', 'visible');
			
		$('contact-error').setStyle('height', '0px');
		
		if(ii <= (ers.length - 1)){
			ers[ii] = ers[ii].split('::');
			$$('#contact-error p').set('text', ers[ii][1]);
			
			var hiBorder = $('contact-us').getElement('input[name=' + ers[ii][0] + ']');
			if(hiBorder == null){
				var hiBorder = $('contact-us').getElement('textarea[name=' + ers[ii][0] + ']');
			}
			
			if(hiBorder && ers[ii][0] != 'None'){
				var erFX3 = new Fx.Morph(hiBorder, {
					duration: 2750,
					transition: Fx.Transitions.Sine.easeOut
				});
				
				erFX3.start({
					'border-color': ['#fb0202', '#fff']
				});
			}
			
			erFX.start({
				'height': ['0px', '34px'],
				'margin-top': ['0px', '-34px']
			});	
		}
		
		ii++;
	}
	
	startEr();
}

KINETIC.getTweets = function(){
	var tweetFx = new Fx.Morph('tweets-here', {duration: 1000, transition: Fx.Transitions.Sine.easeOut, link: 'chain'});
	
	this.getTwitters = new Request.Twitter('kineticdesign', {
		data:{count:6},
		url: 'http://twitter.com/statuses/user_timeline/{term}.json',
		onRequest:function(){
			$$('#tweet-loading').setStyle('visibility', 'visible');
			$$('#tweets-here').setStyle('opacity', 0);
			tweetFx.options.onComplete = function(){
				$$('#tweets-here').empty();
			}
			tweetFx.start({
				'opacity': [1,0]
			});
		},
		onComplete: function(tweets,user){
			$('tweets-here').empty();
			$('tweet-loading').setStyle('visibility', 'hidden');
			var suf = '';
			tweets.each(function(tweet,i) {
				if(i % 2){
					suf = 'b';
				}else{
					suf = 'a';
				}
				
				tweet.tt = 'tweet-' + i;
						
				var tweet_time = tweet.created_at.split(" ", 3).join(" ");
							
				function cleanRT(tw){	 			
				 	tw = tw.replace(/&(lt|gt);/g, function (strMatch, p1){
				 		 return (p1 == "lt")? "<" : ">";
				 	});
				 	
				 	tw = tw.replace(/<\/?[^>]+(>|$)/g, "");
				 	
				 	return tw;
				}
				
				new Element('div',{
					html: '<img src="http://blog.bekinetic.ca/wp-content/themes/kinetic/img/twitter/kinetic.tweet.header-' + suf + '.jpg" style="display: block;" /><div class="tweet-' + suf + '"><div class="tweet-head">' + tweet.text + '</div><div class="tweet-foot"><div class="tweet-time"><span style="font-size: 10px;">' + tweet_time + ' via ' + tweet.source.replace("\\",'') + '</span></div><div class="tweet-reply"><a href="https://twitter.com/?status=@' + tweet.user.screen_name + '%20&in_reply_to_status_id=' + tweet.id + '&in_reply_to=' + tweet.user.screen_name + '" target="_blank"><img src="http://blog.bekinetic.ca/wp-content/themes/kinetic/img/twitter/tweet.reply-' + suf + '.jpg" alt="REPLY" align="left" /></a><a href="http://twitter.com/home/?status=RT+%40' + tweet.user.screen_name  + '+' + cleanRT(tweet.text) + '" target="_blank"><img src="http://blog.bekinetic.ca/wp-content/themes/kinetic/img/twitter/tweet.rt-' + suf + '.jpg" alt="RETWEET" align="left" /></a></div></div><div class="cleaner"></div></div><img src="http://blog.bekinetic.ca/wp-content/themes/kinetic/img/twitter/kinetic.tweet.footer-' + suf + '.jpg" /><br><br>',
					'class': 'tweet',
					'id': 'tweet-' + i
				}).inject('tweets-here');
								
			});
							
			tweetFx.start({
				'opacity': [0, 1]
			});
			
			KINETIC.getTweets.delay(600000, KINETIC.usr);
		}
	}).send();
}

Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {         
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

KINETIC.animatedHeader = function(dur){
	var directionA = '-10px';
	
	var aniA = new Fx.Morph('kin-aurora-a', {
		duration: dur,
		transition: Fx.Transitions.easeInOut,
		link: 'chain',
		onComplete:function(){
			if(directionA == '-10px'){
				directionA = '10px';
			}else{
				directionA = '-10px';
			}
			goAni.delay(dur, goAni);
		}
	});
		
	var goAni = function(){
		aniA.start({
			'margin-left': directionA
		});
	}
	
	goAni();
	
}

/*/ END OPERATIONS /////////////////////////////////////////////////////////////////////////////////*/

/*/ DOMREADY  //////////////////////////////////////////////////////////////////////////////////////*/
window.addEvent('domready', function(){
	Cufon.replace('.nav', {hover: true});
	Cufon.replace('.foot-nav', {hover: true});
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('.postmetadata a');
	Cufon.replace('.kin-head a', {hover: true});
	
	KINETIC.bodyElement = $(document.body);
	
	/*** NAVIGATION ***/
	$$('.nav').each(function(el){
		el.mouseOver = el.getProperty('id') + '-hover';
		$(el.mouseOver).setStyle('opacity', 0);
		
		el.mouseFX = new Fx.Morph(el.mouseOver, {duration: 250, transition: Fx.Transitions.Sine.easeOut, link: 'cancel'});
		
		el.addEvents({
			'mouseover': function(){
				el.mouseFX.start({
					'margin-top': ['8px', '3px'],
					'opacity': [0, 1]
				});
			},
			'mouseout': function(){
				el.mouseFX.start({
					'margin-top': ['3px', '8px'],
					'opacity': [1, 0]
				});
			}		
		});
	});
	/*** END NAVIGATION***/
	
	/*** BROWSER SCROLLING ***/
	KINETIC.scroll = new Fx.Scroll(window, {
		duration: 500,
		transition: Fx.Transitions.Quad.easeInOut,
		onComplete: function(){
			(function(){ $('contact-name').focus(); }).delay(1000);
		}
	});
	
	$('contact-kinetic').addEvent('click', function(e){
		e.stop();
		KINETIC.scroll.toBottom();
	});
	/*** END BROWSER SCROLLING ***/
	
	/*** CONTACT FORM ***/
	$$('.send').each(function(el){
		el.addEvents({
			'mouseover': function(){
				el.setStyle('background-position', 'bottom left');
			},
			
			'mouseout': function(){
				el.setStyle('background-position', 'top left');
			}
		});
	});
	
	$('contact-us').addEvent('submit', function(e){
		e.stop();
		
		this.set('send', {
			onComplete: function(response){
				if(response != 'success'){
					var rep = response.split(',');
					rep = rep.clean('');
					KINETIC.erMsg(rep);
				}else{
					KINETIC.erMsg(Array('None::Well done! We\'ll be in touch!'));
					$$('#contact-us input[type=text]').each(function(inp){
						inp.setStyle('color', '#b1b1b1');
						inp.set('value', inp.getProperty('name'));
					});
					$$('#contact-us textarea').each(function(inp){
						inp.setStyle('color', '#b1b1b1');
						inp.set('value', inp.getProperty('name'));
					});
				}
			}
		}).send();
	});
	
	$$('#contact-us input[type=text]').each(function(inp){
		inp.addEvents({
			'focus': function(){
				KINETIC.setInput(this);
			},
			'blur':function(){
				KINETIC.setInput(this);
			}
		});
	});
	
	$$('#contact-us textarea').each(function(inp){
		inp.addEvents({
			'focus':function(){
				KINETIC.setInput(this);
			},
			'blur':function(){
				KINETIC.setInput(this);
			}
		});
	});
	/*** END CONTACT FORM ***/
	
	/*** WORDPRESS SEARCH FORM ***/	
	if($('searchform')){
		$('s').addEvents({
			'focus':function(){
				KINETIC.setSearch();
			},
			'blur':function(){
				KINETIC.setSearch();
			}
		});
		KINETIC.setSearch();
	}
	/*** END WORDPRESS SEARCH FORM ***/
	
	/*** WORDPRESS COMMENTS ***/
	if($('commentform')){
		$$('#commentform #submit').addEvents({
			'mouseover': function(){
				this.setStyle('background-position', 'bottom left');
			},
			
			'mouseout': function(){
				this.setStyle('background-position', 'top left');
			}
		});
		
		if($('author')){
			$('author').addEvents({
				'focus': function(){
					this.setStyle('color', '#ddd');
					if(this.get('value') == '' || this.get('value') == 'Name'){
						this.set('value', '');
					}
				},
				
				'blur': function(){
					if(this.get('value') == ''){
						this.setStyle('color', '#8a8a8a');
						this.set('value', 'Name')
					}
				}
			});
			
			if($('author').get('value') == ''){
				$('author').setStyle('color', '#8a8a8a');
				$('author').set('value', 'Name');
			}
		}
		
		if($('email')){
			$('email').addEvents({
				'focus': function(){
					this.setStyle('color', '#ddd');
					if(this.get('value') == '' || this.get('value') == 'Email'){
						this.set('value', '');
					}
				},
				
				'blur': function(){
					if(this.get('value') == ''){
						this.setStyle('color', '#8a8a8a');
						this.set('value', 'Email')
					}
				}
			});
			
			if($('email').get('value') == ''){
				$('email').setStyle('color', '#8a8a8a');
				$('email').set('value', 'Email');
			}
		}
		
		if($('url')){
			$('url').addEvents({
				'focus': function(){
					this.setStyle('color', '#ddd');
					if(this.get('value') == '' || this.get('value') == 'Website'){
						this.set('value', '');
					}
				},
				
				'blur': function(){
					if(this.get('value') == ''){
						this.setStyle('color', '#8a8a8a');
						this.set('value', 'Website')
					}
				}
			});
			
			if($('url').get('value') == ''){
				$('url').setStyle('color', '#8a8a8a');
				$('url').set('value', 'Website');
			}
		}
		
		if($('comment')){
			$('comment').addEvents({
				'focus': function(){
					this.setStyle('color', '#ddd');
					if(this.get('value') == '' || this.get('value') == 'Comment'){
						this.set('value', '');
					}
				},
				
				'blur': function(){
					if(this.get('value') == ''){
						this.setStyle('color', '#8a8a8a');
						this.set('value', 'Comment')
					}
				}
			});
			
			if($('comment').get('value') == ''){
				$('comment').setStyle('color', '#8a8a8a');
				$('comment').set('value', 'Comment');
			}
		}
	}
	/*** END WORDPRESS COMMENTS ***/
	
	if(KINETIC.bodyElement.get('id') == 'blog' && $('tweets-here')){
		KINETIC.usr = 'kineticdesign';
		KINETIC.getTweets();
	}
	
	if($('kin-aurora-a') && $('kin-aurora-b')){
		KINETIC.animatedHeader(3000);
	}
});

/*/ END DOMREADY  //////////////////////////////////////////////////////////////////////////////////*/
