// Extend jQuery with classVar function. 
// $(this).classVar("max") on object <span class="max_500" /> returns "500"

(function($){
  $.fn.classVar = function(s) {
  	//if (this.attr("className"))
  	//{
		//alert(this.val());
		
			var classes = this.attr("className").split(/\s/);
			for (var i = 0, len = classes.length; i < len; i++) 
				if (classes[i].toLowerCase().indexOf(s+"_")==0) return classes[i].substr(s.length+1);
			return false;
	//}

  };
})(jQuery);

// intVal function returns 0 if parameter is not a number, 
// otherwise return parameter value as an integer

function intVal (s) {
if (parseInt(s)!=isNaN)
	return parseInt(s)
else
	return 0;
};

$(document).ready(function() {

	// Convert text fields with class "fancyslider" to a jQuery UI slider
	$(".fancyslider")
		.each(function() 
		{
			setOptionFromClassVar = function(opt)
			{ 
				//alert($(this).classVar("min"))
			
				$(this).prev().slider("option", opt, 10);
			};
			
			$(this)
				.hide()
				.before("<div id='"+$(this).attr("id")+"_fancyslider'></div>")
				.prev()
					.slider( 
					{ 
						min: intVal($(this).classVar("min")),
						max: intVal($(this).classVar("max")),
						step: intVal($(this).classVar("step")),
						value: intVal($(this).val()),
						change: function(e, ui) {
							$(this).next().val($(this).slider("option", "value"));
						}
					});
			//$(this)
			//	.hide();
		});

	
	// Do fancy title replacement
	$("input[title]")
		.bind("focus keydown mousedown mouseup clearfield", function () { $(this).removeClass("inactive"); if ($(this).val() == $(this).attr("title")) { $(this).val(""); } })
		.bind("blur", function () { if ($(this).val() == '' || $(this).val() == $(this).attr("title")) $(this).addClass("inactive").val($(this).attr("title")); })
		.trigger("blur")
		.parents("form")
			.submit(function(e) 
			{
				var self = this;
				e.preventDefault();
				$(this).find("input[title]").trigger("clearfield");
				self.submit();
				return false;
			});
	
	// Do image gallery
	$(".presentation_photos a, a.fancybox_photo").fancybox(
	{
		'hideOnContentClick': true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'centerOnScroll'  : true,
		'overlayColor': '#000'

	});
	
	//Create tooltips
	$("dfn[title]").qtip({
		show: 'mouseover',
		hide: 'mouseout',
		style: { 
			border: {
				width: 0,
				radius: 4
			},
			name: 'dark',
			tip: true,
			fontSize: '12px',
			lineHeight: 'normal'
		},
		position: {
			corner: {
				tooltip: 'leftMiddle', // Use the corner...
				target: 'rightMiddle' // ...and opposite corner
			}
		}
	});
});
