var pb = pb || [];

window.addEvent('domready', function() {
	
});

function pb_ec(req, index) {
	if (req[index].Images.length > 0) {
		/*
		<div class="tn">
			<a href="propertyURL"><img src="mediumURL" /></a>
			<div class="tw"><a href="propertyURL">TitleLine1<br />TitleLine2</a></div>
		</div>
		*/
		tn = new Element('div', { 'class': 'pb_tn' });
		lnk = new Element('a', {href: req[index].propertyURL, 'title': 'Click here for full details' });
			img = new Element('img', { src: req[index].Images[0].mediumURL }).inject(lnk);
		if (req[index].imageCSS != '') {
			spn = new Element('span', { 'class': 'tagged ' + req[index].imageCSS });
			spn.inject(lnk);
		}
		tw = new Element('div', { 'class': 'tw' });
			ttl = new Element('a', { 
				href: req[index].propertyURL,
				'title': 'Click here for full details',
				'html': req[index].TitleLine1 + '<br />' + req[index].TitleLine2
			}).inject(tw);
		
		lnk.inject(tn);
		tw.inject(tn);
		tn.inject('carousel_thumbs');
	}
}

function pb_ld() {
	var jsonRequest = new Request.JSON({url: '/handlers/carousel.ashx?mode=pagebottom', 
		onSuccess: function(req){
			if (req != 'null') {
				pbStyle = Asset.css('/scripts/widgets/carousel.css');
				pb.req = req;
				apnlImgs = Asset.images(['/scripts/widgets/80black.png','/scripts/widgets/arr.png','/scripts/widgets/arr_h.png',
					'/scripts/widgets/tb.png'], {
					onComplete: function(){
						
						inImgs = [];
						pb.req.each(function(item, index) {
							if (index < 13) {
								if (item.Images.length > 0) {
									inImgs.push(item.Images[0].mediumURL);
								}
							}
						});
						
						//only preload first few images
						initImages = Asset.images(inImgs, {
							onComplete: function(){
								$('carousel_thumbs').empty();
								pb.req.each(function(item, index) {
									pb_ec(pb.req, index);
								});
								$('bottomcarouselwrapper').setStyle('display', 'block');
								
								//set nav
								pb.sc = new Fx.Scroll('carousel_tray', {
									wait: false,
									duration: 250,
									offset: {'x': 10, 'y': 1},
									transition: Fx.Transitions.Quad.easeInOut
								});
								pb.sc.set(0, 0);
								pb.c = 0;
								pb.w = 0;
								pb.a = 215;
								pb.i = $$('#carousel_thumbs .pb_tn').length * pb.a;
								
								$('carousel_br').addEvent('click', function(ev) {
									ev = new Event(ev).stop();
									if (pg != null) { if (pg.tmp != null) { window.clearTimeout(pg.tmp); pg.tmp = null; } }
									if (pb.c < pb.i - 300) {
										pb.c += pb.a;
										//pb.sc.stop();
										pb.sc.start(pb.c, 0);
									}
								});
								$('carousel_bl').addEvent('click', function(ev) {
									ev = new Event(ev).stop();
									if (pg != null) { if (pg.tmp != null) { window.clearTimeout(pg.tmp); pg.tmp = null; } }
									if (pb.c > -300) {
										pb.c -= pb.a;
										//pb.sc.stop();
										pb.sc.start(pb.c, 0);
									}
								});
								$('carousel_br').addEvent('doubleclick', function(ev) {
									ev = new Event(ev).stop();
									if (pg != null) { if (pg.tmp != null) { window.clearTimeout(pg.tmp); pg.tmp = null; } }
									if (pb.c < pb.i - 300) {
										pb.c += pb.a;
										//pb.sc.stop();
										pb.sc.start(pb.c, 0);
									}
								});
								$('carousel_bl').addEvent('doubleclick', function(ev) {
									ev = new Event(ev).stop();
									if (pg != null) { if (pg.tmp != null) { window.clearTimeout(pg.tmp); pg.tmp = null; } }
									if (pb.c > -300) {
										pb.c -= pb.a;
										//pb.sc.stop();
										pb.sc.start(pb.c, 0);
									}
								});
								//
							}
						});
					}
				});
			}
		}
	}).get();
}



