var SITEURL = "http://"+document.domain+"/";

$(document).ready(function(){
	
	$("img.rollover").hover(
		function() { $(this).attr("src", $(this).attr("src").split("_s.").join("_o.")); },
		function() { $(this).attr("src", $(this).attr("src").split("_o.").join("_s.")); }
	);
	
	$('#tweetlist-1').fadeIn(500);
	showTwitterStatus();

	//grabTwitterFeeds();
	Cufon.refresh();
	$('#logo').fadeOut(500);
		
});


function fontReplace() {
	Cufon.replace('.maplogo, #twitter-block, #topmenu', { hover: true });
}

function grabTwitterFeeds() {
	/* at the very bottom, grab twitter feeds */
	var script = document.createElement("script");
	script.src = "http://twitter.com/statuses/user_timeline/theframeworks.json?callback=twitterCallback2&count=5";
	script.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(script);
}


function showTwitterStatus() {
	$('a.ts').bind('click', function() {
		var tsid = $(this).attr('name');
		var i = tsid.replace('ts','');
		
		$('#twitter-block .hidden').hide();
		$('#tweetlist-'+i).fadeIn(500);
	});
	//Cufon.refresh();
}


/* TOPMENU - TOPMENU - TOPMENU - TOPMENU - TOPMENU - TOPMENU */

var menu_expand;

function showSubmenu(id, delay) {
	if (typeof(delay) == "undefined")
		var delay = 100;
	if (typeof(menu_expand) == "undefined") {
		showMenuItems('submenu'+id, delay);
		$('#mainmenu'+id).addClass('active');
	} else {
		$('#menulevel3 ul li').fadeOut().addClass('hidden');
		for (n=1; n<=3; n++) {
			hideMenuItems(id, 'submenu'+n, delay);
		}
	}
}

function hideMenuItems(id, selector, delay) {
	if ($('#'+selector).children().hasClass('show')) {
		$('#'+selector+" .show:last").fadeOut(delay, function() {
			$(this).addClass('hidden');
			$(this).removeClass('show');
			if ($(this).siblings().hasClass('show')) {
				hideMenuItems(id, selector, delay);
			} else {
				$('#menulevel1 li').removeClass('active');
				showMenuItems('submenu'+id, delay);
				$('#mainmenu'+id).addClass('active');
			}
		});

	}
}

function showMenuItems(selector, delay) {
	menu_expand = selector;
	$('#'+selector+" .hidden:first").fadeIn(delay, function() {
		$(this).addClass('show');
		$(this).removeClass('hidden');
		if ($(this).siblings().hasClass('hidden')) {
			showMenuItems(selector, delay);
		}
	});
	fontReplace();
}

/* SUBMENU - SUBMENU - SUBMENU - SUBMENU - SUBMENU - SUBMENU */

var sub_expand;

function showSubsub(id, delay) {
	$('#sticker').fadeOut();
	if (typeof(delay) == "undefined")
		var delay = 100;
	if (typeof(sub_expand) == "undefined") {
		showSubItems('subsub'+id, delay);
		$('#menuitem2-'+id).addClass('active');
	} else {
		for (n=1; n<=4; n++) {
			hideSubItems(id, 'subsub'+n, delay);
		}
	}
}

function hideSubItems(id, selector, delay) {
	if ($('#'+selector).children().hasClass('show')) {
		$('#'+selector+" .show:last").fadeOut(delay, function() {
			$(this).addClass('hidden');
			$(this).removeClass('show');
			if ($(this).siblings().hasClass('show')) {
				hideSubItems(id, selector, delay);
			} else {
				$('#menulevel2 li').removeClass('active');
				showSubItems('subsub'+id, delay);
				$('#menuitem2-'+id).addClass('active');
			}
		});

	}
}

function showSubItems(selector, delay) {
	sub_expand = selector;
	$('#'+selector+" .hidden:first").fadeIn(delay, function() {
		$(this).addClass('show');
		$(this).removeClass('hidden');
		if ($(this).siblings().hasClass('hidden')) {
			showMenuItems(selector, delay);
		}
	});
	fontReplace();
}


/* WORK - WORK - WORK - WORK - WORK - WORK - WORK - WORK - WORK - WORK */

