
var minHeight; // Altezza minima della pagina
var maxHeight; // Altezza massima della pagina
var home = false;
var nElementsCart = 0;	// Numero di oggetti nel carrello
var elPerRow = 3;		// Numero di oggetti per riga nel carrello
var start = true;
var firstPageSearch = true;

(function(__global){
  if (!__global.console || (__global.console && !__global.console.log)) {
    __global.console = {
      log: (__global.opera && __global.opera.postError) ? __global.opera.postError : function(){ },
      debug: (__global.opera && __global.opera.postError) ? __global.opera.postError : function(){ }
    }
  }
})(this);

/* ___________________________________________
 * Istruzioni di onload
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 */

function delayedLoad(){
   console.debug('delayed',
      $('#wrapper').height(),
      $('.bigimg .images img:visible').length,
      $('.bigimg').height(),
      $(window).height()
   );

	if (( ($('#wrapper').height()==0) || ($('.bigimg .images img:eq(0)').height()==0) ) ){
		setTimeout(delayedLoad, 100);
	}
	else{
		minHeight = $('#wrapper').height();
      var winh = $(window).height();


      if($.browser.opera){
         winh = window.innerHeight;
      }

      var hhh = home ? 100 : 182;

      maxHeight = winh -hhh - (($('.wrapper:eq(2)').height() != null) ? $('.wrapper:eq(2)').height() + 30 : 0) ;

		// Altezza della pagina

		zoomifyDelayed();


		if (window.top!=window){

			var height = $('img[jqimg]:eq(0)').height()-($('#object .object_content .detail').height()+$('#object .object_content h2').height()+$('#object .object_content h3').height())-30;
			height = (height<250) ? 250 : height;

         ///window._typeface_js.initialize();



			$('#object .object_content .description')
				.css({overflow:'auto'})
				//.height(height);

		}



		if($('#wrapper').height() < maxHeight)
			$('#wrapper').height(maxHeight);


      start = false;


	}


}

$.fn.convertchar = function(options) {
	return this.each(function() {
		var text = $(this).text();
		text = text.replace('è',"e'");
		text = text.replace('à',"a'");
		text = text.replace('ì',"i'");
		text = text.replace('ò',"o'");
		text = text.replace('ù',"u'");
		$(this).text(text);
	});
}

function zoomifyDelayed(){
	var imgH = $('img[jqimg]:eq(0)').height();
	var tdW = 400-$(".jqzoom").width();

	//alert(tdW);

	if (tdW < 0){
		tdW = 0;
	}



	if (imgH > 80){
		//document.write(imgH);
		$('.zoomify').css({top:imgH - $('.zoomify').height(), left:tdW/2+10});
		$(".jqzoom").css({marginLeft:tdW/2, marginRight:tdW/2});
	}
	else{
		setTimeout(zoomifyDelayed, 100);
	}
}



