var pg = pg || [];

window.addEvent('domready', function() {
	
});

function ld() {
	var jsonRequest = new Request.JSON({url: '/handlers/carousel.ashx?mode=homepage', 
		onSuccess: function(req){
			if (req != 'null') {
				hpStyle = Asset.css('/scripts/widgets/homepage.css');
				pg.req = req;
				pg.int = 8000;  //carousel delay
				$('homepanel_topbar').empty();
				$('homepanel_topbar').set('text', 'Featured properties ');
				pg.req.each(function(item, index) {
					if (pg.req[index].Images.length > 0) {
						img = new Element('img', { src: pg.req[index].Images[0].largeURL });
					}
					lnk = new Element('a', {
							href: 'javascript:sw(' + index + ');'
						}).inject('homepanel_topbar');
				});
				
				apnlImgs = Asset.images(['/scripts/widgets/80black.png','/scripts/widgets/arr.png','/scripts/widgets/arr_h.png','/scripts/widgets/snk.png',
					'/scripts/widgets/tb.png'], {
					onComplete: function(){
						//initiate panel
						$('homepanel').setStyle('display', 'block');
						sp(0);
						if (pg.req.length > 1) {
							st(1);
						}
						
						//preload images
						pnlImgs = [];
						thumbImgs = [];
						detailImgs = [];
						pg.req.each(function(prop) {
							if (prop.Images.length > 0) {
								pnlImgs.push(prop.Images[0].largeURL);
								prop.Images.each(function(img, index) {
									thumbImgs.push(img.thumbURL);
									if (index > 0) {
										detailImgs.push(img.largeURL);
									}
								});
							}
						});
						apnlImgs = Asset.images(pnlImgs, {
							onComplete: function(){
								athumbImgs = Asset.images(thumbImgs, {
									onComplete: function(){
										adetailImgs = Asset.images(detailImgs);
									}
								});
							}
						});
						//end preload images
					}
				});
				
				//set nav
				$('homepanel_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 (pg.c < pg.i - 200) {
						pg.c += pg.a;
						pg.sc.start(pg.c, 0);
					}
				});
				$('homepanel_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 (pg.c > 0) {
						pg.c -= pg.a;
						pg.sc.start(pg.c, 0);
					}
				});
				$('homepanel_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 (pg.c < pg.i - 200) {
						pg.c += pg.a;
						pg.sc.start(pg.c, 0);
					}
				});
				$('homepanel_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 (pg.c > 0) {
						pg.c -= pg.a;
						pg.sc.start(pg.c, 0);
					}
				});
				//
			}
		}
	}).get();
}

function st(index) {
	if (pg.tmp == null) {
		pg.ci = index;
		pg.tmp = window.setInterval(function() {
			sp(pg.ci);
			pg.ci = (pg.ci < pg.req.length -1) ? pg.ci + 1 : 0;
		}, pg.int);
	}
}

function sw(pid) {
	window.clearInterval(pg.tmp);
	pg.tmp = null;
	sp(pid);
}

function cb() {
	$$('#homepanel_topbar a').each(function(item) { item.removeClass('active'); });
}

function sp(pid) {
	$('homepanel_pnl').empty();
	tw = new Element('div', {'class': 'tw'});
	title = new Element('a', {id: 'homepanel_title', 'title': 'Click here for full details', 'href': pg.req[pid].propertyURL, 
		'text': pg.req[pid].TitleLine1} ).inject(tw);
	$('homepanel_pnl').adopt(tw);
	
	//details panel text
	if ($('homepanel_detail') != null) {
		$('homepanel_detail').empty();
		$('homepanel_detail').set('html', '<h2>' + pg.req[pid].TitleLine1 + '</h2>' + pg.req[pid].Details);
	
		//full details link
		dl = new Element('a', {href: pg.req[pid].propertyURL});
		dt = new Element('div', {'class': 'hp_fulldetails', 'text': 'Full Details' }).inject(dl);
		$('homepanel_detail').adopt(dl);
	}
	
	//image scrolling
	pg.sc = new Fx.Scroll('homepanel_thc', {
		wait: false,
		duration: 250,
		offset: {'x': 10, 'y': 1},
		transition: Fx.Transitions.Quad.easeInOut
	});
	pg.sc.set(0, 0);
	pg.c = 0;
	pg.w = 0;
	pg.i = pg.req[pid].Images.length * 79;
	pg.a = 79;
	pg.pid = pid;
	//
	
	if (pg.req[pid].Images.length > 0) {
		lnk = new Element('a', {
				href: pg.req[pid].propertyURL,
				title: 'Click here for full details'
			}).inject('homepanel_pnl');
		img = new Element('img', {
				src: pg.req[pid].Images[0].largeURL
			}).inject(lnk);
	}
	
	cb();
	$$('#homepanel_topbar a')[pid].addClass('active');
	
	$('homepanel_thumbs').empty();
	pg.req[pid].Images.each(function(item, index) {
		lnk = new Element('a', {
				href: 'javascript:si(' + pid + ', ' + index + ');'
			}).inject('homepanel_thumbs');
		img = new Element('img', {
				src: item.thumbURL
			}).inject(lnk);
	});
}

function si(pid, iid) {
	if (pg.tmp != null) {
		window.clearInterval(pg.tmp);
	}
	if (pg.req[pid].Images.length > 0) {
		$('homepanel_pnl').empty();
		tw = new Element('div', {'class': 'tw'});
		title = new Element('a', {id: 'homepanel_title', 'title': 'Click here for full details', 'href': pg.req[pid].propertyURL, 
			'text': pg.req[pid].TitleLine1} ).inject(tw);
		$('homepanel_pnl').adopt(tw);
		lnk = new Element('a', {
				href: pg.req[pid].propertyURL,
				title: 'Click here for full details'
			}).inject('homepanel_pnl');
		img = new Element('img', {
				src: pg.req[pid].Images[iid].largeURL
			}).inject(lnk);
	}
}


