Data = new Array();
AddIndex = 1;
Timer = null;
TopImage = 2;
SlideInterval = 3000;
isFirstRun = true;
direction = 'forward';

/* parse XML */
function parseXML(url) {
	clearTimeout(Timer);
	AddIndex = 1;
	Data.length = 0;
	
	$.ajax( {
		type :"GET",
		url : url,
		dataType :"xml",
		success : function(xml) {
			pages = xml.documentElement.getElementsByTagName('page');

			for ( var i = 0; i < pages.length; i++) {
				page = pages.item(i);
				// Getting pages attr
				id = page.getAttribute('id');
				name = page.getAttribute('name');
				
				img = page.getElementsByTagName('img').item(0).getAttribute('url');
				url = page.getElementsByTagName('adress').item(0).getAttribute('url');
				map = page.getElementsByTagName('adress').item(0).getAttribute('map');
				addr = 'zip: '+ page.getElementsByTagName('adress').item(0).getAttribute('zip')
					+ '; ' + page.getElementsByTagName('adress').item(0).getAttribute('city') + ' city'
					+ ', ' +page.getElementsByTagName('adress').item(0).getAttribute('street')
					+ ' Street, ' + page.getElementsByTagName('adress').item(0).getAttribute('home') + ' Bldg';
				phone = page.getElementsByTagName('adress').item(0).getAttribute('phone');
				email = page.getElementsByTagName('adress').item(0).getAttribute('email');
				descr = page.getElementsByTagName('adress').item(0).getAttribute('description');
				img = img.replace(/^\.\.\//, '/');
				Data.push( {
					'id' : id,
					'name' :name,
					'url' :url,
					'img' : img,
					'map' :map,
					'address' :addr,
					'phone' : phone,
					'email' :email,
					'description' : descr
				});
				im = new Image();
				im.src = img;
				if (i<2) {
				    $('#i'+(i+1)).attr('src', img);
				}
		
			}
			if (Data.length) {
				$('#mapa').attr('href', Data[0].map);
				$('#mail').attr('href', 'mailto:'+Data[0].email);
				$('#mail').attr('title', 'mailto:'+Data[0].email);
				if (Data[0].phone.length > 0) 
				{
					$('#mobile_phone').attr('title', Data[0].phone);
				}
				$('#home').attr('href', Data[0].url);
				$('#home').attr('title', Data[0].url);
				Timer = setTimeout('addNewSlide()', SlideInterval*2);
				startSlideshow();
			} else 
			{
				alert('No pages found. Please try another search query or catalog');
				$('input[@name=textField]').val('');
			}	
}
	});
	
}


function utf8Encode(string) {
	if (navigator.appName.indexOf("Microsoft") == -1) {
	    //This encoding mechanism is for IE only
	    return 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;
}



function addNewSlide() {
	clearTimeout(Timer);
	$('#next').trigger('click');
	Timer = setTimeout('addNewSlide()', SlideInterval);

}


function swapImages(back) {
	if (TopImage == 1) {
		TopImage = 2;
	} else {
		TopImage = 1;
	}


	ti = TopImage;
	
	ti = (TopImage == 1) ? 2 : 1;
	
	if (back) {
		AddIndex--;
		actIndex = (AddIndex) ? AddIndex-1 : 0;

	} else {
		AddIndex++;
		if (AddIndex<Data.length && $('#i'+TopImage).attr('src') == Data[AddIndex].img) {
			AddIndex++;
		}

	}
	
	if (AddIndex < 0) {
		AddIndex = Data.length-1;
	}

	if (AddIndex >= Data.length) {
			AddIndex = 0;
	}
	
	if (Data[AddIndex]) ;
	else {
	    $('#pause').trigger('click');
	    return;
	}

	actIndex = (AddIndex) ? AddIndex-1 : (Data.length % 2 == 0) ? 0 : Data.length -1;

	if (back && AddIndex == 0) {
		actIndex = Data.length-1;
	}
	
	setTimeout("$('#i"+TopImage+"').attr('src', new String('"+Data[AddIndex].img+"'))",(back) ? 0 :400);
	if (direction == 'forward' && back) {
		$('#i'+ti).attr('src', new String(Data[actIndex].img));
		direction == 'back';
	} else {
		direction == 'forward';
	}
	
	$('#mapa').attr('href', Data[actIndex].map);
	$('#mail').attr('href', 'mailto:'+Data[actIndex].email);
	$('#mail').attr('title', 'mailto:'+Data[actIndex].email);
	$('#mobile_phone').attr('title', Data[actIndex].phone);
	$('#home').attr('href', Data[actIndex].url);
	$('#home').attr('title', Data[actIndex].url);
}


function startSlideshow() {
	
	if (!isFirstRun) {
		return;
	}
	
	isFirstRun = false;
	
$('#pause').click( function() {
	clearTimeout(Timer);
	return false;
});
$('#play').click( function() {
	Timer = setTimeout('addNewSlide()', SlideInterval);
	return false;
});

$('#next').click( function() {
	swapImages();
	return true;
});

$('#prev').click( function() {
	swapImages(true);
	return true;
});
/*
$('#s1').hover( function() {
	$('#controls').fadeIn();
}, function() {
	$('#controls').fadeOut();
});
*/
$('#slides').cycle( {
	fx :'shuffle',
	speed :300,
	timeout :3000,
	next :'#next',
	prev :'#prev'
});
$('#controls').fadeIn();
$('#slides').cycle('pause');
$('#slides').fadeIn();

var slidesAdded = false;
//$('#next').trigger('click');
}

$(function() {parseXML('getcatalog.ashx?catalog=3');});