var pe;
var photochanging = false;
var slide_time = 5;

function changeprices(x) {
	if(x.options[x.selectedIndex].value=='Lettings') {
		$('minSales').hide();
		$('maxSales').hide();
		$('minLettings').show();
		$('maxLettings').show();
	} else { 
		$('minLettings').hide();
		$('maxLettings').hide();
		$('minSales').show();
		$('maxSales').show();
	}
}





function nextslide() {
	
	var stopped = false;
	
	if (pe) {
		pe.stop();
		stopped = true;
	}
	
	thumbphotos = $('thumbs').immediateDescendants();
	iCurrentPhotoID = $('slide_photo_1').className.replace('photo_num_','');
	iNewPhotoID = parseInt(iCurrentPhotoID) + 1;

	isphotovisible = $('slide_photo_1').visible();

	if(isphotovisible == true) {
		photo1img = "slide_photo_1";
		photo2img = "slide_photo_2";
	} else {
		photo1img = "slide_photo_2";
		photo2img = "slide_photo_1";
	}

	if(photochanging == false) {
		photochanging = true;
		new Effect.Fade(photo1img);

		if(iNewPhotoID <= thumbphotos.length) {
			$('slide_photo_1').className = 'photo_num_'+iNewPhotoID;
			$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_'+iNewPhotoID+'_');
		} else {
			$('slide_photo_1').className = 'photo_num_1';
			$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_1_');
		}

		new Effect.Appear(photo2img,{afterFinish:function(){photochanging=false;}});
	}
	
	
	if (stopped) {
		pe = new PeriodicalExecuter(nextslide, slide_time);
	}


	return false;
}



function pauseslide() {
	
	if (pe) {
		pe.stop();
		pe = null;
		$('sso_img_play').show();
		$('sso_img_pause').hide();
	} else {
		$('sso_img_play').hide();
		$('sso_img_pause').show();
		nextslide();
		pe = new PeriodicalExecuter(nextslide, slide_time);
	}
	
}


function prevslide() {
	
	var stopped = false;
	
	if (pe) {
		pe.stop();
		stopped = true;
	}
	
	thumbphotos = $('thumbs').immediateDescendants();
	iCurrentPhotoID = $('slide_photo_1').className.replace('photo_num_','');
	iNewPhotoID = parseInt(iCurrentPhotoID) - 1;

	isphotovisible = $('slide_photo_1').visible();

	if(isphotovisible == true) {
		photo1img = "slide_photo_1";
		photo2img = "slide_photo_2";
	} else {
		photo1img = "slide_photo_2";
		photo2img = "slide_photo_1";
	}

	if(photochanging == false) {
		photochanging = true;
		new Effect.Fade(photo1img);

		if(iNewPhotoID == 0) {
			$('slide_photo_1').className = 'photo_num_'+thumbphotos.length;
			$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_'+thumbphotos.length+'_');
		} else {
			$('slide_photo_1').className = 'photo_num_'+iNewPhotoID;
			$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_'+iNewPhotoID+'_');
		}

		new Effect.Appear(photo2img,{afterFinish:function(){photochanging=false;}});
	}
	
	
	if (stopped) {
		pe = new PeriodicalExecuter(prevslide, slide_time);
	}

	return false;
}


function startslide() {
	resetlinks();
	hidemap(true);

	if(pe) {
		pe.stop();
		pe = null;
		$('property_images').style.display = "none";
		spotlight_off('blackout_screen', 'blackout_stage');
	} else {
		spotlight_on('blackout_screen', 'blackout_stage');
		$('property_images').style.display = "block";
		pe = new PeriodicalExecuter(nextslide, slide_time);
		$('slideshowlink').addClassName('selected');
	}

	return false;
}


function togglemap(x) {
	mapclickpos = $('maplink').innerHTML.indexOf('Click');
	satelliteclickpos = $('satellitelink').innerHTML.indexOf('Click');
	hybridclickpos = $('hybridlink').innerHTML.indexOf('Click');

	resetlinks();

	if(pe) {
		pe.stop();
		pe = null;
	}

	if($('map').style.display == 'none' || $('map').style.display == '' || (mapclickpos > -1 && x!=1) || (satelliteclickpos > -1 && x!=2) || (hybridclickpos > -1 && x!=3)) {
		showmap(x);
	} else {
		hidemap(true);
	}

	return false;
}


function showmap(x) {
	
	$('map').style.display = 'block';
	
	$('main_photo_1').hide();
	$('main_photo_2').hide();

	if (GBrowserIsCompatible()) {
		if($('map')) {
			if(x == 1) {
				var map = new GMap2($('map'), {mapTypes:[G_NORMAL_MAP]});
				$('maplink').addClassName('selected');
				$('maplink').update("Map (Click to hide)");
			} else if(x == 2) {
				var map = new GMap2($('map'), {mapTypes:[G_SATELLITE_MAP]});
				$('satellitelink').addClassName('selected');
				$('satellitelink').update("Satellite Map (Click to hide)");
			} else {
				var map = new GMap2($('map'), {mapTypes:[G_HYBRID_MAP]});
				$('hybridlink').addClassName('selected');
				$('hybridlink').update("Hybrid Map (Click to hide)");
			}
			map.addControl(new GSmallMapControl());
			map.addControl(new GOverviewMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(latitude, longitude), 17);
		}
	}
}


function hidemap(resetphoto) {
	$('map').hide();
	
	if($('maplink')) {
		$('maplink').update("Map");
	}
	
	$('main_photo_1').show();
	$('main_photo_2').hide();

	if(resetphoto == true) {
		$('main_photo_1').src = "/i/p/"+propertyid+"_1_464.jpg";
		$('main_photo_1').className = 'photo_num_1';
	}
}


