/**

 * Library of javascript functions

 * 

 * @version 1.0

 */



// use jQuery with other libraries

// all calls must be made with "$j" instead of "$"

var $j = jQuery;



// ----------------------------------------------------------------------------

// general show/hide

// ----------------------------------------------------------------------------



// timer to hide an element after a some time

var timerHide = new Array();



/**

 * show an element

 * requires JQuery

 * @param el the id or jquery selector of the element to show

 * @param sp the speed to show the element ("slow", "normal", "fast")

 * @param ty the type of appearence ("slide", "fade", or "" for no effect)

 */

function show(el) {

	// parameters can be speed, effect or just speed

	var sp = '';

	var ty = '';

	if (show.arguments.length == 3) {

		sp = show.arguments[1];

		ty = show.arguments[2];

	} else if (show.arguments.length == 2) {

		sp = show.arguments[1];

	}

	if (ty == 'slide')

		$j(el+":hidden").slideDown(sp);

	else if (ty == 'fade')

		$j(el+":hidden").fadeIn(sp);

	else

		$j(el+":hidden").show(sp);

}



/**

 * hide an element

 * requires JQuery

 * @param el the id or jquery selector of the element to hide

 * @param sp the speed to show the element ("slow", "normal", "fast")

 * @param ty the type of appearence ("slide", "fade", or "" for no effect)

 */

function hide(el) {

	// parameters can be speed, effect or just speed

	var sp = '';

	var ty = '';

	if (hide.arguments.length == 3) {

		sp = hide.arguments[1];

		ty = hide.arguments[2];

	} else if (hide.arguments.length == 2) {

		sp = hide.arguments[1];

	}

	if (ty == 'slide')

		$j(el+":visible").slideUp(sp);

	else if (ty == 'fade')

		$j(el+":visible").fadeOut(sp);

	else

		$j(el+":visible").hide(sp);

}



/**

 * show an element if it is hidden, hide it if it is visible

 * requires JQuery

 * @param el the id or jquery selector of the element to show or hide

 * @param sp the speed to show the element ("slow", "normal", "fast")

 * @param ty the type of appearence ("slide", "fade", or "" for no effect)

 */

function toggle(el) {

	sp = ty = null;

	if (toggle.arguments.length == 3) {

		sp = toggle.arguments[1];

		ty = toggle.arguments[2];

	} else if (toggle.arguments.length == 2) {

		sp = toggle.arguments[1];

	}

	if ($j(el+":visible").size() > 0) {

		hide(el, sp, ty);

	} else {

		show(el, sp, ty);

	}

}



/**

 * hide all elements matching an expression, exept one

 * requires JQuery

 * @param exp the jquery expression or selector to select the div(s) to hide

 * @param except the id or jquery selector to keep

 * @param sp the speed to show the element ("slow", "normal", "fast")

 * @param ty the type of appearence ("slide", "fade", or "" for no effect)

 */

function hideAll(exp, except) {

	sp = ty = null;

	if (hideAll.arguments.length == 4) {

		sp = hideAll.arguments[2];

		ty = hideAll.arguments[3];

	} else if (hideAll.arguments.length == 3) {

		sp = hideAll.arguments[2];

	}

	if (ty == 'slide')

		$j(exp+":visible").filter(function(index) {return this.id != except;}).slideUp(sp);

	else if (ty == 'fade')

		$j(exp+":visible").fadeOut(sp);

	else

		$j(exp+":visible").hide(sp);

}



/**

 * hide an element after x seconds

 * @param el the id or jquery selector of the element to hide

 * @param sec the number of seconds to wait before hiding the element

 */

function hideAfter(el, sec) {

	timerHide[el] = setTimeout("hideAfterReal('"+el+"')", sec*1000);

}



/**

 * real call to the hide function

 * @param el the id or jquery selector of the element to hide

 */

function hideAfterReal(el) {

	hide(el);

}



/**

 * cancel the timer to hide an element after x seconds

 * @param el the id or jquery selector of the element to hide

 */

function cancelHideAfter(el) {

	if (timerHide[el] != null) {

		clearTimeout(timerHide[el]);

		timerHide[el] = null;

	}

}



// ----------------------------------------------------------------------------

// general pictures

// ----------------------------------------------------------------------------



// define a new function for jquery to preload images

