var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;
var pos = 0;

/* DD Menu Functions */
function menu_open() {
	menu_canceltimer();
	menu_close();
	ddmenuitem = $(this).find('ul').css('visibility','visible');
}

function menu_close() {
	if(ddmenuitem)
		ddmenuitem.css('visibility','hidden');
}

function menu_timer() {
	closetimer = window.setTimeout(menu_close,timeout);
}

function menu_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function formatText(index, panel) {
	return index + "";
}

$(document).ready(function() {
	// Browser redirection for IE 6
	if($.browser.name == 'msie' && $.browser.versionX == '6') {
		window.location.replace(redirectPath);
	}

	$('#menu > li').bind('mouseover',menu_open);
	$('#menu > li').bind('mouseout',menu_timer);

	$('.anythingSlider').anythingSlider({
		easing: "easeInOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
		autoPlay: true,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
		delay: 4000,                    // How long between slide transitions in AutoPlay mode
		startStopped: false,            // If autoPlay is on, this can force it to start stopped
		animationTime: 600,             // How long the slide transition takes
		hashTags: true,                 // Should links change the hashtag in the URL?
		buildNavigation: false,         // If true, builds and list of anchor links to link to each slide
		pauseOnHover: false,            // If true, and autoPlay is enabled, the show will pause on hover
		startText: "Start",             // Start text
		stopText: "Stop",               // Stop text
		navigationFormatter: formatText // Details at the top of the file on this use (advanced use)
	});
	$("#slide-jump").click(function(){
		$('.anythingSlider').anythingSlider(6);
	});

	$(function () {
		// Cufon config
		Cufon.replace('h2',{fontFamily: 'Open Modata' });
		Cufon.replace('h1',{fontFamily: 'Open Modata' });
		/*Cufon.replace('#slider_left p',{fontFamily: 'Open Modata' });*/
		Cufon.replace('#menu h2',{fontFamily: 'Open Modata' });
		Cufon.replace('#contactPage label',{fontFamily: 'Open Modata' });
		Cufon.replace('thead th',{fontFamily: 'Open Modata' });
		Cufon.replace('tfoot th',{fontFamily: 'Open Modata' });
		Cufon.replace('#left-content h3',{fontFamily: 'Open Modata' });
		Cufon.replace('ul#filter a',{fontFamily: 'Open Modata' });
	});
	
	// Twitter Username
	var username = 'vertinix';
	$.getScript('http://twitter.com/statuses/user_timeline/' + username + '.json?callback=twitterCallback2&count=4');
});
document.onclick = menu_close;

function smartColumns() { //Create a function that calculates the smart columns
    //Reset column size to a 100% once view port has been adjusted
	$("ul.column").css({ 'width' : "100%"});

	var colWrap = $("ul.column").width(); //Get the width of row
	var colNum = Math.floor(colWrap / 200); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

	$("ul.column").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("ul.column li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	
}	

smartColumns();//Execute the function when page loads

$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
	smartColumns();
});

// Twitter API modified Functions
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>';
    });
    statusHTML.push('<p>&ldquo;'+status+'&rdquo; &ndash; <small>'+relative_time(twitters[i].created_at)+'</small></p><br>');
  }
  
  $('.loading').fadeOut(750, function() {
		 $('#latest_tweet').append($(statusHTML.join('')).hide().fadeIn(750));									
  });
}

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';
  }
}
