
/*

-----------------------
jQuery.Mosoni.js
Created: 09/21/08
Last Modified: 09/21/08
-----------------------

*/

var errors = 0;
	
var complete = 0;

var SORT_TOGGLE = false;

var MAP;
var MAP_TOGGLE = false;

var COUNTS = new Array();
	
function disabled() {
	return false;
}

function setTabActive(toolbarId, activeId) {
	$("#"+toolbarId+" .active").removeClass("active").unbind("click", disabled);
	$("#"+activeId).addClass("active").click(disabled);
}

function hideDiv(name) {
	$("#"+name).hide();
}

function toggleDiv(name, callObject, showText, hideText) {
	toggle($("#"+name), callObject, showText, hideText);
}

function toggle(object, callObject, showText, hideText) {
	if(!showText) showText = "Show";
	if(!hideText) hideText = "Hide";
	if($(object).is(":visible")) {
		$(object).slideUp();
		if(callObject) {
			$(callObject).html($(callObject).html().replace(hideText, showText));
			$(callObject).addClass("toggleClosed");
			$(callObject).removeClass("toggleOpen");
		}
	}
	else {
		$(object).slideDown();
		if(callObject) {
			$(callObject).removeClass("toggleClosed");
			$(callObject).addClass("toggleOpen");
			$(callObject).html($(callObject).html().replace(showText, hideText));
		}
	}
}

function toggleSort(link, idClass, location) {
	if(SORT_TOGGLE) {
		$("#"+location).block({message: "<div class=\"save_window\" id=\"saveMessage\">Saving...</div>"});

		$(".sortColumn").each(function() {
			$(this).css("display", "none");
		})
		var idList = "";
		var comma = false;
		$("."+idClass).each(function() {
			if(comma) idList += ",";
			idList += $(this).text();
			comma = true;
		})
		
		$.ajax({
			type : "GET",
			url : "sortImages.xml?idList=" + idList,
			success: function(data) {
				$("#saveMessage").html("Saved...");
				setTimeout("$('#"+location+"').unblock()", 1000);
			},
			complete : function() {
				complete = 1;
			}
		});

		$(link).html("Sort");
		SORT_TOGGLE = false;
	}
	else {
		$(".sortColumn").each(function() {
			$(this).css("display", "block");
		})
		$(link).html("Save Sort");
		SORT_TOGGLE = true;
	}
}

function toggleRow(rowId, values, currentValue) {
	if(values[currentValue]) $("#"+rowId).css("display", "table-row");
	else $("#"+rowId).css("display", "none");
}

function expandInput(obj, max){
	 if (!obj.savesize) obj.savesize=obj.size;
	 obj.size=Math.min(max,Math.max(obj.savesize,obj.value.length));
}

function mirrorInput(value, className) {
	$("."+className).html(value);
}

function initYearSelect(inputId, selected, back) {
	if(!back) back = 100;
	
	var year = (new Date()).getFullYear();
	
	$("#"+inputId).addOption("", "Year")
	while(back >= 0) {
		$("#"+inputId).addOption(year-back, year-back, (year-back == selected));
		back--;
	}
}

function addInputSelection(inputId, resultId, addValue, name, removeFunction) {
	var found = false;
	var valueString = $("#"+inputId).val();
	
	if(valueString.length != 0) {
		var value = valueString.split(",");
		
		for(var i = 0; i < value.length; i++) {
			if(addValue == value[i]) {
				found = true;
				break;
			}
		}
		
		if(!found) valueString += ","+addValue;
	}
	else {
		valueString += addValue;
	}
	
	if(!found) {
		$("#"+inputId).val(valueString);

		if($("#"+resultId).length != 0) {
			if(removeFunction) $("<div class=\"removable_option\"><span>"+name+"</span></div>").click(removeFunction).appendTo($("#"+resultId));
			else $("<div class=\"removable_option\"><span>"+name+"</span></div>").click(function() {removeInputSelection(inputId, addValue);$(this).remove()}).appendTo($("#"+resultId));
		}			
	}
	
	return found;
}

function removeInputSelection(inputId, removeValue) {
	var found = false;
	var valueString = $("#"+inputId).val();
	var newValueString = "";
	
	if(valueString.length != 0) {
		var value = valueString.split(",");
		
		for(var i = 0; i < value.length; i++) {
			if(removeValue != value[i]) {
				if(newValueString.length != 0) newValueString += ",";
				newValueString += value[i];
			}
		}
	}
	
	$("#"+inputId).val(newValueString);
}

//Initializes a date picker on the given input id
function initDatePicker(input, birthday) {
	if(birthday) $.datepicker.setDefaults({
		yearRange: "-100:0"
	});
	$("#"+input).datepicker({ 
	    showOn: "both", 
	    buttonImageOnly: true
	})
}