if (jQuery) {

	jQuery.preloadImages = function() {

		for (var i = 0; i<arguments.length; i++) {

			jQuery("<img>").attr("src", arguments[i]);

		}

	}

}



// show a picture 

function showPicture(id, href) {

	if (href != '')

		$j(id).attr('src', href);

}



// ----------------------------------------------------------------------------

// general url

// ----------------------------------------------------------------------------



// goto an url

function gotoUrl(u) {

	document.location=u;

}



// open the link in a new window

// return false if the popup is shown, true otherwise

function openWindow(oLink, name, width, height, params) {

	var href = '';

	if (oLink.getAttribute) href = oLink.getAttribute('href');

	if (href=='') href = oLink.href;

	if (!href || href=='') href = oLink;

	var all_params = '';

	all_params += 'width=' + width + ',height=' + height;

	if (params && params!='') all_params += ','+params;

	// all is ready, open the popup and focus on it

	var oPopup = window.open(href, (name && name!='' ? name : 'popup'), all_params);

	if (oPopup) oPopup.focus();

	return (oPopup?false:true);

}



// ----------------------------------------------------------------------------

// general string

// ----------------------------------------------------------------------------



// trim the left and right spaces of a string

function trim(str) {

	return str.replace(/^\s+|\s+$/, '');

};



// returns true if the string is empty

function isEmpty(str) {

	return (str == null) || (str.length == 0);

}



// returns true if the string is a valid email

function isEmail(str) {

	if (isEmpty(str)) return false;

	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i

	return re.test(str);

}



// transform new lines to BR tags

function nl2br(s) {

	return s.replace(/\n|\r|\r\n/g,'<br />');

}



// limit a field to the number of chars

function limitChars(f, n) {

	if (f.value.length > n) {

		f.value = f.value.substr(0,n);

	}

}



// copy the content of a form field to an html element

function updateDivWithFieldContent(f, id) {

	$j("#"+id).html(nl2br(f.value));

}



// return a number formatted with zeros before the number

function formatNumber(n, l) {

	n = String(n);

	while (n.length < l) n = "0"+n;

	return n;

}



// ----------------------------------------------------------------------------

// general form

// ----------------------------------------------------------------------------



// confirm and submit the form

function confirmSubmit(theformname, theaction, thefield, theid, theconfirmation) {

	if (confirm(theconfirmation)) {

		if (document.forms[theformname]) {

			document.forms[theformname].elements[thefield].value = theid;

			document.forms[theformname].elements['act'].value = theaction;

			document.forms[theformname].submit();

		}

	}

}



// confirm the delete and submit the form

function doubleConfirmSubmit(theformname, theaction, thefield, theid, theconfirmation) {

	if (confirm("Warning!\n\nPlease read the following screen carefuly.\n\nYou will not be able to undo your action\nafter your confirmation in the next screen.\n\nIf your are not sure, please cancel now!\n")) {

		confirmSubmit(theformname, theaction, thefield, theid, theconfirmation);

	}

}



// change the text of the button and disable it

function waitSubmit(form, btn, div) {

	if (form && form.elements[btn]) {

		form.elements[btn].disabled = true;

	}

	if (div != "") {

		show(div);

	}

	return true;

}



// submit the form 

function submitForm(theformname, theaction) {

	if (document.forms[theformname]) {

		document.forms[theformname].elements['act'].value = theaction;

		document.forms[theformname].submit();

		return true;

	}

	return false;

}



// submit the form with a new value

function submitFormField(theformname, theaction, thefield, thevalue) {

	if (document.forms[theformname]) {

		document.forms[theformname].elements[thefield].value = thevalue;

		document.forms[theformname].act.value = theaction;

		document.forms[theformname].submit();

	}

}



// get basename of file

function basename(path) {

    var slash = (path.indexOf('/') > -1) ? '/' : '\\';

    var filename = path.substring(path.lastIndexOf(slash)+1,path.length);

	return filename.substring(0,filename.lastIndexOf('.'));

}



// copy the filename of a field to another field

function copyFilename(theform, thefile, thefield) {

	// copy only if field empty

	if (theform[thefield].value=="")

		theform[thefield].value = basename(theform[thefile].value);

}



// ----------------------------------------------------------------------------

// general scroll

// ----------------------------------------------------------------------------



// get xy scroll position

function getScrollXY() {

	var pos = [];

	pos[0] = self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;

	pos[1] = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;

	return pos;

}