$(document).ready(function(){

	var ie6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

	//$('.object_content h2').css('visibility', 'hidden');






	// ricerca in ajax
	if (($("#search_result").length > 0) || ($("#cart_list").length > 0)){
		ajaxSearch();
	}

	// google maps
	if ($('#GoogleMaps').length > 0){
		googleMaps();
	}




	//cycle image
	$('#rotate_object').cycle({
		fx:		'flash',
		timeout:5000,
		speed:	1000,
		height:	250,
		width:	255,
		pause:	true,
		random:	true,
		fit:	true
	});


   jQuery.fn.cycle.transitions.vmtrans=function(d,e,f){
      d.css('overflow','hidden');
      f.before.push(function(a,b,c){
         jQuery(this).show();
         c.cssBefore.left=0-b.offsetWidth;
         c.cssBefore.opacity=1;
         c.cssBefore.zIndex=1;
         //c.animOut.opacity=1;
         //c.animOut.left=a.offsetWidth
         c.animOut.opacity=0;
         c.animOut.zindex=10;
      });
      f.cssFirst={left:0, opacity:1, zIndex:1};
      f.animIn={left:0}
   };

	//cycle image
	$('.bigimg .images').cycle({
		fx:		'vmtrans',
		timeout: 5000,
		speed:	1000,
		height:	490,
		width:	700,
      sync:    false,
		pause:	false,
		random:	true,
		fit:	true
	});

	//cycle image
	$('.singlerotate').cycle({
		fx:		'scrollLeft',
		timeout:5000,
		speed:	3000,
		height:50,
		width:50,
		pause:	true,
		random:	true,
		fit:	true
	});


	//cycle news
	$('#news').cycle({
		fx:		'scrollUp',
		timeout : 10000,
		speed   : 3000,
		height  : 200,
		width   : 240,
		pause   : true,
		random  : false,
		pager   : '#newsNav',
		fit     : true
	});

	writeCart();



	$('.auction_list').click(function(){document.location=$('a',this).attr('href')}).css('cursor','pointer');


	$('#news .news').click(function(){
		document.location = $('a',this).attr('href');
	}).css('cursor','pointer').hover(function(){
		$(this).animate({color:'#f99'},500);
	},function(){
		$(this).animate({color:'#fff'},500);
	});


	$(window).resize(function(){
      if (start){
         return;
      }

      console.debug('start', start);

      var winh = $(window).height();
      if($.browser.opera){
         winh = window.innerHeight;
      }
       var hhh = home ? 100 : 182;

      maxHeight = winh - hhh - (($('.wrapper:eq(2)').height() != null) ? $('.wrapper:eq(2)').height() + 30 : 0) ;

      console.debug(winh + ' = ' +maxHeight + ' > ' + minHeight);

      if(maxHeight > minHeight)
			$('#wrapper').height(maxHeight);
	});



	$(".jqzoom").click(startZoom);



	$(".tablesorter").tablesorter({headers:{5:{sorter:false}}});
	$("input#birthday").calendar({timeSeparators:false, dateFormat:"DMY/", yearRange:'1900:2020'});

	$("input[type=text], textarea", '#contact_form')
		.hover(
			function(){$(this).addClass('hover');},
			function(){$(this).removeClass('hover');}
		)
		.focus(function(){$(this).addClass('focus')})
		.blur(function(){$(this).removeClass('focus')});



	$('.zoomify')
		.css('top', $('img[jqimg]:eq(0)').height()- $('.zoomify').height() )
		.click (function(){
			startZoom();
			$(this).hide();
		})
		.hover(
			function(){$(this).addClass('zoomifyOver');},
			function(){$(this).removeClass('zoomifyOver');}
		);



	$('.lastAuction a, div.pdf a').each(function(){
		var href = $(this).attr('href');
		$(this).parent().click(function(){
			document.location = href;
		}).css('cursor','pointer');/*.hover(function(){
			$(this).animate({color:'#900',backgroundColor:'#eee'},200);
		},function(){
			$(this).animate({color:'#000',backgroundColor:'#fff'},200);
		});*/
	});


	$('.buttoncancel, .button').hover(function(){
		$(this).css({backgroundPosition:'0 -17px'});
	},function(){
		$(this).css({backgroundPosition:'0 0'});
	});

	$('.submit, .buttonsave').hover(function(){
		$(this).css({backgroundPosition:'0 -40px', color:'#c04649'});
	},function(){
		$(this).css({backgroundPosition:'0 0', color:'#000'});
	});


	$('.download, .newsletter').click(function(){
		document.location = $('a', this).attr('href')
	}).css('cursor','pointer').hover(
		function(){$(this).addClass('download_over');},
		function(){$(this).removeClass('download_over');}
	);

	$('#browse_crag').prepend($.ajax({url: '/'+translation.language+"/auth/getLinks/", async: false, cache: false}).responseText);


	var timeBlinking = 1500;
	if ($('.blink').length>0){
		setInterval(function(){$('.blink').fadeTo(timeBlinking/2, 0).fadeTo(timeBlinking/2, 1)},timeBlinking);
	}


	if ($('.nextprevlot a').length > 0){
		$('.nextprevlot a').click(function(){
			parent.changeTitle('');
		});
		if (parent != document){
			var title = $('.object_content h2').text();
			title+= ' - '+ $('.object_content td:first').text();
			parent.changeTitle(title);
		}
	}
	$(".show_tooltip").tooltip();
   setTimeout(delayedLoad, 100);
});


function changeTitle(title){
	$('#TB_ajaxWindowTitle').text(title);
}




