function showNotes(args)
{
		var winStr = "/notes.php?" + args;
		noteWin = window.open(winStr, "Notes", "toolbar=0,location=0,status=0,menubar=0,scrollbars=yes,width=640,height=480");
		noteWin.resizeTo(650, 800);
		noteWin.focus();
}


function showNotePreview(id)
{
	if(document.getElementById(id).style.display=='block') {
		document.getElementById(id).style.display='none';
	} else {
		document.getElementById(id).style.display='block';
	}
}
function showLinkDetail(id)
{
	if(document.getElementById(id).style.display=='block') {
		document.getElementById(id).style.display='none';
	} else {
		document.getElementById(id).style.display='block';
	}
}

function showDocTagEdit(id)
{
	tags='tags_' + id;
	document.getElementById(tags).style.background='#FFFF99';
	document.getElementById('tagpencil_'+id).style.display='block';


}

function ToggleElement(id)
{
	if(document.getElementById(id).style.display=='') {
		document.getElementById(id).style.display='none';
	} else {
		document.getElementById(id).style.display='';
	}
}



function hideDocTagEdit(id)
{
	tags='tags_' + id;
	document.getElementById(tags).style.background='#FFFFFF';
	document.getElementById('tagpencil_'+id).style.display='none';


}
function editDocTags(id)
{
	document.getElementById('tags_'+id).style.display='none';
	document.getElementById('edittags_'+id).style.display='block';
}

var currentdocid;
var http;
function saveDocTags(id)
{
	formname='docform'+id;
	newtags=document.forms['docform'+id].elements['edittag_'+id].value;


	currentdocid=id;
	document.getElementById('tags_'+currentdocid).style.display='block';
	document.getElementById('edittags_'+currentdocid).style.display='none';
	document.getElementById('tagdata_'+currentdocid).innerHTML='Saving...';

	http=createRequestObject();
    http.open('get', '/docrpc.php?docid='+id+'&action=updatetags&newtags='+newtags);
    http.onreadystatechange = handleSaveDocTags;
    http.send(null);
}

function handleSaveDocTags()
{
    if(http.readyState == 4){
        var response = http.responseText;
		document.getElementById('tagdata_'+currentdocid).innerHTML=response;

    }


}

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

window.name = 'winparent';
function openNewWindow(url) {
		revWin = window.open(url, "newWin");
		revWin.focus();
}

function openHelpScreen(url) {
		helpWin = window.open(url, "Help", "toolbar=0,location=0,status=0,menubar=0,scrollbars=yes,width=640,height=480");
		helpWin.focus();
}


//Drag & Drop functions
var dragObject = null;
var mouseOffset = null;

// this function can probably be depreciated, as any element that has class="draggable" will now be made draggable
/** Main drag & drop function.
 *  @param item A reference to the element to be made moveable
 *  @param handle A string containing the name of a subelement which can be used to drag 'item'.  If none specified, then the entire 'item' can be used
 */
function makeDraggable(item,handle){
	if(!item) return;
	if (!handle) handle = item;
	else handle = document.getElementById(handle);
	handle.parentContainer = item;
	handle.onmousedown = function(ev){
		dragObject  = this.parentContainer;
		mouseOffset = getMouseOffset(this.parentContainer, ev);
		document.onmousemove = mouseMove;
		document.onmouseup = EndDrag;
	}
}

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos = getPosition(target);
	var mousePos = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

function getPosition(e){
	var left = 0;
	var top = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top += e.offsetTop;
		e = e.offsetParent;
	}

	left += e.offsetLeft;
	top += e.offsetTop;

	return {x:left, y:top};
}

function mouseMove(ev){
	ev = ev || window.event;
	var mousePos = mouseCoords(ev);
	dragObject.style.left = mousePos.x - mouseOffset.x;
	dragObject.style.top = mousePos.y - mouseOffset.y;
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX,
		y:ev.clientY
	};
}

function EndDrag(){
	document.onmousemove = null;
	document.onmouseup = null;
	dragObject = null;
}

