var c3d = function (){
	return {
		slide: function(el) {
			var speed = 500;
			var me = $(el);
			var top = me.css('top');
			if (top === "-30px") {
				me.animate({"top": 0}, speed);
				me.toggleClass("open");
			} else {
				me.animate({"top":"-30px"}, speed, function(){ me.toggleClass("open"); });
			}
			
		}
		
	} // end return
}();

jQuery.fn.hint = function(blurClass) {
	if (!blurClass) { 
	 	blurClass = "blur";
	}

	return this.each(function () {
		// get jQuery version of 'this'
		var $input = jQuery(this),
	
		// capture the rest of the variable to allow for reuse
		  title = $input.attr('title'),
		  $form = jQuery(this.form),
		  $win = jQuery(window);
	
		function remove() {
		  if ($input.val() === title && $input.hasClass(blurClass)) {
			$input.val('').removeClass(blurClass);
		  }
		}
	
		// only apply logic if the element has the attribute
		if (title) { 
		  // on blur, set value to title attr if text is blank
		  $input.blur(function () {
			if (this.value === '') {
			  $input.val(title).addClass(blurClass);
			}
		  }).focus(remove).blur(); // now change all inputs to title
	
		  // clear the pre-defined text when form is submitted
		  $form.submit(remove);
		  $win.unload(remove); // handles Firefox's autocomplete
		}
  });

}

$(document).ready(function(){
	
	$('input[type=date]').datePicker().val(new Date().asString());
	
	$('#tab_search').click(function(){
		c3d.slide('#search');
		return false;
	});
	$('#tab_signup').click(function(){
		c3d.slide('#enews');
		return false;
	});
	$(".tabs input").hint();
	
	$("#mainPromos")
		.append('<div class="navi"></div>')
		.scrollable({
					size: 1,
					clickable:false,
					items:"p"
					})
		.circular()
		.autoscroll({
					autoplay: true
					,autopause: true
					})
		.navigator({
				idPrefix:"navi"
			});
	$("#sortby").change(function(){
		var url = window.location.toString();
		var $this = $(this);
		if(url.substr(url.lastIndexOf("list")).length > 5){
			// replace url
			//alert("replace");

			window.location = (url.substring(0,url.lastIndexOf("list") + 5) + $this.val() + ($this.val().length > 0 ? "/" : ""));
		} else {
			// append to url
			//alert("append");
			window.location = window.location + $this.val() + "/";
		}
	});
	
	
	var imgGallery = $('a[rel=pop]');
	if(imgGallery.size()) {
		imgGallery.hover(function() {
								  var href = $(this).attr("href");
								  $("#previewImg").attr("src", href.replace("large", "medium"))
								  				.closest("a").attr("href", href);
								  })
			.overlay({expose:"#505050",target: '#pop'}).gallery({speed:800,autohide:false});
	}
});

/*===========================================
	FROM: twitter.com/javascripts/blogger.js
	Modified: Lane Roberts, Corporate3Design
  ===========================================*/
function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
	var source = twitters[i].source;
    statusHTML.push('<li><span>'+status+'</span><br /><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+c3d_time(twitters[i].created_at)+'</a> from ' + source + '</li>');
  }
  $('#twitter_update_list').html( statusHTML.join('') );
}

function c3d_time(time_value) {
	var monthNames = [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	];
	var values = time_value.split(" ");
	//time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	//Fri Jul 31 2009 10:11:48 GMT-0500 (CST)
	//Fri Jul 31 15:11:48 +0000 2009
	// 0   1  2      3      4     5
	var parsed_date = Date.parse(time_value);
	var d = new Date();
	d.setTime(parsed_date);
	var str = (d.getHours() > 12) ? d.getHours() - 12 + ':' + ( (d.getMinutes() < 9) ? '0' + d.getMinutes() : d.getMinutes() ) + ' PM ' : d.getHours() + ':' + ( (d.getMinutes() < 9) ? '0' + d.getMinutes() : d.getMinutes() )  + ' AM ';
	str += monthNames[d.getMonth()] + ' ' + d.getDate();
	return str;
}
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';
  }
}