/*
	This file should include actions for elements across the site
*/

/* 
	user_block_my_links()
	Created for 5.0
	When the user is logged in, toggles the "my links" below mini profile and arrow 
*/
function user_block_my_links()
{
	var o = $('#my_links_button_arrow');
	var image = o.attr('src');
	var data = 
	{
		action: 'save',
		name: 'show_my_links',
		value: null
	};

	
	if(image.indexOf('_up') > 0) // hide
	{
		image = image.replace('_up', '_down');
		data.value = 0;
	}
	else //show
	{
		image = image.replace('_down', '_up');
		data.value = 1;
	}
	o.attr('src', image);
	$.get('/json/user_preference', data);
}

/* 
	mySlideToggle()
	Created for 5.0
	A generic function to show/hide a div and rotate the arrow 
*/
function mySlideToggle(div_id, o_this, preference_name)
{
	var o_div = $('#'+div_id);
	var o_img =	o_this.find('img');
	var image = o_img.attr('src');
	var data = 
	{
		action: 'save',
		name: null,
		value: null
	};
	
	if(o_div.css('display') == 'none')
	{
		o_div.slideDown();
		o_img.attr('src', image.replace('right', 'down'));
		data.value = 'true'; // Display it
	}
	else
	{
		o_div.slideUp();
		o_img.attr('src', image.replace('down', 'right'));
		data.value = 'false'; // hide it
	}
	if(preference_name)
	{
		data.name = preference_name;
		$.get('/json/user_preference', data);
	}
}

/* 
	CreateBookmarkLink()
	Created for 5.0
	Creates a book mark of the givin page 
*/
function CreateBookmarkLink() 
{
	title = location.title; 
	url = location.href;
	if (window.sidebar) // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	else if( window.external ) // IE Favorite
		window.external.AddFavorite( url, title);
	else if(window.opera && window.print)  // Opera Hotlist
	{
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
}

function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}

function status_bar_set(str) {
	window.status = str;
	return true; 
} 

function status_bar_reset() {
	window.status = "";
}

//
// field - form object - the input field to check the limit of the string that is passed
// maxlimit - int - Maximum number of characters that this field can contain before being trucated
// cntfield - html object
//
function textCounter(field, maxlimit, cntfield)
{	
	clearTimeout(field.zid);
	var qtext = field.value;
	if (qtext.length > maxlimit)
	{ 	
		field.value = field.value.substring(0, maxlimit);
	} 

	// Calculate the remainding number of characters
	var characters_left = maxlimit - qtext.length;
	if(characters_left < 0) {
		characters_left = 0;
	}

	if(cntfield) {
		// if an ID string was passed to the html object.
		// then select on the object to be used.
		if(typeof cntfield == "string") 
		{
			cntfield = document.getElementById(cntfield);
		}

		// Store the remanding number of characters that can be typed.
		if(cntfield.nodeName == "INPUT")
		{
			cntfield.value = characters_left; 
		}
		else 
		{
			cntfield.innerHTML = characters_left; 
		}
	}

	return true;
}

function createReferrerLink(url, text, variable)
{
	variable = variable ? variable : 'referrer'
	return '<a href="'+url+'?'+variable+'='+window.location.pathname+(window.location.search?window.location.search:'')+'">'+text+'</a>';
}

function switch_conv(newval)
{
	if(newval)
		$('#conv_box').attr("className", "check_on");
	else 
		$('#conv_box').attr("className", "check_off");

	$.get('/json/user_preference', 
		{ action:'save', name:'hide_conversational', value:newval}, 
		function () { window.location = window.location; } );

}




