var verk = {

	init: function() {
		this.ui.init();
	},

	ui: {

		_settings: {
			cycle: {
				pause: 1,
				speed: 400,
				timeout: 6500,
				next: '.controlles .next',
				prev: '.controlles .prev'
			}
		},

		init: function() {
			this.cycle();
			this.readmore();
			this.cases();
			this.social();
		},

		cases: function() {
			$('.case .content').css('opacity', 0.8);
			$('.case')
			.hover(function() {
				$(this).find('p').slideToggle();
			}, function() {
				$(this).find('p').slideToggle();
			});
		},

		cycle: function() {
			$('.images').cycle(this._settings.cycle);
			$('.slideshow').hover(function() {
				$(this).find('.controlles').fadeIn('fast');
			}, function() {
				$(this).find('.controlles').fadeOut('fast');
			});
		},

		readmore: function() {
			var i = 0;
			var chb = $('#content').height();
			$('.more').hide();
			var cha = $('#content').height();

			$('.readmore').bind('click', function() {
				i++;
				if(i%2 == 1) {
					$('#content').animate({
						height: chb + 'px'
					}, 500, function() {
						$('.more').fadeIn('slow');
						$('.readmore .btn').html('Read less');
					});
				} else {
					$('.more').fadeOut('slow', function() {
						$('.readmore .btn').html('Read more');
						$('#content').animate({
							height: cha + 'px'
						}, 500);
					});
				}
			});
		},

		social: function() {

			$('#menu-social-networks li').hoverIntent({
				timeout: 200,
				over: function() {

					// clear old content
					$('#social .wrapper').empty().fadeOut();

					if($(this).hasClass('twitter')) {
						verk.feed.get('twitter', 'verk', function(d) {
							$.each(d.data, function(idx) {
								var view = {
									date: prettyDate(d.data[idx].created_at),
									body: d.data[idx].text
								};
								var template = '\
									<div class="grid_2"> \
										<div class="date">{{date}}</div> \
										<div class="body">{{body}}</div> \
									</div>';
								var elm = Mustache.to_html(template, view);
								$(elm).appendTo('#social .wrapper');

								if(idx == 5) return false;
							});
							$('#social .wrapper').fadeIn();
						});
					}


					if($(this).hasClass('flickr')) {
						verk.feed.get('flickr', '52260305@N07', function(d) {
							$.each(d.data.items, function(idx) {
								var item = d.data.items[idx];
								var view = {
									src: item.media.m,
									alt: item.title,
									title: item.title
								};
								var template = '\
									<div class="grid_3"> \
										<div class="image"> \
											<img src="{{src}}" alt="{{alt}}" /> \
										</div> \
										<div class="title">{{title}}</div> \
									</div>';
								var elm = Mustache.to_html(template, view);
								$(elm).appendTo('#social .wrapper');

								if(idx == 3) return false;
							});
							$('#social .wrapper').fadeIn();
						});
					}


					// lets expand the content within
					$('#social').animate({
						height: 150 + 'px'
					});
				},
				out: function() {
					$('#social .wrapper').fadeOut();
					$('#social').animate({
						height: 0 + 'px'
					});
				}
			});
		}

	},

	feed: {

		_request: null,

		_options: {
			loader: '.loader',
			loaderTextComplete: 'Complete!',
			loaderTextLoading: 'Loading <img src="/wp-content/themes/verk/images/loader.gif" />'
		},


		/**
		 * Gets the feed data from the selected source
		 *
		 * @var feed string
		 * @var username string
		 * @var callback function
		 */
		get: function(feed, username, callback) {
			var data, uri;
			var _this = this;

			if(!feed && !username) {
				throw('Oh noes. We are missing a feed or username');
			}

			switch(feed) {
				case 'twitter':
					uri = _this.adapters.twitter.parse(username);
				break;
				case 'flickr':
					uri = _this.adapters.flickr.parse(username);
				break;
			}
			_this._sendRequest(uri, _this._options, callback);
		},


		destroy: function() {
			return this._request.abort();
		},


		/**
		 * Sends the request to the chosen service. Returns false,
		 * or fires a callback.
		 *
		 * @var uri string
		 * @var options object
		 * @var callback function
		 * @return mixed false/callback
		 */
		_sendRequest: function(uri, options, callback) {
			var d = {};

			$(options.loader).html(options.loaderTextLoading).fadeIn();

			this._request = $.ajax({
				url: uri,
				type: 'GET',
				dataType: 'json',
				contentType: 'application/json; charset=utf-8',

				/* callbacks */
				complete: function() {
					$(options.loader).html(options.loaderTextComplete).fadeOut();
				},
				success: function(data) {
					d.success = true;
					d.data = data;
					callback(d);
				},
				error: function() {
					d.success = false;
					return d;
				}
			});
		},


		/**
		 * adapters for different social networks
		 */
		adapters: {
			rss: {
				_api: '',
				parse: function(uri) {
					return uri;
				}
			},
			twitter: {
				_api: 'http://twitter.com/statuses/user_timeline/[USERNAME].json?callback=?',
				parse: function(username) {
					var uri = this._api.replace('[USERNAME]', username);
					return uri;
				}
			},
			github: {
				_api: 'http://github.com/api/v1/json/[USERNAME]?callback=?',
				parse: function(username) {
					var uri = this._api.replace('[USERNAME]', username);
					return uri;
				}
			},
			flickr: {
				_api: 'http://api.flickr.com/services/feeds/photos_public.gne?id=[USERNAME]&lang=en-us&format=json&jsoncallback=?',
				parse: function(username) {
					var uri = this._api.replace('[USERNAME]', username);
					return uri;
				}
			},
			facebook: { // @TODO add facebook support
				_api: '',
				parse: function(username) {
					var uri = this._api.replace('[USERNAME]', username);
					return uri;
				}
			}
		}
	}
};


/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;

	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
	jQuery.fn.prettyDate = function(){
		return this.each(function(){
			var date = prettyDate(this.title);
			if ( date )
				jQuery(this).text( date );
		});
	};


jQuery(document).ready(function($) {
	verk.init();
});