/* ___________________________________________
 * addToCart
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Aggiunge un elemento nel carrello e nel
 * cookie, così da non dover riutilizzare
 * il db successivamenete
 *
 * n: 			id
 * num: 		numero di oggetto
 * lot:		lotto				// :DTM. Here.
 * auction: 		asta
 * title:		titolo
 * image:		immagine
 * state:		stato del lotto
 */

function addToCart(n, num, lot, auction, title, image, state){


	// Se è in iframe passa al padre
	if (window.top!=window)
		parent.addToCart(n, num, lot, auction, title, image);

	var c = Array(n, num, lot, auction, title, image, state);
	$.cookieJar('VM_Cart').set(n, c);

	if ($('#cart').length == 0 ){
		alert('Oggetto aggiunto al carrello!');
		return;
	}

	addElement(n, num, lot, auction, title, image, state);

	nElementsCart++;
	if (nElementsCart==1){
		$('#cart, #cartTop, #cartBottom').show();
	}
}

/* ___________________________________________
 * removeToCart
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Rimuove  un elemento nel carrello.
 *
 * n: 			id dell'oggetto
 */
function removeToCart(n){
	$.cookieJar('VM_Cart').remove(n);
	$('#cartEl_'+n).hide("slow", function(){$('#cartEl_'+n).remove();});
	$('#tablecartEl_'+n+' *').fadeOut("slow", function(){$('#tablecartEl_'+n+' *').remove();});

	nElementsCart--;
	if (nElementsCart==0)
		$('#cart, #cartTop, #cartBottom').hide();
}

/* ___________________________________________
 * addElement
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Aggiunge l'oggetto nell'html del carrello.
 *
 * n: 			id
 * num: 		numero di oggetto
 * lot: 		lotto				// :DTM. Here.
 * auction: 		asta
 * title:	  	titolo
 * image:		immagine
 */

function addElement(id, num, lot, auction, title, image, state){

	if ($('#cartEl_'+id).length > 0 ){
		$('#cartEl_'+id).animate({
			backgroundColor: "#ffc",
			borderTopColor: "#fcc",
			borderBottomColor: "#fcc"
		}, "slow").animate({
			backgroundColor: "#fff",
			borderTopColor: "#fff",
			borderBottomColor: "#fff"
		}, "slow");
		return;
	}

	title = Utf8.decode(title);

	var toadd = '<div id="cartEl_'+id+'" class="element" style="display:none">'+
			image+
			(state == 'returned' ? '<img class="imgreturned" src="/public/frontend/images/icon_alert.gif">':'')+
			'Lotto n. '+lot+' &bull; Asta n. '+auction+
			' <a href="javascript:void(0)" class="removecart" onclick="removeToCart('+id+')"><img src="/public/admin/images/cross.png"></a><br />'+
			'<strong><a href="/'+translation.language+'/'+translation.object+'/'+id+'/'+stripUrl(title)+'.html">'+title+'</a></strong>'+
		'</div>';


	$('#cartelements').append(toadd);
	$('#cartEl_'+id).fadeIn('slow');

	return false;
}


/* ___________________________________________
 * writeCart
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Scrive il carrello contenuto nei cookie.
 */
function writeCart(){
	var ob = $.cookieJar('VM_Cart').toObject();
	for (var el in ob){
		nElementsCart ++;
		addElement(ob[el][0], ob[el][1], ob[el][2], ob[el][3], ob[el][4], ob[el][5], ob[el][6]);
	}
	if (nElementsCart>0){
		$('#cart, #cartTop, #cartBottom').show();

	}

}


/* ___________________________________________
 * ajaxSearch
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Funzione che converte la ricerca normale
 * in una bellissima ricerca in ajax :-)
 */

