/* ========================================================================= */
/* BE SURE TO COMMENT CODE/IDENTIFY PER PLUGIN CALL                          */
/* ========================================================================= */

    $(document).ready(function(){
    
        $('input, textarea').clearField({
			blurClass: 'myBlurredClass',
			activeClass: 'myActiveClass'
		});
		var tabindex = 1;
    $('input,select,textarea').each(function() {
        if (this.type != "hidden") {
            var $input = $(this);
            $input.attr("tabindex", tabindex);
            tabindex++;
        }
    });
    
		$('.slides').cycle({
            fx:      'fade',
            timeout:  8000,
            speed: 600,
            prev:    '#banner-left',
            next:    '#banner-right',
            pager:   '#banner-nav ul',
            pagerAnchorBuilder: function(idx, slide) { 
                // return selector string for existing anchor 
                return '#banner-nav ul li:eq(' + idx + ') a'; 
            }
        });
        
        $('#news').cycle({
            fx:      'fade',
            timeout:  8000,
            speed: 400,
            cleartype: true,
            pager:   '.news_pag',
            pagerAnchorBuilder: function(idx, slide) { 
                return '<a href="' + idx + '"></a>'; 
            }
        });
        
        $('#testis').cycle({
            fx:      'fade',
            timeout:  8000,
            speed: 400,
            cleartype: true,
            pager:   '.testi_pag',
            pagerAnchorBuilder: function(idx, slide) { 
                return '<a href="' + idx + '"></a>'; 
            }
        });
        
        
        /* Twitter Calls */
 	    if ( $( '#tweets-loading' ).length > 0 ) {	// only do if twitter container exists (saves us an http request)
	 	    var username = 'LockPath';
			var count = 3;
			var maxResults = 1;
			
		    $.getJSON('http://twitter.com/status/user_timeline/'+ username +'.json?count='+ count +'&include_rts=1&callback=?', function(data) {
		        for (i = 0; i < maxResults; i++ ) {
		            var text = data[i].text;
		            var timestamp = data[i].created_at;
		            var tweetId = data[i].id;
		            var tweetLink = 'http://twitter.com/#!/' + username;
		            var tweet = '<p class="tweet">' + format_tweet(text) + '<br /><a target="_blank" href="' + tweetLink + '">' + get_relative_time(timestamp) + '</a></p>';
		            $('#tweets-loading').replaceWith( tweet );
		        }
		    });   
        }
        
        
	});



// Get relative time for Twitter (e.g. 3 days ago)
	function get_relative_time(timestamp)
	{
		var now = new Date();
		var offset = now.getTimezoneOffset() * 60 * 1000;
		var dparts = timestamp.split(' ');
		var tparts = dparts[3].split(':');
		var then = new Date(dparts[1] +' '+ ' '+ dparts[2] +', '+ dparts[5] +' '+ tparts[0] +':'+ tparts[1] + ':'+ tparts[2]);
		var diff = now.getTime() - then.getTime() + offset;
		var timeDiff = get_timestamp_desc(diff, 'day', 86400000);
		
		if (false === timeDiff) {
			timeDiff = get_timestamp_desc(diff, 'hour', 3600000);
			if (false === timeDiff) {
				timeDiff = get_timestamp_desc(diff, 'minute', 60000);
				if (false === timeDiff) {
					timeDiff = get_timestamp_desc(diff, 'second', 60);
					if (false === timeDiff) {
						timeDiff = 'just now';
					}
				}
			}
		}
	 
		return timeDiff; 
	}
	
	// Create actual links using some fancy regular expressions
	function format_tweet(text) {
	    text = text.replace(/http:\/\/([^\s]+)?/ig, '<a href="http://$1" target="_blank">http://$1</a>');
	    text = text.replace(/\x40([^\s]+)/ig, '@<a href="http://twitter.com/$1" target="_blank">$1</a>');
	    text = text.replace(/\x23([^\s]+)/ig, '#<a href="http://twitter.com/#!/search/$1" target="_blank">$1</a>');
	    return text;
	}
	
	// Helper for the get_relative_time() function
	function get_timestamp_desc(diff, unit, timeDivisor)
	{ 
		var unitAmount = parseInt(diff / timeDivisor);
		if (unitAmount > 0) {
			return unitAmount + ' ' + unit + (unitAmount == 1 ? '': 's') + ' ago';
		} else if (unitAmount < 0) {
			return 'in ' + Math.abs(unitAmount) + ' ' + unit + (unitAmount == 1 ? '' : 's');
		} else {
			return false;
		}
	}