function initColorPicker(id, color, selectFunction) {
	$('#'+id).ColorPicker({
		color: color,
		onShow: function (colpkr) {
			$(colpkr).fadeIn(500);
			return false;
		},
		onHide: function (colpkr) {
			$(colpkr).fadeOut(500);
			if(selectFunction) selectFunction($("#"+id+" div").css("backgroundColor"));
			return false;
		},
		onChange: function (hsb, hex, rgb) {
			$('#'+id+' div').css('backgroundColor', '#' + hex);
		}
	});
}

function CheckPage(page) {
	URL = window.location.href;
	if (URL.indexOf(page) != -1) {
		return true;
	}
}

//function loadCount(countName) {
//	$.get("/count.xml?countNames="+countName, function(data){
//		$(data).find("counts").find("count").each(function() {
//			writeCount($(this).find("name").text(), $(this).find("value").text());
//		});
//	});
//}
//
//function loadCounts(timeout) {
//	$.ajax({
//		type : "GET",
//		url : "/count.xml?countNames="+COUNTS.join(),
//		dataType : "xml",
//		success: function(data) {
//			$(data).find("counts").find("count").each(function() {
//				writeCount($(this).find("name").text(), $(this).find("value").text());
//			});
//		},
//		complete : function() {
//			complete = 1;
//		}
//	});
//	
//	if(timeout) setTimeout("loadCounts('"+timeout+"');", timeout);
//}
//
//function addCount(countName) {
//	COUNTS.push(countName);
//}
//
//function writeCount(countName, count) {
//	$("."+countName+"Container").each(function() {
//		if(count > 0) $(this).html("<span style=\"background: url(/images/notification_left_bg.png) no-repeat scroll 0px 0px; height: 14px; padding-left: 7px;\" class=\""+countName+"\"><span style=\"background-color: #9dabc6; height: 14px;background: url(/images/notification_right_bg.png) no-repeat scroll right 0px; color: #333333; padding-right: 7px;\" class=\""+countName+"Count\">"+count+"</span></span>");
//		else $(this).html("");
//	});
//	$("."+countName+"SimpleContainer").each(function() {
//		if(count > 0) $(this).html(count);
//		else $(this).html("");
//	});
//}

function displayMessage(divId, message, timeout) {
	$("#"+divId).html(message);
	if($("#"+divId).is(':hidden')) $("#"+divId).show();
	if(timeout) setTimeout("clearMessage('"+divId+"');", 2000);
}

function clearMessage(divId) {
	$("#"+divId).hide();
	$("#"+divId).html("");
}

function showMap(divId, address) {
	 MAP = new GMap2(document.getElementById(divId));
		
	 icon = new GIcon();
	 icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
	 icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	 icon.iconSize = new GSize(12, 20);
	 icon.shadowSize = new GSize(22, 20);
	 icon.iconAnchor = new GPoint(6, 20); 

	 geocoder = new GClientGeocoder();
	 
	 geocoder.getLatLng(address, function(point) {
		 if (!point) {
			 alert(address + " not found")
		 } else {
			   MAP.setCenter(point, 15);
			   var marker = new GMarker(point, {icon:icon, title: address});
			   MAP.addOverlay(marker);
			   MAP.panTo(point);
		 }
	 });

//	 MAP.addControl(new GLargeMapControl());
//	 MAP.addControl(new GMapTypeControl());
//	 MAP.addControl(new GScaleControl());
	 MAP.addControl(new GOverviewMapControl());
}

function toggleMap(divId, tall, short, callObject) {
	if(MAP_TOGGLE) {
		$("#"+divId).css("height", short);
		$(callObject).html("Expand");
		MAP_TOGGLE = false;
	}
	else {
		$("#"+divId).css("height", tall);
		$(callObject).html("Compress");
		MAP_TOGGLE = true;
	}
}

function scrollTable(tableId, headerHeight) {
	$("#"+tableId).height($(window).height()-64-144-headerHeight);
	$("#"+tableId).css("overflow", "auto");
}

function formatPageCurrency(selector) {
	$(selector).each(function() {
		$(this).html(formatCurrency($(this).html()));
	})
}

function setDataRowOddEvenClass(tableId, className) {
	var clazz = className?className:"data_row";
	var count = 0;
	$("#"+tableId).find("."+clazz).each(function() {
		if(count % 2 == 0) {
			$(this).removeClass("odd");
			$(this).addClass("even");
		}
		else {
			$(this).removeClass("even");
			$(this).addClass("odd");
		}

		count++;
	});
	
	if($("#"+tableId).find("."+clazz).length > 0) {
		$("#"+tableId+" > .saveOrder").css("display", "block");
		$("#"+tableId+" > .tableEmpty").css("display", "none");
	} else {
		$("#"+tableId+" > .tableEmpty").css("display", "block");
		$("#"+tableId+" > .saveOrder").css("display", "none");
	}
}

function bindNumericInput(selector) {
	$(selector).bind('keypress', function(e) {
		return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
	});
}
// EOF
function openPopUp(url, windowName, w, h, scrollbar) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar+',resizable=1';
	win = window.open(url, windowName, winprops);
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}


