;
/*
 * jQuery Form Example Plugin 1.5
 * Populate form inputs with example text that disappears on focus.
 *
 * e.g.
 *  $('input#name').example('Bob Smith');
 *  $('input[@title]').example(function() {
 *    return $(this).attr('title');
 *  });
 *  $('textarea#message').example('Type your message here', {
 *    className: 'example_text'
 *  });
 *
 * Copyright (c) Paul Mucur (http://mudge.name), 2007-2011.
 * Dual-licensed under the BSD (BSD-LICENSE.txt) and GPL (GPL-LICENSE.txt)
 * licenses.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
(function(a,b){a.fn.example=function(b,c){var d=a.isFunction(b),e=a.extend({},c,{example:b});return this.each(function(){var b=a(this);if(a.metadata)var c=a.extend({},a.fn.example.defaults,b.metadata(),e);else var c=a.extend({},a.fn.example.defaults,e);a.fn.example.boundClassNames[c.className]||(a(window).bind("unload.example",function(){a("."+c.className).val("")}),a("form").bind("submit.example",function(){a(this).find("."+c.className).val("")}),a.fn.example.boundClassNames[c.className]=!0),!b.attr("value")&&(d||b.val()==c.example)&&b.val(""),b.val()==""&&this!=document.activeElement&&(b.addClass(c.className),b.val(d?c.example.call(this):c.example)),b.bind("focus.example",function(){a(this).is("."+c.className)&&(a(this).val(""),a(this).removeClass(c.className))}),b.bind("change.example",function(){a(this).is("."+c.className)&&a(this).removeClass(c.className)}),b.bind("blur.example",function(){a(this).val()==""&&(a(this).addClass(c.className),a(this).val(d?c.example.call(this):c.example))})})},a.fn.example.defaults={className:"example"},a.fn.example.boundClassNames=[]})(jQuery);
;
if (Drupal.jsEnabled) {
  $(document).ready(function(){
  
    $('form#search-block-form input.form-text').example('Find a solution', {className: 'search_example'});

  });
};
if( !String.trim || ( typeof String.trim != 'function' && typeof String.trim != 'object' ) )
{
	String.prototype.trim = function()
									{
										var ret = String( this ).replace( /^\s+/, '' );
										for( var i = ret.length - 1; i >= 0; i-- )
										{
											if( /\S/.test( ret.charAt( i ) ) )
											{
												ret = ret.substring( 0, i + 1 );
												break;
											}
										}
										i = null;
										return ret;
									};
}

if( !String.inArray || ( typeof String.inArray != 'function' && typeof String.inArray != 'object' ) )
{
	String.prototype.inArray = function( arr )
										{
											var ret = false;
											if( ( typeof arr == 'array' || typeof arr == 'object' ) && arr.length > 0 )
											{
												for( var i = 0; i < arr.length; i++ )
												{
													if( String( arr[ i ] ) == String( this ) )
													{
														ret = true;
														break;
													}
												}
												i = null;
											}
											return ret;
										};
}

function CTF_inArray( val, arr )
{
	var ret = false;
	if( typeof arr == 'array' || typeof arr == 'object' )
	{
		for( var i in arr )
		{
			if( arr[ i ] == val )
			{
				ret = true;
				break;
			}
		}
	}
	return ret;
}

if( !self.CTF_returnFalse || ( typeof self.CTF_returnFalse != 'function' && typeof self.CTF_returnFalse != 'object' ) )
{
	function CTF_returnFalse()
	{
		return false;
	}
}

function CTF_addEvent( el, event_type, attached_function, allow_propagation )
{
	if( el && event_type && attached_function )
	{
		if( typeof el.attachEvent == 'function' || typeof el.attachEvent == 'object' )
		{
			el.attachEvent( String( 'on' + event_type ), attached_function );
		}
		else
		{
			el.addEventListener( String( event_type ), attached_function, ( allow_propagation ? true : false ) );
		}
	}
}

function CTF_removeEvent( el, event_type, attached_function )
{
	if( el && event_type && attached_function )
	{
		if( typeof el.attachEvent == 'function' || typeof el.attachEvent == 'object' )
		{
			el.detachEvent( String( 'on' + event_type ), attached_function );
		}
		else
		{
			el.removeEventListener( String( event_type ), attached_function );
		}
	}
}

function CTF_getCoords( el )
{
	var ret = { x: 0, y: 0 };
	if( el )
	{
		while( el.offsetParent )
		{
			ret.x = el.offsetLeft;
			ret.y = el.offsetTop;
			el = el.offsetParent;
		}
	}
	return ret;
}

function CTF_closeSmallNote( e )
{
	var box = null;
	if( typeof e != 'string' && typeof e.type == 'string' )
	{
		box = e.srcElement || e.target;
	}
	else if( typeof e == 'string' )
	{
		box = document.getElementById( e ) || null;
	}
	if( box && box.parentNode )
	{
		var shade = document.getElementById( 'shade_of_' + box.id ) || null;
		if( shade && shade.parentNode )
		{
			shade.parentNode.removeChild( shade );
		}
		box.parentNode.removeChild( box );
	}
	box = null;
}

function CTF_smallNote( msg, can_be_closed, auto_hide_secs, attached_el, x, y )
{
	var box = document.getElementById( 'smallnote_' + escape( msg ) ) || null;
	if( !box )
	{
		box = document.createElement( 'DIV' );
		box.className = 'smallnote_box';
		box.id = 'smallnote_' + escape( msg );
	}
	if( box )
	{
		if( parseInt( x, 10 ) >= 0 && parseInt( y, 10 ) >= 0 )
		{
			if( document.documentElement && document.documentElement.scrollTop > 0 )
			{
				y += document.documentElement.scrollTop;
			}
			else if( document.body.scrollTop > 0 )
			{
				y += document.body.scrollTop;
			}
			if( document.documentElement && document.documentElement.scrollLeft > 0 )
			{
				x += document.documentElement.scrollLeft;
			}
			else if( document.body.scrollLeft > 0 )
			{
				x += document.body.scrollLeft;
			}
			box.style.left = ( parseInt( x, 10 ) - 5 ) + 'px';
			box.style.top = ( parseInt( y, 10 ) - 5 ) + 'px';
		}
		else if( attached_el )
		{
			var coords = CTF_getCoords( attached_el );
			box.style.left = coords.x + 'px';
			box.style.top = coords.y + 'px';
			coords = null;
		}
		else
		{
			// asemointi keskelle viewporttia
			var height = ( self.innerHeight && typeof self.innerHeight == 'number' ? self.innerHeight : ( self.frameElement ? self.frameElement.offsetHeight : document.documentElement.clientHeight ) );
			var width = ( self.innerWidth && typeof self.innerWidth == 'number' ? self.innerWidth : ( self.frameElement ? self.frameElement.offsetWidth : document.documentElement.clientWidth ) );
			box.style.top = Math.round( ( height / 2 ) - ( box.offsetHeight / 2 )  ) + 'px';
			box.style.left = Math.round( ( width / 2 ) - ( box.offsetWidth / 2 )  ) + 'px';
			width = null;
			height = null;
		}

		box.innerHTML = msg;
		if( can_be_closed || auto_hide_secs < 1 )
		{
			box.style.cursor = 'pointer';
			box.title = 'Click to close this message.';
			CTF_addEvent( box, 'mouseup', CTF_closeSmallNote );
		}
		if( auto_hide_secs > 0 )
		{
			self.setTimeout( 'CTF_closeSmallNote( \'' + box.id + '\' )', auto_hide_secs * 1000 );
		}
		document.body.appendChild( box );

		var shade = document.getElementById( 'shade_of_' + box.id ) || null;
		if( !shade )
		{
			shade = document.createElement( 'DIV' );
			shade.className = 'smallnote_shade';
			shade.id = 'shade_of_' + box.id;
		}
		if( shade )
		{
			shade.style.top = ( parseInt( box.style.top.replace( 'px', '' ) ) - 5 ) + 'px';
			shade.style.left = ( parseInt( box.style.left.replace( 'px', '' ) ) - 5 ) + 'px';
			shade.style.height = ( box.offsetHeight + 10 ) + 'px';
			shade.style.width = ( box.offsetWidth + 10 ) + 'px';
			document.body.appendChild( shade );
		}
		shade = null;
	}
	box = null;
}

var ctf_disabledAddButtons = [];
function CTF_disableAddToMyPage( el )
{
	el.style.cursor = 'default';
	el.src = String( el.src ).replace( '.gif', '_disabled.gif' ).replace( '.png', '_disabled.png' ).replace( '.jpg', '_disabled.jpg' );
	el.title = '';
	ctf_disabledAddButtons.push( el );
}

function CTF_triggerLoginLink()
{
	var as = document.getElementsByTagName( 'A' );
	if( as && as.length > 0 )
	{
		for( var i = 0; i < as.length; i++ )
		{
			if( as[ i ] && String( as[ i ].href ).indexOf( 'user/login' ) != -1 )
			{
				if( typeof document.createEvent == 'function' || typeof document.createEvent == 'object' )
				{
					var evt = document.createEvent( 'Event' );
					evt.initEvent( 'click', true, true );
					as[ i ].dispatchEvent( evt );
				}
				else if( typeof as[ i ].fireEvent == 'function' || typeof as[ i ].fireEvent == 'object' )
				{
					as[ i ].fireEvent( 'onclick' );
				}
				break;
			}
		}
		i = null;
	}
	as = null;
}

function CTF_addToMyPage( e, nodetype, subtype, id, msg )
{
	var el = e.srcElement || e.target;
	if( el && ( !ctf_disabledAddButtons || ctf_disabledAddButtons.length < 1 || !CTF_inArray( el, ctf_disabledAddButtons ) ) )
	{
		var a = ctf_makeAjaxCallStacktObject();
		if( a )
		{
			var params = 'n=' + nodetype + '&s=' + subtype + '&i=' + id;
			a.open( 'POST', 'sites/all/themes/ctf/add_to_my_page.php?x=' + Math.round( Math.random() * 10000000 ), true );
			a.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
			a.setRequestHeader( 'Content-length', params.length );
			a.setRequestHeader( 'Connection', 'close' );
			a.message = msg;
			a.el = el;
			a.x = e.clientX;
			a.y = e.clientY;
			a.onreadystatechange =	function()
											{
												if( ( this.readyState == 4 || this.readyState == 'complete' ) && this.status == 200 )
												{
													if( this.responseText.toLowerCase().indexOf( 'fatal error' ) == -1 && this.message != '' )
													{
														if( this.responseText.toLowerCase().indexOf( 'unauthorized' ) != -1 )
														{
															CTF_triggerLoginLink();
														}
														else
														{
															CTF_disableAddToMyPage( this.el );
															CTF_smallNote( this.message, true, 3, this.el, this.x, this.y  );
														}
													}
												}
											};
			a.send( params );
			params = null;
		}
	}
	el = null;
}

function CTF_RemoveItemFromMyPage( e, id, msg )
{
	var item_el = document.getElementById(e) || null;
	if( id > 0 && confirm( 'Are you sure you want to remove this item from My Page?' ) )
	{
		var a = ctf_makeAjaxCallStacktObject();
		if( a )
		{
			var params = 'item_id=' + id;
			a.open( 'POST', 'sites/all/themes/ctf/remove_from_my_page.php?x=' + Math.round( Math.random() * 10000000 ), true );
			a.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
			a.setRequestHeader( 'Content-length', params.length );
			a.setRequestHeader( 'Connection', 'close' );
			a.message = msg;
			a.el = item_el;
			a.onreadystatechange =	function()
									{
										if( ( this.readyState == 4 || this.readyState == 'complete' ) && this.status == 200 )
										{
											if( this.responseText.toLowerCase().indexOf( 'fatal error' ) == -1 && this.message != '' )
											{
												if( this.responseText.toLowerCase().indexOf( 'unauthorized' ) != -1 )
												{
													CTF_triggerLoginLink();
												}
												else
												{
													alert( this.message );
													a.el.style.display = "none";
												}
											}
										}
									};
			a.send( params );
			params = null;
		}
	}
	//el = null;
}

function CTF_recordThumbs( e, delta, nodetype, id, msg )
{
	var el = e.srcElement || e.target;
	if( el && ( delta == 1 || delta == -1 ) && String( nodetype ).trim() != '' && id > 0 )
	{
		var a = ctf_makeAjaxCallStacktObject();
		if( a )
		{
			var params = 'o=' + nodetype + '&i=' + id + '&t=' + delta;
			a.open( 'POST', 'sites/all/themes/ctf/give_thumb.php?x=' + Math.round( Math.random() * 10000000 ), true );
			a.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
			a.setRequestHeader( 'Content-length', params.length );
			a.setRequestHeader( 'Connection', 'close' );
			a.nodetype = nodetype;
			a.nodeid = id;
			a.message = msg;
			a.el = el;
			a.x = e.clientX;
			a.y = e.clientY;
			a.onreadystatechange =	function()
											{
												if( ( this.readyState == 4 || this.readyState == 'complete' ) && this.status == 200 )
												{
													if( this.responseText.toLowerCase().indexOf( 'fatal error' ) == -1 && this.message != '' )
													{
														if( this.responseText.toLowerCase().indexOf( 'error:' ) != -1 )
														{
															CTF_smallNote( String( this.responseText ).replace( 'error:', '' ).replace( 'ERROR:', '' ).trim(), true, 3, this.el, this.x, this.y );
														}
														else if( this.responseText.toLowerCase().indexOf( 'unauthorized' ) != -1 )
														{
															CTF_triggerLoginLink();
														}
														else
														{
															if( String( this.nodetype ).trim() != '' && this.nodeid > 0 )
															{
																var rx = /ups\=([0-9]+)\/downs\=([0-9]+)/mig;
																var res = rx.exec( this.responseText );
																if( res && parseInt( res[ 1 ], 10 ) >= 0 && parseInt( res[ 2 ], 10 ) >= 0 )
																{
																	var i_up = document.getElementById( 'thumbs_up_for_' + this.nodetype + '_' + this.nodeid ) || null;
																	var i_down = document.getElementById( 'thumbs_down_for_' + this.nodetype + '_' + this.nodeid ) || null;
																	i_up.innerHTML = res[ 1 ];
																	i_down.innerHTML = res[ 2 ];
																	i_up = null;
																	i_down = null;
																}
																res = null;
																rx = null;
															}
															CTF_smallNote( this.message, true, 3, this.el, this.x, this.y );
														}
													}
												}
											};
			a.send( params );
			params = null;
		}
		a = null;
	}
	el = null;
}

var ctf_open_ajax_calls = 0, ctf_max_ajax_calls = 5;
function ctf_makeAjaxCallStacktObject()
{
	var ret = false;
	if( self.XMLHttpRequest )
	{
		try
		{
			ret = new XMLHttpRequest();
		}
		catch( e )
		{
			ret = false;
		}
	}
	else if( self.ActiveXObject )
	{
		try
		{
			ret = new ActiveXObject( 'Msxml2.XMLHTTP' );
		}
		catch( e )
		{
			try
			{
				ret = new ActiveXObject( 'Microsoft.XMLHTTP' );
			}
			catch( e )
			{
				ret = false;
			}
		}
	}
	return ret;
}

function ctf_handleAjaxCallStack()
{
	if( ctf_ajax_call_interval && ctf_open_ajax_calls <= ctf_max_ajax_calls )
	{
		if( ctf_ajax_call_stack && ctf_ajax_call_stack.length > 0 )
		{
			var temp = ctf_ajax_call_stack;
			for( var i = 0; i < temp.length; i++ )
			{
				if( temp[ i ] && temp[ i ].el && temp[ i ].src )
				{
					var a = ctf_makeAjaxCallStacktObject();
					if( a )
					{
						/*Poistettu &-merkki x-kirjaimen edestä, koska ei toiminut my page -sivulla ajax-kysely. Ongelma johtui filu.php?&x=2121212. HUOM! Tätä pitää todennäköisesti muokata*/
						var url = temp[ i ].src;// + 'x=' + Math.round( Math.random() * 10000000 );
						a.open( 'GET', url, true );
						a.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
						a.setRequestHeader( 'Content-length', 0 );
						a.setRequestHeader( 'Connection', 'close' );
						a.replace = temp[ i ].replace;
						a.url = url;
						a.el = temp[ i ].el;
						a.function_after_success = ( temp[ i ].function_after_success ? temp[ i ].function_after_success : null );
						a.function_after_failure = ( temp[ i ].function_after_failure ? temp[ i ].function_after_failure : null );
						a.function_after = ( temp[ i ].function_after ? temp[ i ].function_after : null );
						a.onreadystatechange =	function()
														{
															if( ( this.readyState == 4 || this.readyState == 'complete' ) && this.status == 200 )
															{
																ctf_open_ajax_calls--;
																if( this.responseText.toLowerCase().indexOf( 'fatal error' ) == -1 )
																{
																	this.el.innerHTML = this.responseText;
																	if( this.replace && this.el.parentNode )
																	{
																		var safe = 0;
																		while( this.el.firstChild && safe++ < 100 )
																		{
																			this.el.parentNode.insertBefore( this.el.removeChild( this.el.firstChild ), this.el );
																		}
																		safe = null;
																		this.el.parentNode.removeChild( this.el );
																	}
																	if( this.function_after_success && ( typeof this.function_after_success == 'function' || typeof this.function_after_success == 'object' ) )
																	{
																		this.function_after_success();
																	}
																}
																else if( this.function_after_failure && ( typeof this.function_after_failure == 'function' || typeof this.function_after_failure == 'object' ) )
																{
																	this.function_after_failure();
																}
																if( this.function_after && ( typeof this.function_after == 'function' || typeof this.function_after == 'object' ) )
																{
																	this.function_after();
																}
															}
														};
						a.send( null );
						ctf_open_ajax_calls++;
						ctf_ajax_call_stack.splice( i, 1 );
						url = null;
					}
				}
				if( ctf_open_ajax_calls >= ctf_max_ajax_calls )
				{
					break;
				}
			}
			temp = null;
			i = null;
		}
		else
		{
			self.clearInterval( ctf_ajax_call_interval );
			ctf_ajax_call_interval = null;
		}
	}
}

var ctf_ajax_call_stack = [], ctf_ajax_call_interval = null;
function ctf_openAjaxSrc()
{
	var divs = document.getElementsByTagName( 'DIV' ) || null;
	if( divs && divs.length > 0 )
	{
		for( var i = 0; i < divs.length; i++ )
		{
			if( divs[ i ] && divs[ i ].className.indexOf( 'ajax_onload' ) != -1 && divs[ i ].getAttribute( 'src' ) != '' )
			{
				ctf_ajax_call_stack.push( { el: divs[ i ], src: divs[ i ].getAttribute( 'src' ), replace: ( ( divs[ i ].getAttribute( 'replace' ) == 'true' || divs[ i ].getAttribute( 'replace' ) == '1' ) ? true : false ) } );
			}
		}
		i = null;
		if( ctf_ajax_call_stack.length > 0 )
		{
			ctf_ajax_call_interval = self.setInterval( 'ctf_handleAjaxCallStack()', 1000 );
		}
	}
	divs = null;
}
if( typeof self.attachEvent == 'function' || typeof self.attachEvent == 'object' )
{
	 self.attachEvent( 'onload', ctf_openAjaxSrc );
}
else
{
	 self.addEventListener( 'load', ctf_openAjaxSrc, false );
};