function resetlinks() {
	links = new Array(
		['slideshow','Slideshow'], 
		['floorplans','Floorplans'], 
		['map','Map'], 
		['satellite','Satellite Map'], 
		['hybrid','Hybrid Map'], ['print','Print'], 
		['viewing','Arrange a Viewing'], 
		['virtualtour','Virtual Tour'],
		['hip','Request a Hip Pack']
	);

	for(i=0;i<links.length;i++) {
		if($(links[i][0]+"link")) {
			$(links[i][0]+"link").removeClassName('selected');
			$(links[i][0]+"link").update(links[i][1]);
		}
	}
}


Event.observe(window, 'load', function() {
	getattrs = document.location.search.split('&');
	for(i=0;i<getattrs.length;i++) {
		keyandvalue = getattrs[i].split('=');
		if(keyandvalue[0] == "slide" && keyandvalue[1] == "true") {
			startslide();
		}
	}

	if($('thumbs')) {
		alinks = $('thumbs').getElementsByClassName("thumblink");

		for(i=0;i<alinks.length;i++) {
			Event.observe(alinks[i], 'click', function(e) {
				if(photochanging == false) {
					resetlinks();
					hidemap(false);

					if(pe) {
						pe.stop();
						pe = null;
					}
					
					curlinkelement = Event.element(e);

					parts = curlinkelement.src.split('_');

					isphotovisible = $('main_photo_1').visible();

					if(isphotovisible == true) {
						$('main_photo_1').src = curlinkelement.src.replace('156','464');
						$('main_photo_1').className = 'photo_num_'+parts[1];
					} else {
						$('main_photo_2').src = curlinkelement.src.replace('156','464');
						$('main_photo_1').className = 'photo_num_'+parts[1];
					}
				}

				Event.stop(e);
			});
		}
	}

	if($('next')) {
		$('next').onclick = function () {
			thumbphotos = $('thumbs').immediateDescendants();
			iCurrentPhotoID = $('main_photo_1').className.replace('photo_num_','');
			iNewPhotoID = parseInt(iCurrentPhotoID) + 1;
		
			isphotovisible = $('main_photo_1').visible();
		
			if(isphotovisible == true) {
				photo1img = "main_photo_1";
				photo2img = "main_photo_2";
			} else {
				photo1img = "main_photo_2";
				photo2img = "main_photo_1";
			}
		
			if(photochanging == false) {
				photochanging = true;
				new Effect.Fade(photo1img);
		
				if(iNewPhotoID <= thumbphotos.length) {
					$('main_photo_1').className = 'photo_num_'+iNewPhotoID;
					$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_'+iNewPhotoID+'_');
				} else {
					$('main_photo_1').className = 'photo_num_1';
					$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_1_');
				}
		
				new Effect.Appear(photo2img,{afterFinish:function(){photochanging=false;}});
			}
		
			return false;
		}
	}

	if($('prev')) {
		$('prev').onclick = function() {
			thumbphotos = $('thumbs').immediateDescendants();
			iCurrentPhotoID = $('main_photo_1').className.replace('photo_num_','');
			iNewPhotoID = parseInt(iCurrentPhotoID) - 1;

			isphotovisible = $('main_photo_1').visible();

			if(isphotovisible == true) {
				photo1img = "main_photo_1";
				photo2img = "main_photo_2";
			} else {
				photo1img = "main_photo_2";
				photo2img = "main_photo_1";
			}

			if(photochanging == false) {
				photochanging = true;
				new Effect.Fade(photo1img);

				if(iNewPhotoID == 0) {
					$('main_photo_1').className = 'photo_num_'+thumbphotos.length;
					$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_'+thumbphotos.length+'_');
				} else {
					$('main_photo_1').className = 'photo_num_'+iNewPhotoID;
					$(photo2img).src = $(photo1img).src.replace('_'+iCurrentPhotoID+'_','_'+iNewPhotoID+'_');
				}

				new Effect.Appear(photo2img,{afterFinish:function(){photochanging=false;}});
			}

			return false;
		}
	}

	if($('slideshowlink')) {
		$('slideshowlink').onclick = startslide;
	}

	if($('maplink')) {
		$('maplink').onclick = function() {
			return togglemap(1);
		}
	}

	if($('satellitelink')) {
		$('satellitelink').onclick = function() {
			return togglemap(2);
		}
	}

	if($('hybridlink')) {
		$('hybridlink').onclick = function() {
			return togglemap(3);
		}
	}
});



/*****************
	SPOTLIGHT
*****************/



function spotlight_on (bg_id, star_id) {

	var bg = document.getElementById(bg_id);
	var star = document.getElementById(star_id);

	bg.style.display = "block";
	star.style.display = "block";

}


function spotlight_off (bg_id, star_id) {

	var bg = document.getElementById(bg_id);
	var star = document.getElementById(star_id);

	bg.style.display = "none";
	star.style.display = "none";

}


function set_speed (speed) {
	document.getElementById('slideshow_speed').style.backgroundImage = "url(../i/slideshow/speed_control/speed_" + speed + ".jpg)";
	
	switch (speed) {
		
		case 4:
			slide_time = 1;
		break;
		
		case 3:
			slide_time = 3;
		break;
		
		case 1:
			slide_time = 10;
		break;
		
		default:
			slide_time = 5;
		
	}
}


window.onunload = function() {
	GUnload();
}