// JavaScript Document

var photos = [
	//Exterior
	{photo:'0789m.jpg', caption:'Front Door', sec:'ext', hoz:false},
	{photo:'0883m.jpg', caption:'Front Door Looking Down', sec:'ext', hoz:true},
	{photo:'0907m.jpg', caption:'Pond/Fountain Near Entrance', sec:'ext', hoz:false},
	{photo:'0856m.jpg', caption:'Canyon Side Terrace', sec:'ext', hoz:true},
	{photo:'0863m.jpg', caption:'Canyon Side Terrace', sec:'ext', hoz:false},
	//{photo:'8674m.jpg', caption:'Canyon Side Terrace', sec:'ext', hoz:true},
	 {photo:'1859m.jpg', caption:'Exterior Photo', sec:'ext', hoz:true},
	 {photo:'8757m.jpg', caption:'Evening View From Terrace', sec:'ext', hoz:true},
	//{photo:'8397m.jpg', caption:'Pool', sec:'ext', hoz:true},
	//{photo:'8679m.jpg', caption:'Pool w/Casita Loggia', sec:'ext', hoz:true},
	//{photo:'8696m.jpg', caption:'View From Casitas', sec:'ext', hoz:true},
	//{photo:'8702m.jpg', caption:'View Towards Casitas', sec:'ext', hoz:true},
	//{photo:'8712m.jpg', caption:'Villa Entry', sec:'ext', hoz:false},
	//{photo:'8717m.jpg', caption:'Entrance From Street', sec:'ext', hoz:true},
	{photo:'0861m.jpg', caption:'View', sec:'ext', hoz:true},
	{photo:'0850m.jpg', caption:'Pool From Main House', sec:'ext', hoz:true},
	{photo:'0823m.jpg', caption:'Pool Area from Casita Loggia', sec:'ext', hoz:true},
	{photo:'0805m.jpg', caption:'Pool', sec:'ext', hoz:true},
	
	
	//First Floor		  
	{photo:'8547m.jpg', caption:'Dining Room & Terrace', sec:'1stfloor', hoz:true},
	{photo:'8561m.jpg', caption:'Living/Media & Entry', sec:'1stfloor', hoz:true},
	{photo:'8565m.jpg', caption:'Living Areas & Entry', sec:'1stfloor', hoz:true},
	{photo:'8570m.jpg', caption:'Library/Music Room', sec:'1stfloor', hoz:true},
	{photo:'8634m.jpg', caption:'Entry From Living Rooms', sec:'1stfloor', hoz:false},
	{photo:'8638m.jpg', caption:'Entry Hall & Stairs', sec:'1stfloor', hoz:false},
	{photo:'8724m.jpg', caption:'Living/Media Room', sec:'1stfloor', hoz:true},
	{photo:'8777m.jpg', caption:'Living-Dining Rooms', sec:'1stfloor', hoz:true},
	
	{photo:'8521m.jpg', caption:'Ground Floor Bathroom', sec:'1stfloor', hoz:false},
	{photo:'8524m.jpg', caption:'Ground Floor Bedroom', sec:'1stfloor', hoz:true},
	{photo:'8537m.jpg', caption:'Office Powder Room', sec:'1stfloor', hoz:false},
	{photo:'8539m.jpg', caption:'Office', sec:'1stfloor', hoz:true},
	{photo:'8578m.jpg', caption:'Kitchen/Dining', sec:'1stfloor', hoz:true},
	{photo:'8590m.jpg', caption:'Kitchen View From Island', sec:'1stfloor', hoz:true},
	{photo:'8749m.jpg', caption:'Kitchen w/Evening Terrace View', sec:'1stfloor', hoz:true},
	
	//Second Floor
	{photo:'8663m.jpg', caption:'Master Bedroom', sec:'2ndfloor', hoz:true},
	{photo:'8792m.jpg', caption:'Guest Bedroom', sec:'2ndfloor', hoz:true},
	{photo:'8801m.jpg', caption:'Guest Bath', sec:'2ndfloor', hoz:false},
	{photo:'8808m.jpg', caption:'His Bath', sec:'2ndfloor', hoz:false},
	{photo:'8811m.jpg', caption:'Evening View From Master', sec:'2ndfloor', hoz:true},
	{photo:'8820m.jpg', caption:'Master w/View  To Dressing & Office', sec:'2ndfloor', hoz:true},
	{photo:'8822m.jpg', caption:'Her Bath', sec:'2ndfloor', hoz:true},
	
	//Casita
	{photo:'8390m.jpg', caption:'Exterior Casita View', sec:'casita', hoz:true},
	{photo:'8421m.jpg', caption:'Casita I Living', sec:'casita', hoz:true},
	{photo:'8422m.jpg', caption:'Casita I Living', sec:'casita', hoz:true},
	{photo:'8432m.jpg', caption:'Casita I Bedroom', sec:'casita', hoz:true},
	{photo:'8434m.jpg', caption:'Casita I Bath', sec:'casita', hoz:true},
	{photo:'8487m.jpg', caption:'Casita II Living', sec:'casita', hoz:true},
	{photo:'8491m.jpg', caption:'Casita II Bath', sec:'casita', hoz:true},
	{photo:'8498m.jpg', caption:'Casita II Bedroom', sec:'casita', hoz:true}
];

