var homeSliderTimer = 8000,
		autoSlideTimer = 8000,
		autoTwitterTimer = 5000;

function openLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=anchors.length-1; i>=0; i--) {
		var theHref = anchors[i];
		if (theHref.hostname && theHref.hostname !== location.hostname)	theHref.target = "_blank";
	}	
};
		
jQuery.fn.rdy = function(func){
	this.length && func.apply(this);
	return this;
};
jQuery(document).ready(function($){
	$('html').removeClass('no-js');
	openLinks();		
	var fakeInput = document.createElement("input"),
			placeHolderSupport = ("placeholder" in fakeInput),
			clearValue = function (e) {
				if ($(e).val() === $(e).data('placeholder')) {
					$(e).val('');
				}
			};
	/*if (!placeHolderSupport) {
		$('input[placeholder]').each(function(){
			var searchField = $(this),
					originalText = searchField.attr('placeholder'),
					val = this.value;
			searchField.data('placeholder', originalText);
			if(val == '') { this.value = originalText; }else {
				searchField.addClass("placeholder")
			}
			
			searchField.bind("focus", function () { this.value = ''; }).bind("blur", function () {
				if (this.value.length === 0) {
					$(this).val(originalText).addClass("placeholder");
				}
			});
		});

		// Empties the placeholder text at form submit if it hasn't changed
		$("form").bind("submit", function () {
			clearValue($('input[placeholder]', this));
		});

		// Clear at window reload to avoid it stored in autocomplete
		$(window).bind("unload", function () {
			clearValue($('input[placeholder]', this));
		});
	}
	
	$('body').click(function(e){
		if(!$(e.target).closest('#headSearch').length){
		$('#searchInFilters').hide();	
		}
	});
	$('#s').attr('autocomplete', 'off').bind('focus', function(e){
		$('#searchInFilters').show();	
	});*/

	$('#homeSlider').rdy(function(){
		var slide = $('#homeSlider'),
				ul = $('ul', slide),
				li = $('li', ul),
				len = li.length,
				wid = li.width(),
				pag = $('<div class="pagination"/>');
				
		for(var i=0;i<len;i++){
			pag.append('<a href="#" class="i" />');
		}
		pag.delegate('a', 'click', function(e){
			$(this).addClass('s').siblings().removeClass('s');
			ul.animate({
				marginLeft: -$(this).index()*wid
			});
			return false;
		});
		$('a:first', pag).click();
		
		function nextPagination() {
			var sel = $('a.s', pag);
			if($('a:last', pag).hasClass('s')){
				$('a:first', pag).click();
			}else {
				sel.next().click();
			}			
		}; /* nextPagination */
		
		homeSliderInterval = window.setInterval(function(){nextPagination();}, homeSliderTimer);
		slide.bind('mouseenter mouseleave', function(e){
			window.clearInterval(homeSliderInterval);
			if(e.type=="mouseleave"){
				homeSliderInterval = window.setInterval(function(){nextPagination();}, homeSliderTimer);
			}
		});
		slide.append(pag);
	});




	$('#twitterUpdates').rdy(function(){
		var updates = $('#twitterUpdates'),
				count = updates.attr('data-count') || 9,
				twitterUser = updates.attr('data-user') || 'shankopotamus09';
			
		$.getJSON('http://api.twitter.com/1/statuses/user_timeline/'+twitterUser+'.json?count='+count+'&include_rts=1&callback=?', function(data){
			updates.html('<dl/>');
			$(data).each(function(i){
				var d = data[i], status;
				if($.browser.msie){
					status = d.text; // sorry ie, you suck!
				}else {
					status = d.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			      return '<a target="_blank" href="'+url+'">'+url+'</a>';
			    });
		    }
				$('<dd><table><tr><td><span class="status">'+status+'</span><span class="date"><a href="http://twitter.com/'+twitterUser+'/statuses/'+d.id+'">'+relative_time(d.created_at)+'</a></span></td></tr></table></dd>').click(function(){
					// click events?
				}).appendTo($('dl', updates));
			});
			openLinks();
			var wrapperH = updates.height(),
					li = $('dd', updates),
					liH = li.height(), //one element height
					maxPos = li.length*liH - liH*3,
					twitsWrap = $('dl', updates).wrap('<div id="twitsWrap"></div>');
					
			function at() {
				var curPos = parseInt(twitsWrap.css('marginTop'), 10) || 0;
				twitsWrap.animate({
					marginTop: -maxPos < curPos ?  "-="+liH : 0
				})
			}; /* at */
			
			autoTwitterInterval = window.setInterval(at, autoTwitterTimer);
			$('#twitterUpdates').bind('mouseenter mouseleave', function(e){
				window.clearInterval(autoTwitterInterval)
				if(e.type=='mouseleave'){
					autoTwitterInterval = window.setInterval(at, autoTwitterTimer);
				}
			});
		});
	});

		


	
});



function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}


