

var XMas = new Class({
	
	swf: null,
	googleImageSearch: null,
	currentSearch: null,
	searchQueue: [],
	
	
	initialize: function() {
		google.load('search', '1');
		google.setOnLoadCallback(this.onGoogleLoad.bind(this));
		window.addEvent('domready', this.onDOMReady.bind(this));
		
	},
	
	onDOMReady: function() {
		this.swfInit();
	},
	
	onGoogleLoad: function() {
		this.googleImageSearch = new google.search.ImageSearch();
		this.googleImageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE, google.search.ImageSearch.IMAGESIZE_MEDIUM);
		this.googleImageSearch.setSearchCompleteCallback(this, this.onGoogleSearchComplete.bind(this), null);
		
		//this.search('cat'); // for testing
	},
	
	onGoogleSearchComplete: function() {
		if(this.googleImageSearch.results && this.googleImageSearch.results.length > 0) {

			var xmlString = //"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
							"<response>\n" +
							"	<query text=\"" + this.currentSearch.query + "\" />\n" +
							"	<imagesList>\n";

			var results = this.googleImageSearch.results;
			console.debug('results:', results);
			for (var i = 0; i < results.length; i++) {
				xmlString += "		<image src=\"" + results[i].tbUrl + "\" " + 
										  "width=\"" + results[i].tbWidth + "\" " +
										  "height=\"" + results[i].tbHeight + "\" " + 
										  "small_width=\"" + results[i].tbWidth + "\" " + 
										  "small_height=\"" + results[i].tbHeight + "\" />\n";
			}
			xmlString += "	</imagesList>\n</response>";

			// pass results to the flash
			$('xMas').searchFeedResults(xmlString);
			//Swiff.remote(this.swf, 'searchFeedResults', xmlString);
		}
		
		// proceed with the queue
		this.currentSearch = false; // the search cycle has ended
		this.searchQueueShift();
	},
	
	search: function(query, numImagesToGet) {
		var searchId = Math.random();
		this.searchQueue.push({ 'id': searchId, 'query': query.trim(), 'numImagesToGet': numImagesToGet });
		this.searchQueueShift();
	},
	searchQueueShift: function() {
		if(!this.currentSearch) {
			this.currentSearch = this.searchQueue ? this.searchQueue.shift() : false;
			console.debug('current search: ', this.currentSearch);
			if(this.currentSearch) {
				this.googleImageSearch.execute(this.currentSearch.query);
			}
		}
	},
	
	
	
	
	
	
	
	swfInit: function() {
		if(Browser.Plugins.Flash.version >= 8) {
			this.swf = new Swiff('/xmas/xmas1.swf?r=' + Math.random(), {
			    id: 'xMas',
				name: 'xMas',
			    container: 'allContainer',
			    width: '100%',
			    height: '100%',
			    params: {
				    allowScriptAccess: 'always',
			        wmode: 'opaque',
					scale: 'noscale',
					salign: 'lt'
			    },
			    vars: {
			        'LAYOUT_PATH': "/xmas/",
					'ENVIRONMENT': "web",
					'LANG': GLOBAL_OPTIONS.languageId,
					'SCREEN_WIDTH': screen.width,
					'SCREEN_HEIGHT': screen.height,
					'GREETING_ID': GLOBAL_OPTIONS.greetingId
			    }
			});
		}
		
		$('allContainer').setStyle('visibility', 'visible');
	}
	


});

var xmas = new XMas();