var clickItem = function(itemid) {
	itemid = $.trim(itemid);
	var itemdialog = $("div#itemdialog");
	if (itemdialog.length < 1) {
		var str = '<div id="itemdialog" style="display:none"></div>';
		$("body").append(str);
		itemdialog = $("div#itemdialog");
	}
	
	itemdialog.dialog({autoOpen: false, width:825, height:475, position:['right','bottom']});
	
	itemdialog.html('<span style="position:relative;font-size:x-large;top:35%;margin-left:50%;margin-right:50%">\
									<img src="/images/ajax/circle.loading.gif" alt="loading...">\
								</span>');
	$.get("/upccheck.php", { 
		partnos : escape(itemid),
		show : 'bare'
	}, function(data) {
		itemdialog.html(data);
		itemdialog.dialog('option', 'title', itemid + ' <a target="_new" href="/upccheck.php?partnos=' + escape(itemid) + '">Open in new tab</a>');
	});
	itemdialog.dialog('open');
}

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}
function wordwrap(str, int_width, str_break, cut) {
	var m = ((arguments.length >= 2) ? arguments[1] : 75   );
	var b = ((arguments.length >= 3) ? arguments[2] : "\n" );
	var c = ((arguments.length >= 4) ? arguments[3] : false);
	var i, j, l, s, r;
	str += '';
	if (m < 1) {
		return str;
	}
	for (i = -1, l = (r = str.split(/\r\n|\n|\r/)).length; ++i < l; r[i] += s) {
		for (s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")){
			j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
		}
	}
    return r.join("\n");
}

$(document).ready(function(){
	/* not working properly with this plugin:
	$.lazy({
		src: '/js/jgrowl/jquery.jgrowl_compressed.js',
		name: 'jGrowl',
		dependencies: {
			css: ['/js/jgrowl/jquery.jgrowl.css']
		}
	});
	*/
	$.lazy({
		src: '/js/jquery.floatheader.min.js',
		name: 'floatHeader'
	});
	$.lazy({
		src: '/js/jquery.autogrow.js',
		name: 'autogrow'
	});
	$.lazy({
		src: '/js/jquery.autocomplete.pack.js',
		name: 'autocomplete',
		dependencies: {
			css: ['/js/jquery.autocomplete.css']
		}
	});
	$.lazy({
		src: '/js/jquery.example.min.js',
		name: 'example'
	});
	$.lazy({
		src: '/js/jquery.tablesorter.patched.js',
		name: 'tablesorter'
	});
	$.lazy({
		src: '/js/jquery.tooltip.js',
		name: 'tooltip',
		dependencies: {
			css: ['/js/jquery.tooltip.css']
		}
	});
	$.lazy({
		src: '/js/jquery.heatcolor.pack.js',
		name: 'heatcolor'
	});
	$.lazy({
		src: '/js/jquery.jcontext.js',
		name: 'showMenu'
	});
	$.lazy({
		src: '/js/jquery.jScale.js',
		name: 'jScale'
	});
	$.lazy({
		src: '/js/jquery.scrollTo-min.js',
		name: 'scrollTo'
	});
	
	var sidebar = $("ul#idSidebar");
	$(window).bind('resize', function() {
		if ($(window).height() >= sidebar.height()) {
			sidebar.css({'position':'fixed'});
		} else {
			sidebar.css({'position':'absolute'});
		}
	}).trigger('resize');
	var draggable = $(".draggable");
	var resizable = $(".resizable");
	
	var productinfo = $(".productinfo");
	if (draggable.length > 0)
		draggable.draggable({containment: 'document', scroll: false });
	if ( resizable.length > 0) {
		 resizable.resizable();
	}
	
	$.each($.browser, function(i, val) {
		if(i=="mozilla") {
			var autogrow = $("textarea.autogrow");
			if (autogrow.length > 0) {
				autogrow.autogrow();
			}
		}
	});
	
	if (productinfo.length > 0) {
		productinfo.click(function(){
			clickItem($(this).html())
		});
		productinfo.hover(function(){
			$(this).css("cursor","pointer");
		});
	}
	
});

function saveUserPref(pref,value){
	var url = "/rpc/userprefs/save";
	url = url + "/pref/" + escape(pref);
	url = url + "/value/" + escape(value);

	var callback = {
		success:function(o){}
	};
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);

}