/*
* miscStuff version .3.2
* Copyright 2000-2009 by Intellectual Reserve, Inc. All rights reserved.
* This notice may not be removed.
*/
/*
This file is for miscellaneous small scripts that don't warrant their own plugin file.  Additionally, to be included in this file the script should be:
- Relatively small (around 500 bytes)
- Have the potential to be used on the majority of pages
*/
$(document).ready(function(){


// DROPDOWN MENUS
jQuery('ul#platform-nav').superfish({
	autoArrows: false,
	hiInterval:20,
	animation: {height: 'toggle'},
	speed: 'fast'
})
.addClass("clear");


// MODAL DIALOG SETUP
// $(".modal").colorbox({speed:300, opacity:0.70});
$("#platform-authlink").click(function (e) {
	e.preventDefault();
	jQuery.modal(this.href,{
		isAjax:true,
		useShadow:true,
		title:"Sign In",
		width:520
	});
});





// LANGUAGE PICKER
$("#edit-select").editable("http://www.appelsiini.net/projects/jeditable/php/save.php", { 
    indicator : '<img src="/shared/common/platform/images/loading_taupe.gif">',
    data   : "{'English (英語)':'English (英語)','čeština (チェコ)':'čeština (チェコ)','français (フランス語)':'français (フランス語)'}",
    type   : "select",
    submit : "OK",
    style  : "inherit",
    submitdata : function() {
      return {id : 2};
    }
});





// SEARCH DROPDOWN STUFF
$("#platform-search").autocomplete("quickSearch.html", {
	delay: 100,
	width: 341,
	scroll: false,
	selectFirst: false,
	autoFill: false,
	minChars: 3,
	elementPlacement: "right",
	elementOffset: 31,
	resultsClass: "platform-ac-results",
	formatResult: function(row) {
		return ' ';
	}
}).result(function(event,item){
	str = item[1];
	//re = "</div>";
	re = str.substr(str.indexOf("<"));
	str = str.replace(re, "");
	//itemUrl = str.replace(re, "");
	itemUrl = str.replace(/\&amp;/g,'&');
	finalUrl = itemUrl;
	location.href = finalUrl;
});

// The following functions are used for submitting the quick search form to the faceted search page.

function submitQuickSearchForm( name, value ) {{
	var escapedValue = escapeQuickSearchChars(value);
	escapedValue = escapedValue.replace(/^\s*/, '').replace(/\s*$/, ''); 
	document.getElementById('quickSearch').value = '';
	if(escapedValue != '' && escapedValue.length > 1) {{
		var currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", name);
		currentElement.setAttribute("value", escapedValue);
		currentElement.setAttribute("id", name);
		document.forms['quickSearchForm'].appendChild(currentElement);
	}}
	document.quickSearchForm.submit();
}}


function escapeQuickSearchChars(unescaped) {{
	var escaped = '';
	for(i = 0; i < unescaped.length; i++) {{
		var character = unescaped.charAt(i);
		if( character == '<') escaped += '';
		else if(character == '>') escaped += '';
		else if(character == '{') escaped += '';
		else if(character == '}') escaped += '';
		else if(character == '(') escaped += '';
		else if(character == ')') escaped += '';
		else escaped += character;
	}}
	return escaped;
}}

// END OF DOCUMENT READY
});




(function($) { // hide the namespace
	// inputHint version .5.2 - 12/12/08
	// script for having some instructional text INSIDE a text field until clicked on, when it will clear out to allow for text entry.  If the field ever returns to an empty state it will put the instructions back in.
	$.fn.inputHint = function(options) {
		// define the default settings, then update them with any settings the user has provided
		var settings = jQuery.extend({
			hintClass: "text-hint"
		}, options);
	
		return this.each(function(){
			if(!$(this).hasClass("text-hint-added")){ // this makes sure to not apply the code to the same fields again if called multiple times
				$(this).bind("focus.inputHint",function(){ // do this on focus of the field
					if(this.value == $(this).attr("title")){ // if the value and the title attribute are the same, then clear the value and remove the hintClass so the user can put in their own value
						$(this).val("").removeClass(settings.hintClass);
					}
				})
				.bind("blur.inputHint",function(){ // do this onblur of the field
					if(this.value == ""){ // if the value is empty, put back in the title and hintClass
						$(this).addClass(settings.hintClass).val($(this).attr("title"));
					}
				})
				.addClass("text-hint-added");
			
				if(this.value == "" || (this.value != "" && this.value == $(this).attr("title"))){ // if the value is empty, put in the title and hintClass.  This is for when the page is loaded or refreshed.
					$(this).addClass(settings.hintClass).val($(this).attr("title"));
				}
				// need to clear out values when the form is submitted
				if(!$(this).parents("form").hasClass("inputHintSubmitDone")){ // check if this parent form already has the stuff added 
					$(this).parents("form").bind("submit.inputHint",function(){ // nope, so add it onsubmit
						$.fn.inputHint.destroy();
					}).addClass("inputHintSubmitDone");
				}
			}
		});
	};

	$.fn.inputHint.destroy = function (){
		// this destroys all instance of inputHint.  if it needs to be localized we will need to enhance it
		$(".text-hint-added").each(function(){
			$(this).unbind(".inputHint").removeClass("text-hint-added").removeClass("text-hint"); // clear out the focus/blur events, remove classes
			if(this.value == $(this).attr("title")) this.value = ""; // and if the value is the same as the title, clear it out
		});
		$("form.inputHintSubmitDone").unbind(".inputHint").removeClass("inputHintSubmitDone"); // remove bindings and class
	};
	
	$.fn.inputHint.create = function(){
		// this calls the inputHint with the most common set of form fields 
		$("input[type='text'][title],textarea[title]").inputHint();
	};
	$(function(){
		$.fn.inputHint.create(); // setup for the first time onload
	});	
})(jQuery);