function filterReady() {
	/* === DROP DOWN === */
	//$('.droplist').jScrollPane({scrollbarMargin:0});
	
	if ($.browser.msie) {
		$('.headlink').prepend('<div class="round-ie"></div>');
	}
	
	$('.headlink').click( function() {
		$(this).parent().find('.dropdown').toggleClass('invisible').toggleClass('visible');
	});
	
	$('.filterarea').bind('mousemove', function () {
		if($(this).find('.dropdown').hasClass('invisible') == false)
			$(this).find('.dropdown').removeClass('invisible');
	});
	$('.filterarea').bind('mouseleave', function () {
		$(this).find('.dropdown').addClass('invisible');
	});
	
	loadWorkList(section, client);
}

function resetFilter() {
	$('#work-clients .headlink').removeClass('bg-color');
	$('#work-clients .headlink .inner').html('All Clients');
}

function updateFilter(title) {
	$('#work-clients .headlink').addClass('bg-color');
	$('#work-clients .headlink .inner').html(title);
	$('#work-clients .dropdown').addClass('invisible');	
}

/* AJAX CALL */
function loadWorkList(section, client) {
	$.get(SITEURL + 'work/ajax_worklist.php', { section:section, client:client }, function(data) {
		$('#worklist').fadeOut(function() {
			$(this).html(data).slideDown(100);
		});
	});
}

function loadSearchList(section, keywords) {
	$.get(SITEURL + 'work/ajax_searchlist.php', { section:section, keywords:keywords }, function(data) {
		$('#worklist').fadeOut(function() {
			$(this).html(data).slideDown(100);
		});
	});
}

function loadPostContent(post_name) {
	$.get(SITEURL + 'work/ajax_postcontent.php', { post_name:post_name }, function(data) {
		$('#content').fadeOut(function() {
			$(this).html(data).slideDown();
		});
	});
}


function manageWorkHistory() {
	/* The following script is added to the document onready queue */
	$(function() {
		/* The "historyadd" event is triggered whenever navigation occurs. */
		$('#content').historyadd(function(e, currentHash, previousHash) {
			loadPostContent(currentHash);
		});
		/* The "history" event is triggered whenever the user presses the back button or jumps backwards or forwards in their browser's history list. */
		$('#content').history(function(e, currentHash, previousHash) {
			if(currentHash == '') {
				currentHash = latestPost;
			}
			resetFilter();
			loadPostContent(currentHash);			
		});
		/* Use getCurrent() function to get the hash for the initial load or if the user refreshes the page */
		var initialHash = $.history.getCurrent();
		
		if(initialHash == '') {
			initialHash = latestPost;
			$.history.add(latestPost);
		} else {
			loadPostContent(initialHash);
		}
		
	});
}

function scrollToTop (speed) {
	if (!speed) speed = "fast";
	$("body").animate({scrollTop:"0px"},speed);
}

function attachNavEffect(target) {
	var nav = '#listitem-'+target+' a';
	
	var h = $(nav).height() + 'px';
	var txt = $(nav).text();
	$('#currentitem').fadeOut(function(){
		$(this).text(txt);
		$(this).animate({'height':h}, function(){
			$(this).fadeIn();
		});
	});
	$(nav).fadeOut(500,function(){
		$('.listitem a.hidden').addClass('inactive').removeClass('hidden').slideDown(500);
		$(this).addClass('hidden');
	})
}


/* blog, extranet, placeholder time */
function GetTime() {
	var dt = new Date();
	var def = dt.getTimezoneOffset()/60;
	var gmt = (dt.getHours() + def);
	var ending = ":" + IfZero(dt.getMinutes()) + ":" +  IfZero(dt.getSeconds());
	
	var _GMT =check24(((gmt + (24+1)) > 24) ? ((gmt + (24+1)) - 24) : (gmt + (24+1)));
	var val = (IfZero(_GMT) + ":" + IfZero(dt.getMinutes()) + ":" + IfZero(dt.getSeconds()));
	$("#london").html( val );
	
	var det =check24(((gmt + (24-4)) > 24) ? ((gmt + (24-4)) - 24) : (gmt + (24-4)));
	val = (IfZero(det) + ending);
	$("#detroit").html( val );
	
	var syn =check24(((gmt + (24+10)) > 24) ? ((gmt + (24+10)) - 24) : (gmt + (24+10)));
	val = (IfZero(syn) + ending);
	setTimeout("GetTime()", 1000);
	$("#sydney").html( val );
}
function IfZero(num) {
	return ((num <= 9) ? ("0" + num) : num);
}
function check24(hour) {
	return (hour >= 24) ? hour - 24 : hour;
}