// set xy scroll position

function setScrollXY(px, py) {

	window.scrollTo(px, py);

}



// ----------------------------------------------------------------------------

// CSS Browser Selector

// ----------------------------------------------------------------------------



// CSS Browser Selector   v0.2.5

// Documentation:         http://rafael.adm.br/css_browser_selector

// License:               http://creativecommons.org/licenses/by/2.5/

// Author:                Rafael Lima (http://rafael.adm.br)

// Contributors:          http://rafael.adm.br/css_browser_selector#contributors

var css_browser_selector = function() {

	var 

		ua=navigator.userAgent.toLowerCase(),

		is=function(t){ return ua.indexOf(t) != -1; },

		h=document.getElementsByTagName('html')[0],

		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',

		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';

	var c=b+os+' js';

	h.className += h.className?' '+c:c;

}();



// ----------------------------------------------------------------------------

// general ajax test

// ----------------------------------------------------------------------------



// test if ajax is available on the browser

// borrowed from http://www.jibbering.com/2002/4/httprequest.html

function ajaxAvailable() {

	var xmlhttp = false;

	/*@cc_on @*/

	/*@if (@_jscript_version >= 5)

	// JScript gives us Conditional compilation, we can cope with old IE versions.

	// and security blocked creation of the objects.

	try {

		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

	} catch (e) {

		try {

			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

		} catch (E) {

			xmlhttp = false;

		}

	}

	@end @*/

	// if we have no xmlhttp object, try to create it in a standard way

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {

		try {

			xmlhttp = new XMLHttpRequest();

		} catch (e) {

			xmlhttp = false;

		}

	}

	// still no xmlhttp object, maybe this is IceBrowser (java)

	if (!xmlhttp && window.createRequest) {

		try {

			xmlhttp = window.createRequest();

		} catch (e) {

			xmlhttp = false;

		}

	}

	// return result

	return !xmlhttp ? false : true;

}



// test for ajax

var ajax_available = ajaxAvailable();





// ----------------------------------------------------------------------------

// site layout functions

// ----------------------------------------------------------------------------



// keep the footer always aligned to the bottom of the page, and after #global

// requires checking window height first, then setting footer position

// credits to alistapart.com

function getWindowHeight() {

	var windowHeight = 0;

	if (typeof(window.innerHeight) == 'number') {

		windowHeight = window.innerHeight;

	} else {

		if (document.documentElement && document.documentElement.clientHeight) {

			windowHeight = document.documentElement.clientHeight;

		} else {

			if (document.body && document.body.clientHeight) {

				windowHeight = document.body.clientHeight;

			}

		}

	}

	// shorter version but not foul proof

	windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

	if (typeof(windowHeight) != 'number') windowHeight = 0;

	return windowHeight;

}

function setFooter() {

	if (document.getElementById) {

		var windowHeight = getWindowHeight();

		if (windowHeight > 0) {

			var contentHeight = document.getElementById('global').offsetHeight;

			var footerElement = document.getElementById('footer');

			var footerHeight = footerElement.offsetHeight;

			if (windowHeight - (contentHeight + footerHeight) >= 0) {

				footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';

			} else {

				footerElement.style.top = '0px';

			}

		}

	}

}



// jump menu

function MM_jumpMenu(targ,selObj,restore){ //v3.0

	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");

	if (restore) selObj.selectedIndex=0;

}



// ----------------------------------------------------------------------------

// form checking

// ----------------------------------------------------------------------------



// check if the form is correctly filled

function checkNewsletterSubscr(f) {

	if (f) {

		//var defs = "ffirstname,flastname,femail".split(',');

		var mand = "ffirstname,flastname,femail".split(',');

		/*for (var i = 0; i < defs.length; i++) {

			if (f.elements[defs[i]].value == f.elements[defs[i]].title)

				f.elements[defs[i]].value = '';

			f.elements[defs[i]].value = trim(f.elements[defs[i]].value);

		}*/

		for (var i = 0; i < mand.length; i++) {

			/*if (f.elements[mand[i]].type == 'select-one') {

				if (f.elements[mand[i]].selectedIndex <= 0) {

					f.elements[mand[i]].focus();

					return false;

				}

			} else {*/

				if (f.elements[mand[i]].value.length < 2) {

					f.elements[mand[i]].focus();

					return false;

				}

			//}

		}

		waitSubmit(f, "send", ""); // disable the send button

		return true;

	}

	return false;

}