function ajaxSearch(){

	paginationLinkToAjax();

	$("#search_result a.returned, .show_tooltip").tooltip();

	$("td", "#search_result, #cart_list").each(function () {


		$('.image a, .object_title a', this).each(function(){
			$(this).attr('href', $(this).attr('href').replace(translation.object, translation.object+'/ajax')+'?TB_iframe=true');
			$(this).addClass('thickbox');
		});

		if (!firstPageSearch){
			$('a.thickbox, area.thickbox, input.thickbox', this).each(function(){
				$(this).click(function(){
					var t = this.title || this.name || null;
					var a = this.href || this.alt;
					var g = this.rel || false;
					tb_show(t,a,g);
					this.blur();
					return false;
				});

			});
		}

		if ($(this).parent().parent().attr('id') == 'search_result'){
			$(this).hover( function(){
				$(this).animate({backgroundColor: "#ccc"}, 500);
			}, function(){
				var bkcol = '#ffffff';
				if  (strpos($(this).attr('class'),'highlight')!==false)	bkcol = '#ffc';
				else if (strpos($(this).parent().attr('class'),'alternate')!==false) bkcol = '#f5f5f5';
				$(this).animate({backgroundColor: bkcol}, 100);
			});
		}

		//cycle image
		$('div.images', $(this)).cycle({
			fx:		'scrollLeft',
			timeout: 4000,
			height:	'100px',
			width:	'100px',
			pause:	true,
			random:	true,
			fit:	true
		});


	});

}


/* ___________________________________________
 * startZoom
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Inizia il processo di zoom sulle immagini
 */
var zoomStarted = false;
function startZoom(){
	if (zoomStarted) return;

	zoomStarted = true;



	$(".jqzoom").jqueryzoom({
		xzoom: parseInt($('td.object_content').width()), //zooming div default width(default width value is 200)
		yzoom: parseInt($('.jqzoom:visible img').height()-4), //zooming div default width(default height value is 200)
		offset:10, //zooming div default offset(default offset value is 10)
		position: "right", //zooming div position(default position value is "right")
		preload:1, // by default preload of big images is 1
		lens:1 // by default the lens is 1
	});

	// Riposizionamento
	$(".jqzoom").hover(function(){
		var height = parseInt($('.jqzoom:visible img').height()-4);
		var width = parseInt($('td.object_content').width());
		$('td.object_content').hide();
		var left = parseInt($('.zoomdiv').css('left'));
		if (window.top!=window){
			$('.zoomdiv').css({height:height,width:width});
		}
		else
			$('.zoomdiv').css({top:2, left:left-160, height:height,width:width});
	},function(){
		$('td.object_content').show();
	});
}


/* ___________________________________________
 * paginationLinkToAjax
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Converti i link della paginazione e
 * dell'ordinamento in link per l'ajax :)
 */

function paginationLinkToAjax(){
	var pos = 2;
	var orderbylot = $('#lotorder').length;
	$(".pagination a", "#search_result").each(function () {
		var $this = $(this);
		var href = new String($(this).attr('href'));
		var hrefs = href.split('/');
		$this.attr('href','#toplist')
		if (hrefs[0]=='http:'){
			pos=4;
		}

		href = href.replace(hrefs[pos+1],'ajax');

		$this.click( function(){
			firstPageSearch = false;
			$.ajax({
				url: href,
				success: function(html){
					$("#search_result").html(html);
					/*
					 #toplist bug ie. WHY?!?!?
					$('h1').sifr({
						path: '/public/sifr/',
						font: 'calibri',
						fontSize:32,
						width:580,
						roY: 0,
						color:'#000',
						textAlign:'left'});
					*/
					ajaxSearch();
					$('#search-waiting').fadeOut(500, function() {
						$('#search-waiting').remove();
						$('#jquery-overlay').fadeOut(500, function() { $('#jquery-overlay').remove(); });
					});

				},

				beforeSend: function(){
					var arrPageSizes = getPageSize();
					var arrPageSroll = getPageScroll()
					$('body').append('<div id="jquery-overlay"></div><div id="search-waiting"><img src="/public/shared/images/lightbox-ico-loading.gif"><br> '+ translation['wait']+'</div>');
					$('#search-waiting').css({
						left:				arrPageSizes[0]/2 -200,
						top:				825, //arrPageSroll[1]+ 200,
						zIndex:			200001
					}).fadeIn(500);

					$('#jquery-overlay').css({
						backgroundColor:	'#333',
						opacity:			0.8,
						width:				arrPageSizes[0],
						height:				arrPageSizes[1],
						position:			'absolute',
						top:					0,
						left:					0,
						zIndex:				50
					}).fadeIn();


					//$("#search_result").html('<img src="/public/shared/images/loader.gif">');
				}
			});

			//document.location = '#toplist';
			//return false;

		});

		var n = $this.text();

		if (orderbylot && !isNaN(n)){
			$this
				.attr('title', text = 'Lotti dal '+(n*20-19)+' al '+(n*20) )
				.tooltip();
		}
		//$(this).attr('href','#toplist');

	});
}

