app = {};
app.data = {};
app.models = {};
app.views = {};
app.controllers = {};
app.hooks ={};
app.libraries = {};
app.state = {'page':''};
app.state.thumbs_loaded = false;
foco = app;
foco.c = app.controllers;
foco.m = app.models;
foco.v = app.views;


foco.c.site = fw.Controller.extend({
	initialize:function(){
		this.parent();
		this.projectImages = {equipment:['carousel.jpg', 'swings.jpg', 'bench.jpg'], windows:['1.jpg','2.jpg','3.jpg'], xmas2007:['1.jpg','2.jpg','3.jpg']};
		this.thumbs = {
			'before':[{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/before/1.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/before/2.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/before/3.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/before/4.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/before/5.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/before/6.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/before/7.jpg'}],
			'after':[{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/after/1.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/after/2.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/after/3.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/after/4.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/after/5.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/projects/equipment/new/bench.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/projects/equipment/new/swings.jpg'}],
			'kids':[
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/2.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/3.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/4.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/5.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/6.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/7.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/8.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/9.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/10.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/11.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/12.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/13.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/14.jpg'},
					{src:'http://friendsofcolombianorphans.org/foco/images/site/gallery/kids/15.jpg'}
			]
			}
	},
	
	fixSizes:function(){
		if($.browser.msie){
			$("p").css({'margin':'5px'});
			if($.browser.version < 7){
				$('#rightContent').css({'padding':'0px'});
			}
		}
	},
	
	showLoading:function(text){
		text = text || 'Loading ';
		$id('contentContainer').innerHTML = '<p>'+text+'<img src="/foco/images/site/loading.gif"></p>';
	},
	
	showSplitView:function(){
		splitView = this.load.view('site/splitView');
		$html('contentContainer', splitView);
	},
	
	switchLanguage: function(to){
		var from = to == 'spanish' ? 'english': 'spanish';
		$('.'+from).fadeOut(function(){ $('.'+to).fadeIn(); });
	},
	
	highlightMenu:function(which){
		if(($.browser.version > 100 && $.browser.version < 500)) return;
		$('.menuItem').removeClass('menuSelected');
		$('#menu_'+which).addClass('menuSelected').blur();
	},
	
	showPage:function(which){
		this.showLoading();
		this.highlightMenu(which);
		app.state.page = which;
		this.showSplitView();
		$html('leftContent',this.load.view('site/leftContent/'+which));
		$html('rightContent', this.load.view('site/rightContent/'+which));
		this.fixSizes();
	},
	
	mission:function(){
		app.state.page = 'mission';
		this.showPage('mission');
	},

	showGallery:function(which){
		//alert(which+' photo gallery coming soon');
	},
	
	gallery:function(){
		this.loadThumbs();
		this.highlightMenu('gallery');
		app.state.page = 'gallery';
	},
	
	loadThumbs:function(){
//return this.showThumbs();
		if(app.state.thumbs_loaded) return this.showThumbs();
		var _this = this;
		this.load.library('image_loader');
		this.image_loader.load(this.getAllThumbs(), function(){ _this.showThumbs(); app.state.thumbs_loaded = true; }, function(num){ _this.showLoading('Loading '+num+' images ');_this.showThumbs();});
		$('#menu').click(function(){ _this.image_loader.cancel();});
	//	this.image_loader.load(this.getAllThumbs(), function(){ _this.showThumbs(); app.state.thumbs_loaded = true; }, function(num){ _this.showLoading('Loading '+num+' images ');});
	},
	
	showThumbs:function(){
		this.load.library('skim');
		this.skim.setOptions({
			images: this.thumbs['before'],
			mapWidth:'200',
			mapHeight:'200',
			mapName:'beforeGallery',
			imageId:'beforeImageId',
			defaultHref:"javascript:run('site/showGallery/before',{},true);",
			imageClass:'galleryThumb'
		});
		var before = this.skim.render();
		
		this.skim.clear();
		this.skim.setOptions({
			images:this.thumbs['after'],
			mapName:'afterGallery',
			imageId:'afterImageId',
			imageClass:'galleryThumb',
			defaultHref:"javascript:run('site/showGallery/after',{},true);"
		});
		var after = this.skim.render();
		
		this.skim.clear();
		this.skim.setOptions({
			images:this.thumbs['kids'],
			mapName:'kidsGallery',
			imageId:'kidsImageId',
			imageClass:'galleryThumb',
			defaultHref:"javascript:run('site/showGallery/kids',{},true);"
		});
		var kids = this.skim.render();
		$html('contentContainer', this.load.view('site/gallery', {'before':before, 'after':after,'kids':kids}));
	},
	

	
	getAllThumbs:function(){
			return this.thumbs.before.concat(this.thumbs.after, this.thumbs.kids);
	},
	
	projects:function(which){
		if(!which) return this.showPage('projects');
		this.showLoading();
		this.highlightMenu('projects');
		app.state.page = which || 'projects';
		$id('contentContainer').innerHTML = this.load.view('site/projects/'+which);
	},
	
	switchKnittingImages: function(page){
		var from_id = (page == 1 ? '#knittingImages1' : '#knittingImages');
		var to_id = (from_id == '#knittingImages1' ? '#knittingImages' : '#knittingImages1');
		$(from_id).fadeOut(function(){$(to_id).fadeIn();})
	},
	
	switchProjectImages:function(project, oldNew){
		var images = this.projectImages;
		if(!($.browser.version > 100 && $.browser.version < 500)) $('#projectImages').fadeOut();
		setTimeout(function(){
				$id('project_images_1').src = "http://friendsofcolombianorphans.org/foco/images/projects/"+project+"/"+oldNew+"/"+images[project][0];
			$id('project_images_2').src = "http://friendsofcolombianorphans.org/foco/images/projects/"+project+"/"+oldNew+"/"+images[project][1];
				$id('project_images_3').src = "http://friendsofcolombianorphans.org/foco/images/projects/"+project+"/"+oldNew+"/"+images[project][2];
				var link = oldNew == 'old' ? 'new' : 'old';
				var text = link == 'old' ? 'before' : 'after';
				if(project == 'xmas2007'){
					if(text == 'before') text = 'previous';
					else text = 'more';
				}
				$html('beforeAfterLink', '<a href="javascript:run(\'site/switchProjectImages/'+project+'/'+link+'\', {}, true);">'+text+' images</a>');
				$html('beforeAfterLabel', (link == 'old'? 'after:' : 'before:'))
				$('#projectImages').fadeIn('slow');
		}, 350);
	},
	
	showProjects:function(which){
		
	},
	
	
	contact:function(){
		app.state.page = 'contact';
		this.showPage('contact');
	//	$('#contactImage').css('padding-left','55px').show();
	},
	
	donate:function(){
		app.state.page = 'donate';
		this.showPage('donate');
	
	},
	
	donors: function(){
		app.state.page = 'donors';
		this.showPage('donors');
	},
	
	about:function(){
		app.state.page = 'about';
		this.showPage('about');
	},	
	
	submitContactForm:function(){		
		cont = {name:$val('contact_name'), email:$val('contact_email'), message:$val('contact_message')}
		$html('leftContent', '<p>Sending Message <img src="/foco/images/site/loading.gif"></p>');
		_this = this;
		$.post('/index.php/welcome/sendContact', cont, function(data){
			_this.showSplitView();
			$html('leftContent', _this.load.view('site/leftContent/contactThankYou'));
			$html('rightContent', _this.load.view('site/rightContent/contact'));
		});
	}
});

foco.c.test = fw.Controller.extend({
	initialize:function(){
		this.parent();
	},
	
	run:function(){
		this.load.library('al', [], this);
		this.al.run();
	}
});