var current_photo = {};
var current_img = {};
var current_idx = 0;
var elBody = null;
var elLogo = null;
var elComment = null;
var photoSpace = null;
var preLoadImage = null;
var photoChangeTask = null;
var photoChangeTasker = null;
var layer = 1000;
var speed = 10000;
var gmenu_sec = ['g_ext','g_1stfloor','g_2ndfloor','g_casita'];
var last_gmenu_sec;
var changeLock = false;

var commentXoffset = 115;
var commentYoffset = 90;

function changeMenuSection(sec){
	
	if(sec == last_gmenu_sec){
		return;
	}
	
	//Remove Previous Selection
	var c = gmenu_sec.length;
	for(var i = 0; i<c; i++){
		var el = Ext.get(gmenu_sec[i]);
		if(el) el.removeClass('gCurr');
	}
	
	//Highlight Menu
	var gm = Ext.get('g_'+sec);
	if(gm){
		gm.addClass('gCurr');
	}
	
	//Save this selection
	last_gmenu_sec = sec;
}

function changePicture(){
	changeLock = true;
	var hoz = true;
	
	if(preLoadImage){
		if(!preLoadImage.complete){
			return;	
		}
	}
	
	var elPhoto = elBody.createChild({
		id:Ext.id(),
		cls:'hide'
	});
	
	var elImg = elPhoto.createChild({
		id:Ext.id(),
		tag: 'img'
	});
	elPhoto.setWidth(370);
	elPhoto.position('absolute', layer, -1000, -1000);
	layer++;
	
	//console.log("IDX: ", current_idx, "  l  ", layer, '  p  ', elPhoto.dom);
	elImg.dom.src = 'photos/' + photos[current_idx].sec + '/' + photos[current_idx].photo;
	if(!photos[current_idx].hoz) hoz = false;
	var comment = photos[current_idx].caption;
	changeMenuSection(photos[current_idx].sec);
	Ext.state.Manager.set('lastPicIndex', current_idx);
	
	current_idx++;
	if(current_idx >= photos.length){
		current_idx = 0;
	}
	
	preLoadImage = new Image();
	preLoadImage.src = 'photos/' + photos[current_idx].sec + '/' + photos[current_idx].photo;
	
	//elPhoto.position('absolute', layer, photoSpace.getX(), photoSpace.getY());
	elPhoto.alignTo(photoSpace, 'tl-tl');
	if(!hoz){
		elPhoto.setX(elPhoto.getX()+122);
	}
	
	if(current_photo.fadeOut) current_photo.fadeOut();
	var elcopy = elPhoto;
	var elcopyimg = elImg;
	if(elLogo){
		elComment.update(comment);
	}
	elPhoto.fadeIn({duration: 1,callback:photoFadeInComplete.createCallback(elcopy, elcopyimg)});
	
	changeLock = false;
}

function photoFadeInComplete(elp, eimg){
	
	if(current_photo.remove){
		current_photo.remove();
	}
	if(current_img.remove) {
		current_img.remove();
	}
	current_photo = elp;
	current_img = eimg;
}

function jumpToGallerySection(sec){
	photoChangeTasker.stopAll();
	var c = photos.length;
	for(var i = 0; i < c; i++ ){
		if(photos[i].sec == sec){
			current_idx = i;
			break;
		}
	}
	photoChangeTasker.start(photoChangeTask);
}

function forceGalleryNav(move){
	photoChangeTasker.stopAll();
	var c = photos.length, idx;
	if(move=='left'){
		idx = current_idx-2;
		if(idx<0) idx = c-1;
	}else{
		idx = current_idx;
	}
	current_idx = idx;
	photoChangeTasker.start(photoChangeTask);
}

function initGalleryMenu(){
	var c = gmenu_sec.length;
	for(var i = 0; i < c; i++ ){
		var el = Ext.get(gmenu_sec[i]);
		if(el){
			el.on('click', jumpToGallerySection.createCallback(gmenu_sec[i].replace('g_', '')));	
		}
	}
	
	var lb = Ext.get('g_left');
	if(lb) lb.on('click', forceGalleryNav.createCallback('left'));
	
	var rb = Ext.get('g_right');
	if(rb) rb.on('click', forceGalleryNav.createCallback('right'));
}

Ext.onReady(function(){
	
	Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	current_idx = Ext.state.Manager.get('lastPicIndex', 0);
	
	photoSpace = Ext.get('photo_place');
	elBody = Ext.getBody();
	elLogo = Ext.get('logo');
	
	elComment = elBody.createChild({
		id:'comment',
		cls: 'comment'
	});
	
	if(elLogo){
		elComment.setX(elLogo.getX()+commentXoffset);
		elComment.setY(elLogo.getY()+commentYoffset);
	}else{
		elComment.setX(-1000);	
		elComment.setY(-1000);	
	}
	
	photoChangeTask = {
		run: changePicture,
		interval: speed
	};
	
	photoChangeTasker = new Ext.util.TaskRunner();
	photoChangeTasker.start(photoChangeTask);
	
	initGalleryMenu();
	
	window.onresize = function(){
		if(current_photo.remove){
			current_photo.alignTo(photoSpace, 'tl-tl');
		}
		
		if(elLogo){
			elComment.setX(elLogo.getX()+commentXoffset);
			elComment.setY(elLogo.getY()+commentYoffset);
		}else{
			elComment.setX(-1000);	
			elComment.setY(-1000);	
		}
	};
	
});