/* ___________________________________________
 * addslashes
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Aggiunge gli slash ai " alla stringa str
 */
function addslashes(str) {
	str=str.replace(/\"/g,'\\"');
	return str;
}

/* ___________________________________________
 * addslashes
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Sostituisce tutti i caratteri di un url
 * con solamente alafanumerici. I restanti li
 * converte in -
 */

function stripUrl(str){
	//str=str.replace(/ /g,'-');
	str = str.toLowerCase();
	str=str.replace(/[^a-z0-9]/gi,'-');
	str=str.replace(/[\-]+/g,'-');
	str=str.replace(/[\-]+$/g,'');
	str=str.replace(/^[\-]+/g,'');
	return str;
}


/* ___________________________________________
 * strpos
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 *  Come strpos in php
 */

function strpos( haystack, needle, offset){
	if (haystack==undefined )
		return false;
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}


/* ___________________________________________
 * googleMaps
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 *  Aggiunge Google Maps
 */

function googleMaps(){

	if(typeof(GBrowserIsCompatible)=='undefined'){
		setTimeout(googleMaps, 1000);
		return;
	}


	mapdiv = $('#GoogleMaps');

	eval('var info ='+mapdiv.attr('title'));
	mapdiv.attr('title','');


	if (GBrowserIsCompatible()) {

		var map = new GMap2(document.getElementById("GoogleMaps"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(parseFloat(info.lat),parseFloat(info.lng)),parseInt(info.zoom));

		function createMarker(point,html) {
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			});
			return marker;
		}

		for (var point in info.markers){
			var p = new GLatLng(parseFloat(info.markers[point].lat),parseFloat(info.markers[point].lng));
			var m = createMarker(p,info.markers[point].text);
			map.addOverlay(m);
		}


	}
	else {
		mapdiv.text('Sorry, the Google Maps API is not compatible with this browser');
	}
}


/* ___________________________________________
 * changeImage
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 *  Funzione per cambiare immagine da un elenco
 *  end: numero di foto
 *  add: se true passa alla successiva,
 *       se false passa alla precedente
 */
var imageCounter = 0;
function changeImage(end, add){
	$('#img'+imageCounter).hide();

	if (add){
		imageCounter++;
		if (imageCounter>end-1)
			imageCounter = 0;
	}
	else{
		imageCounter--;
		if (imageCounter<0)
				imageCounter = end-1;
	}

	$('#img'+imageCounter).show();
	$('#currentImages').text(imageCounter+1);

}

/* ___________________________________________
 * check_ajax
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * Controlla che la data pagina sia in iframe,
 * altrimenti rimanda alla pagina originale.
 */
function check_ajax(){
	if (window.top==window){
		var url = new String(document.location);
		url = url.replace('/ajax/', '/');
		document.location = url;
	}
	else{
		parent.getAjaxHeight($('#wrapper_ajax'));
		//$('#wrapper_ajax').css({height:parent.getAjaxHeight()});

		//parent.setAjaxHeight(height, $('.jqzoom img:first').height() );
		//parent.setAjaxHeight();

	}
}

var thickboxHeight = 0;

function getAjaxHeight($el){
	var height = $('#TB_iframeContent').height();
	if (height == 0){
		setTimeout(function(){getAjaxHeight($el)}, 500);
	}
	else{
		$el.css({height:height, minHeight:height});
	}

/*

	var height = $('#TB_iframeContent').height();
	if (height == 0){
		setTimeout('setAjaxHeight('+val+', '+imgval+')',100);
		return;
	}

	if ($.browser.safari){
		height+=imgval;

	}



	if (height>val){
		$('#TB_iframeContent').height(val+50);
		//$('#TB_iframeContent').css('overflow','hidden');
	}
 */

}



var Utf8 = {

	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}