(function($){  
 /**
  * Plug in to tame form elements
  * @param section is a parent element containing HTML form elements
  * by Joshua Smith http://uikit.org
  */
 $.fn.uikit = function() {
	
		uikit_forms();
		uikit_windows();
		uikit_tabs();
		uikit_header();
	
	function uikit_tabs() {
		$('li[uikit_tab] a').live('click',function(){
			$(this).closest('li').siblings('li').removeClass('uikit_selected');
			$(this).closest('li').addClass('uikit_selected');
			uikit_selected_tab = $(this).closest('li').attr('uikit_tab');
			$('div[uikit_tab]:visible').slideUp(500,function(){
				$('div[uikit_tab="'+uikit_selected_tab+'"]').slideDown();				
			});	
			return false;
		});	
	}
	
	function uikit_device(arg) {
		switch (arg) {
			case 'handheld' :
				return ( $(window).width() <= 500 ) ? true : false ;
				break;
			case 'tablet' :
				return ( $(window).width() > 500 ) ? true : false ;
				break;
			case 'touch' :
				return ( navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) ) ? true : false ;
				break;
			default :
		}	
	}

	function uikit_windows() {
		$('.uikit_window_body').prepend('<div class="uikit_window_bottomleft"></div><div class="uikit_window_bottomright"></div><div class="uikit_window_bottom"></div><div class="uikit_window_left"></div><div class="uikit_window_right" />');
		$('.uikit_window_title').prepend('<div class="uikit_window_topleft"></div><div class="uikit_window_topright"></div>');
	}
	
	function uikit_twitter_timeline() {
		$.ajax({
			type:'GET',
			url:'http://api.twitter.com/1/statuses/user_timeline.json',
			data: {
				'screen_name': 'somegoodpixels',	
				'count': 5				
			},
			dataType: 'jsonp',
			success: function(tweets){
				var uikit_tweets = eval(tweets);
				for ( var i = 0 ; i < tweets.length ; i++ ) {
					$('.uikit_twitter_timeline').append('<div class="uikit_social_post"><a href="http://twitter.com/'+tweets[i].user.screen_name+'" target="_blank"><strong>'+tweets[i].user.screen_name+'</strong></a><p>'+uikit_convert_tags(tweets[i].text)+'</p><a class="uikit_time" href="http://twitter.com/#!/'+tweets[i].user.screen_name+'/status/'+tweets[i].id_str+'" target="_blank">'+uikit_convert_time('abbr',tweets[i].created_at)+'</a></div>');
				}
			},
			error: function() {
				$('.uikit_twitter_timeline').html('<p>twitter timeline error</p>');
			}
		});
	}
	
	function uikit_convert_time(format,time) {
		var uikit_time_stamp = Date.parse(time);
		var uikit_time = new Object();
		uikit_time.day = new Object();
		uikit_time.year = new Object();
		uikit_time.month = new Object();
		// ampm
			uikit_time.ampm = ( uikit_time_stamp.getHours() < 12 ) ? 'am' : 'pm';
		// weekday
			switch ( uikit_time_stamp.getDay() ) {
				case 0 :	uikit_time.day.long = 'Sunday';		uikit_time.day.short = 'Sun';		break;
				case 1 :	uikit_time.day.long = 'Monday';		uikit_time.day.short = 'Mon';		break;
				case 2 :	uikit_time.day.long = 'Tuesday';	uikit_time.day.short = 'Tue';		break;
				case 3 :	uikit_time.day.long = 'Wednesday';	uikit_time.day.short = 'Wed';		break;
				case 4 :	uikit_time.day.long = 'Thursday';	uikit_time.day.short = 'Thu';		break;
				case 5 :	uikit_time.day.long = 'Friday';		uikit_time.day.short = 'Fri';		break;
				case 6 :	uikit_time.day.long = 'Saturday';	uikit_time.day.short = 'Sat';		break;
				default :
			}
		// year
			uikit_time.year.long = uikit_time_stamp.getFullYear();
			uikit_time.year.short = (uikit_time.year.long + '').charAt(2) + '' + (uikit_time.year.long + '').charAt(3);
		// hour
			if ( uikit_time_stamp.getHours() == 0 ) {
				uikit_time.hours = 12;
			} else if ( uikit_time_stamp.getHours() > 12 ) {
				uikit_time.hours = uikit_time_stamp.getHours() - 12;
			} else {
				uikit_time.hours = uikit_time_stamp.getHours();
			}				
		// minute
			uikit_time.minutes = ( uikit_time_stamp.getMinutes() < 10 ) ? '0' + uikit_time_stamp.getMinutes() : uikit_time_stamp.getMinutes();
		// month
			switch ( uikit_time_stamp.getMonth() ) {
				case 0 :	uikit_time.month.long = 'January';		uikit_time.month.short = 'Jan';		break;
				case 1 :	uikit_time.month.long = 'February';		uikit_time.month.short = 'Feb';		break;
				case 2 :	uikit_time.month.long = 'March';		uikit_time.month.short = 'Mar';		break;
				case 3 :	uikit_time.month.long = 'April';		uikit_time.month.short = 'Apr';		break;
				case 4 :	uikit_time.month.long = 'May';			uikit_time.month.short = 'May';		break;
				case 5 :	uikit_time.month.long = 'June';			uikit_time.month.short = 'Jun';		break;
				case 6 :	uikit_time.month.long = 'July';			uikit_time.month.short = 'Jul';		break;
				case 7 :	uikit_time.month.long = 'August';		uikit_time.month.short = 'Aug';		break;
				case 8 :	uikit_time.month.long = 'September';	uikit_time.month.short = 'Sep';		break;
				case 9 :	uikit_time.month.long = 'October';		uikit_time.month.short = 'Oct';		break;
				case 10 :	uikit_time.month.long = 'November';		uikit_time.month.short = 'Nov';		break;
				case 11 :	uikit_time.month.long = 'December';		uikit_time.month.short = 'Dec';		break;
				default :
			}				
		// date		
			uikit_time.date = uikit_time_stamp.getDate();
		// today
			uikit_time.today = ( uikit_time_stamp.getFullYear() == (new Date()).getFullYear() && uikit_time_stamp.getFullYear() == (new Date()).getFullYear() && uikit_time_stamp.getDate() == (new Date()).getDate() ) ? true : false;
			
		switch ( format ) {
			case 'abbr' :
				if ( uikit_time.today ) {
					return uikit_time.hours + ':' + uikit_time.minutes + uikit_time.ampm;
				} else {
					return uikit_time.month.short + ' ' + uikit_time.date;
				}
				break;
			default :
				return time;
		}
	}
	
	function uikit_convert_tags(text) {
		var uikit_convert_links = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
		var uikit_convert_handles = /(@([\w]*[\w]))/gim;
		var uikit_convert_hash = /(#([\w]*[\w]))/gim;
		if (text) {
			uikit_converted = text.replace(uikit_convert_links, '<a href="$1" target="_blank" >$1</a>');
			uikit_converted = uikit_converted.replace(uikit_convert_handles, '<a href="http://www.twitter.com/$2" target="_blank" >$1</a>');
			uikit_converted = uikit_converted.replace(uikit_convert_hash, '<a href="http://search.twitter.com/search?q=%23$2" target="_blank" >$1</a>');
		} else {
			uikit_converted = "";
		}
		return uikit_converted;
	}
	
	function uikit_forms() { 
		$('input').each(function(){
			switch ( $(this).attr('type') ) {
				case 'checkbox' :
					$(this).addClass('uikit_input_checkbox');
					break;
				case 'radio' :
					$(this).addClass('uikit_input_radio');
					break;
				case 'file' :
					$(this).addClass('uikit_input_file').wrap('<span class="uikit_button uikit_file_input_wrapper" />');
					$(this).closest('.uikit_file_input_wrapper').append('<span>Select File</span>');
					$(this).bind('change',function(){
						uikit_update_file($(this));
					});
					break;
				case 'submit' :
					$(this).addClass('uikit_button_submit');
				case 'reset' :
				case 'button' :
					$(this).addClass('uikit_button');
					break;
				default :
					$(this).addClass('uikit_input').wrap('<div class="uikit_input_wrapper">');
			}
			if ( $(this).is(':disabled') ) {
				$(this).addClass('uikit_input_disabled');
			}
		});
		$('textarea').each(function(){
			$(this).wrap('<div class="uikit_textarea_wrapper">');
		});
		$('select').each(function(){
			var uikit_select_element = $('<div class="uikit_select" />');
			var uikit_select_button = $('<a href="#" class="uikit_button" />');
			if ( $(this).find('option:selected').html() == '' ) {
				uikit_select_button.html('Select an Option &#9662;');
			} else {
				uikit_select_button.html($(this).find('option:selected').html()+' &#9662;');
			}
			uikit_select_element.append(uikit_select_button);
			var uikit_select_list = $('<ul />');
			$(this).find('option').each(function(){
				if ( $(this).html() != '' ) {
					var new_li = $('<li />');
					var new_a = $('<a href="#" />')
					new_a.attr('uikit_option',$(this).attr('value'));
					new_a.html($(this).html());
					new_li.append(new_a);
					uikit_select_list.append(new_li);
				}				
			});
			uikit_select_element.append(uikit_select_list);
			$(this).wrap('<div class="uikit_select_wrapper">').hide();
			$(this).closest('.uikit_select_wrapper').append(uikit_select_element);
		});
		$('.uikit_select .uikit_button').live({
			'click' : function(){
				$(this).closest('.uikit_select').find('ul').css('display','inline-block');
				return false;
			}
		});
		$('.uikit_select ul').live({
			'mouseleave' : function(){
				$(this).hide();
			}
		});
		$('.uikit_select ul li a').live({
			'click' : function(){
				var option_value = $(this).attr('uikit_option');
				$(this).closest('.uikit_select').find('.uikit_button').html($(this).html()+' &#9662;')
				$(this).closest('.uikit_select_wrapper').find('select option').removeAttr('selected');
				$(this).closest('.uikit_select_wrapper').find('select option').filter(function(){
					return $(this).val() == option_value
				}).attr('selected',true);
				$(this).closest('ul').hide();
				$(this).closest('.uikit_select_wrapper').find('select').trigger('change');
				return false;
			}
		});
		$('.uikit_search input[type="submit"]').hide();
	}
	
	function uikit_update_file(input) { 
		var uikit_split_file = $(input).val().split(/\\/).pop();
		if ( uikit_split_file ) {
			$(input).closest('.uikit_file_input_wrapper').find('span').html('File: '+uikit_split_file);
		} else {
			$(input).closest('.uikit_file_input_wrapper').find('span').html('File: '+$(input).val());
		}
	}
	
	function uikit_header() {
		$('.uikit_header li').live({
			'mouseenter' : function() {
				if ( $(this).find('ul').length > 0 ) {
					$(this).addClass('uikit_header_dropdown');
				}
			},
			'mouseleave' : function() {
				if ( $(this).find('ul').length > 0 ) {
					$(this).removeClass('uikit_header_dropdown');
				}
			}
		});
	}
	
 };  
})(jQuery);
