/*---------------------------------
 * Initiate Jquery Functions
 *---------------------------------
 */
//Tweetmeme Button
function tweetMemeButton() {
if (document.getElementById("tm_box"||"tm_box2")) {
var iframeCode="";
iframeCode += "<iframe ALLOWTRANSPARENCY=\"true\" width=\"90\" height=\"20\" frameborder=\"0\" scrolling=\"no\" src=\"http:\/\/api.tweetmeme.com\/widget.js?url=" + escape(document.URL) + "&style=compact&source=GrooveCommerce&service=bit.ly\"><\/iframe>";
		document.getElementById("tm_box").innerHTML = iframeCode;
		document.getElementById("tm_box2").innerHTML = iframeCode;
	}
}
window.onload = tweetMemeButton;

//Kick-off / Page Load Functions
$(document).ready(function(){
	//Secret plays slides
	var maxHeight = 0;
	$(".plays ul").each( function() {
		var height = $(this).height();
		if (height > maxHeight)
			maxHeight = height;
	});
	
	$(".plays").height(maxHeight);
	
	$("a.continue").click( function() {
		$(".page1").slideToggle();
		
		if ($(this).hasClass("next")) {
			$(this).html("&lt; Previous Secret Plays");
			$(".playImage .img1").fadeOut();
			$(this).addClass("prev");
			$(this).removeClass("next");
		}
		else {
			$(this).html("Continue Secret eCommerce Plays &gt;");
			$(".playImage .img1").fadeIn();
			$(this).addClass("next");
			$(this).removeClass("prev");
		}
	});
	//Add last class to remove border in css
	$('#sidenav ul li:last-child').addClass('last');
	
	//Initiate the tabs
	$("#tabs > ul").tabs({ fx: { opacity: 'toggle' } });
	
	//Zebra Stripe Jobs
	$("ul.job-openings li:odd").addClass("alt");
	
	//Fix leadgenform button
	$("#leadgenform form input[type='submit']").val("");
	$("#cformsform input[type='submit']").val("");
	
	//Fancybox
	$("a.fancybox").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300}); 
	$("a.lightbox").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300}); //legacy
	$("a.portfolio_lightbox").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300}); //legacy
	
	//New tab popup
	$("a.external-link").attr("target", "_blank");
	$("a.popup_tab").attr("target", "_blank");
	$("a.popup_external").click(function(){
		var e=window.open($(this).attr("href"), 'external_popup', 'width=970, height=500, scrollbars=yes, menubar=yes, toolbar=yes');
		e.focus();
		return false;
	});

	//AutoPopulate by Andy Stratton (http://www.andystratton.com/)
	$(".populate").each( function(){
		if ( $(this).val() == "" ) {
			$(this).val( $(this).attr("title") );
		}
	});
	$(".populate").focus(function(){
		if ( $(this).val() == $(this).attr("title") ) {
			$(this).val("");
		}
	});
	$(".populate").blur(function(){
		if ( $(this).val() == "" ) {
			$(this).val( $(this).attr("title") );
		}
	});

});


// 
// New Sorting Capabilities for Portfolio Page
// 
function sort_items( items ) {
	var tmp, title1, title2;
	var swaps = 0;
	if ( items.length > 1 ) {
		for ( var i = 1; i < items.length; i++ ) {
			title1 = $("strong.title", items[i-1].get(0)).text();
			title2 = $("strong.title", items[i].get(0)).text();
			// if 
			if ( title2.toLowerCase() < title1.toLowerCase() ) {
				tmp = items[i-1];
				items[i-1] = items[i];
				items[i] = tmp;
				swaps++;
			}
		}
		if ( swaps ) {
			items = sort_items(items);
		}
	}
	return items;
}

function sort_inactive() {
	var items = [];
	$(".work-list ul li").not(".active").each(function(){
		items.push($(this));
	});
	the_items = sort_items(items);
	for ( var i = 0; i < the_items.length; i++ ) $(".work-list ul").append(the_items[i]);
}

$(function(){
	sort_inactive();
	
	$(".tags ul li a").click(function(){
		if ( $(this).parent().hasClass("active") ) {
			$(this).parent().removeClass("active");
			$(".work-list ul li").removeClass("active");
			sort_inactive();
		}
		else {
			var tag = $(this).text();
			$(this).parent().siblings().removeClass('active');
			$(this).parent().addClass('active');
			$(".work-list ul li").removeClass('active');
			var items = [];
			$(".work-list ul li[title*=" + tag + "]").each(function(){
				items.push($(this).addClass("active"));
			});
			var the_items = sort_items(items);
			for ( var i = the_items.length-1; i >= 0 ; i-- ) $(".work-list ul").prepend(the_items[i]);
			sort_inactive();
		}
		return false;
	});
});
//
// End New Sorting.
//
