// Various initialization functions
function smartColumns() { //Create a function that calculates the smart columns
	var wh = $(window).height();
	var ww = $(window).width();

	$('#scrollable').width( ww - 200 );
	$('#scrollable').height( wh - 70);

    //Reset column size to a 100% once view port has been adjusted
	//$("ul.column").css({ 'width' : "auto"});

	var colWrap = $("ul.column").width(); //Get the width of row
	var colNum = Math.floor(ww / 300); //Find how many columns of 250px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(ww / 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

	//tm.contentWidth = parseInt(colFixed) * tm.tabCount + 5;	// save content width for use by horizontal scroll pane

	//$("ul.column").css({ 'width' : '2000em'}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions. -> colWrap before
	$("ul.column li.tabWrapper").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column		

	$('.tabWrapper').height( wh - 109 ); // adjust tab height
	$('.tabListing').height( wh - 137 );

	$('#column_container').width( $("ul.column li.tabWrapper").size() * (colFixed + 15)  );
	$('#column_container').sortable({ handle: '.tabHeader'}); 							// sortable tabs
	$('#column_container').bind('sortstart', saveSortOrigin);		// change cookie on sort end
	$('#column_container').bind('sortstop', tm.saveCookie);		// change cookie on sort end
	
	tm.saveCookie();
}	

function saveSortOrigin(event, obj) {
	if(tm.isLoggedIn){
		var tabObj = obj.item;
		var index = $(event.target).find("li.tabWrapper").index(tabObj[0]);
		
		tm.sortOrigin = index;
	}
}

function startSearching(form) {
	tm.addTabs(escape(form.elements['q'].value));
	form.elements['q'].value='';
	
	return false;
}

var trendsHide = null;
var accountsHide = null;

$(document).ready(function () {								// animate the dropdowns
	$('#ftwittaccounts').hover(function () { clearTimeout(accountsHide); });
	
	$('a.accounts_head').click(function () {
		$(this).addClass('btn_main_active'); 
		$('#ftwittaccounts').show();
		$('#ftwittaccounts').parent().hover(
			function (){ }, 
			function (){ 
				clearTimeout(accountsHide);
				accountsHide = setTimeout("$('ul.menu_accounts').hide(); $('a.accounts_head').removeClass('btn_main_active');", 500);
			}
		);
		
	});
	
	$('.user_welcome > img').width(24);
	$('.user_welcome > img').height(24);
});	

function bindGroupLinks() {
	$('.gr_link').unbind('click');
	$('.gr_link').bind(
		'click',
		function()
		{
			$('.gr_link').parent().removeClass("active");
			$(this).parent().addClass("active");
			var id = $(this).parent().attr("id").substr(6);
			tm.workGroup = id;
			
			if(tm.workGroup > 0) {
				tm.wipeOut(); // remove all tabs and load group
				$.getJSON('includes/ajax/groups_get_tabs.php?callback=?', {id: tm.workGroup},
						function (data) {
							if(data && data.error) {
								$('#basic-modal-content').html("Could not get tabs for this group!");
								$('#basic-modal-content').modal( { autoResize: false, maxWidth: 300, maxHeight: 150, position: ['30%'] } );
							}
							else { 
								for(var i=data.tabs.length -1; i>=0; i--) {
									tm.addTabs(data.tabs[i]);
								}
							}
						});
			}
			return false;
		}
	);
}

$(window).resize( smartColumns ); // Execute on window resize