
Home = function() {
	return {
		init: function(){
			
			//setup search
			Utilities.searchFocus(false);
			Ext.fly('txtSearch').addKeyListener(Ext.EventObject.ENTER, function(){ Home.search(); });
			
			//load topics
			var tpl = new Ext.XTemplate('<tpl for="."><li><a href="collection.aspx?v={#}">{text}</a></li></tpl>');
			tpl.overwrite('topics', config.volumes);
			
			//image of the week
			this.iotw();
			
			//get figure details
			this.getFigures();
			
			//my imagesets
			this.getImageSets();
						
		},
		
		search: function(){
			var term = Ext.fly('txtSearch').getValue();
			if (term && term != lang.search_default) self.location = 'collection.aspx?q=' + term;
			return false;
		},
		
		
		iotw: function(){
			var today = new Date();
			var week = today.format('n'); //n = month 1 to 12
			var max = ImageOfTheWeek.length;
			if (week > max) week = max;
			
			var iotw = ImageOfTheWeek[week - 1];
			Ext.get('iotwImg').set({src: 'images/iotw' + iotw.link.substring(iotw.link.lastIndexOf('/')) + '.jpg'});
			Ext.get('iotwText').update(iotw.text);
			Ext.get('iotw').on('click', function(){ Utilities.showFigure(null, iotw.link); });
		},
		
		
		getFigures: function(){
			var stp = new Ext.data.ScriptTagProxy({
				url: config.serverRoot + String.format(config.homepageFiguresPath, config.siteId),
				callbackParam: 'cb',
				timeout: config.serverTimeout
			});
			stp.on('loadexception', function(o, data, cb, e){
				if (data && data[2][0].type) {
					var tpl = new Ext.XTemplate.from('figureTemplate');
					
					var mostSaved = data[0][0];
					mostSaved.heading = lang.most_saved_figure;
					mostSaved.downloadcount = String.format('{0}&#160;{1}', mostSaved.downloadcount, lang.downloads);
					tpl.overwrite('figureMostSaved', mostSaved);
					Home.addRating(mostSaved.link);
					
					var unrated = data[1][0];
					unrated.heading = lang.unrated_figure,
					unrated.downloadcount = '';
					tpl.overwrite('figureUnrated', unrated);
					Home.addRating(unrated.link);
					
					var topRated = data[2][0];
					topRated.heading = lang.top_rated_figure;
					topRated.downloadcount = '';
					tpl.overwrite('figureTopRated', topRated);
					Home.addRating(topRated.link);

				}
				else {
					globals.online = false;
				}
				
			});
			
			stp.load(null, null, function(){
			});
		},
		
		addRating: function(figure){
			var stars = new Ext.ux.StarRating({
				id: 'stars' + figure,
				totalStars: 5
			});
			
			stars.valueClicked = (globals.rated.indexOf(figure) != -1);
			
			stars.on('rate', function(o, vote){
				Ext.fly(this.id + '-text').update(lang.figure_rating_updating);
				
				var stp = new Ext.data.ScriptTagProxy({
					url: config.serverRoot + String.format(config.figureRatingPath, config.siteId, figure, vote),
					callbackParam: 'cb',
					timeout: config.serverTimeout
				});
				stp.on('loadexception', function(o, data, cb, e){
					try {
						if (data) {
							if (data.votes == 0) {
								Ext.fly(this.id + '-text').update(lang.figure_rating_befirst);
							}
							else {
								stars.average = data.rating;
								stars.resetStars();
								Ext.fly(this.id + '-text').update(String.format(lang.figure_rating, data.rating, data.votes));
								
								//if voted, add to array to prevent multiple votes this session
								//also disable mouseover
								if (vote) {
									globals.rated[globals.rated.length] = data.figure;
									stars.stars.addClass('disabled');
									Utilities.log('Rating', data.figure);
								}
							}
						}
						else {
							Ext.fly(this.id + '-text').update(lang.figure_rating_error);
						}
					} 
					catch (e) { /*do nothing - window could have been closed before callback*/
					}
				}, stars);
				stp.load(null, null, function(){
				});
			});
			
			stars.render(Ext.get('rating' + figure));
			stars.fireEvent('rate', this, '');
		},
		
		getImageSets : function() {
			//read cookie to see if logged in
			
			/*check for cookie*/
			if (document.cookie) {
				var userInfo = Utilities.readCookie();
				if (userInfo) {
					user.id = userInfo[0];
					user.email = userInfo[1];
				}
			}
						
			if (globals.online) {
				
				var stp = new Ext.data.ScriptTagProxy({
					url:  config.serverRoot + String.format(config.mySlidesGetPath, config.siteId, user.email, user.pwd, user.id),
					callbackParam: 'cb',
					timeout: config.serverTimeout
				});
				
				stp.on('loadexception', function(o, data, cb, e){
				
					var showSets = false;
					
					try {
						showSets = (data && data.length > 0 && data[0].children.length);
					}
					catch(e) {}
				
					if (showSets) {
						var tpl = new Ext.XTemplate.from('imagesetsTemplate');
						tpl.overwrite('imagesets-user-container', data);
						Ext.fly('imagesets-creating').setDisplayed(false);						
						Ext.fly('imagesets-user').setDisplayed(true);
					}
					else {
						Ext.fly('imagesets-creating').setDisplayed(true);						
						Ext.fly('imagesets-user').setDisplayed(false);
					}
				});
				
				stp.load(null, null, function(){});
			}
		},
		
		
		downloadSample : function(setTitle, slides, filename) {
		
			var downloadWin = new Ext.Window({
		    layout :'fit',
		    title : lang.download_sample_title,
		    maximizable : false,
		    width : 400,
		    height : 300,
		    autoScroll : true,
		    closeAction : 'close',
	    	modal: true,
	    	plain: true,
	    	html : '<div id="downloadContent"><p>' + lang.loading + '</p></div>'
		  });
			
			downloadWin.show();
		  
	  	var tplData = {
	  		title : setTitle,
	  		count : slides,
	  		file : filename
	  	}
	  	
	  	var tpl = new Ext.XTemplate.from('downloadTemplate');
	  	tpl.overwrite('downloadContent', tplData);
		
		}
		
	}
}();

Ext.onReady(Home.init, Home);