// check if the form is correctly filled

function checkNewsletterUnSubscr(f) {

	if (f) {

		//var defs = "femail".split(',');

		var mand = "femail".split(',');

		/*for (var i = 0; i < defs.length; i++) {

			if (f.elements[defs[i]].value == f.elements[defs[i]].title)

				f.elements[defs[i]].value = '';

			f.elements[defs[i]].value = trim(f.elements[defs[i]].value);

		}*/

		for (var i = 0; i < mand.length; i++) {

			/*if (f.elements[mand[i]].type == 'select-one') {

				if (f.elements[mand[i]].selectedIndex <= 0) {

					f.elements[mand[i]].focus();

					return false;

				}

			} else {*/

				if (f.elements[mand[i]].value.length < 2) {

					f.elements[mand[i]].focus();

					return false;

				}

			//}

		}

		waitSubmit(f, "send", ""); // disable the send button

		return true;

	}

	return false;

}



// check if the form is correctly filled

function checkSendtofriends(f) {

	if (f) {

		//var defs = "femail".split(',');

		var mand = "ffrom_firstname,ffrom_email,fto_firstname,fto_email".split(',');

		/*for (var i = 0; i < defs.length; i++) {

			if (f.elements[defs[i]].value == f.elements[defs[i]].title)

				f.elements[defs[i]].value = '';

			f.elements[defs[i]].value = trim(f.elements[defs[i]].value);

		}*/

		for (var i = 0; i < mand.length; i++) {

			/*if (f.elements[mand[i]].type == 'select-one') {

				if (f.elements[mand[i]].selectedIndex <= 0) {

					f.elements[mand[i]].focus();

					return false;

				}

			} else {*/

				if (f.elements[mand[i]].value.length < 2) {

					f.elements[mand[i]].focus();

					return false;

				}

			//}

		}

		waitSubmit(f, "send", ""); // disable the send button

		return true;

	}

	return false;

}



// --

// -- picture 

// --

function switchPic(classToHide, idToShow) {
	var id=document.getElementById(idToShow);
	if(id.style.display=='none'){
		$j('.'+classToHide+':visible').fadeOut('normal');
		$j('#'+idToShow+':hidden').fadeIn('normal');
	}

}

// footerfixed //

/*--------------------------------------------------------------------------*
 *  
 *  footerFixed.js
 *  
 *  MIT-style license. 
 *  
 *  2007 Kazuma Nishihata [to-R]
 *  http://blog.webcreativepark.net
 *  
 *--------------------------------------------------------------------------*/
/*
new function(){
	
	var footerId = "footer";
	//??C?“
	function footerFixed(){
		//?h?L?…??“?g?????
		var dh = document.getElementsByTagName("body")[0].clientHeight;
		//?t?b?^[??top????????’u
		document.getElementById(footerId).style.top = "0px";
		var ft = document.getElementById(footerId).offsetTop;
		//?t?b?^[?????
		var fh = document.getElementById(footerId).offsetHeight;
		//?E?B?“?h?E?????
		if (window.innerHeight){
			var wh = window.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight != 0){
			var wh = document.documentElement.clientHeight;
		}
		if(ft+fh<wh){
			document.getElementById(footerId).style.position = "relative";
			document.getElementById(footerId).style.top = (wh-fh-ft-1)+"px";
		}
	}

	*/
	//•???T?C?Y
	function checkFontSize(func){
	
		//”?’?—v‘f??’???	
		var e = document.createElement("div");
		var s = document.createTextNode("S");
		e.appendChild(s);
		e.style.visibility="hidden"
		e.style.position="absolute"
		e.style.top="0"
		document.body.appendChild(e);
		var defHeight = e.offsetHeight;
		
		//”?’???”
		function checkBoxSize(){
			if(defHeight != e.offsetHeight){
				func();
				defHeight= e.offsetHeight;
			}
		}
		setInterval(checkBoxSize,1000)
	}
	
	//?C?x?“?g???X?i[
					  
		/*			  
	function addEvent(elm,listener,fn){
		try{
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent("on"+listener,fn);
		}
	}

	addEvent(window,"load",footerFixed);
	addEvent(window,"load",function(){
		checkFontSize(footerFixed);
	});
	addEvent(window,"resize",footerFixed);
	
}
*/
