/*jslint browser: true */
/*global $, $$, Autocompleter, Element, Event, Fx, window */

(function() {
// start namespace
	
	var is_ie/*@cc_on = {
		version : parseFloat(navigator.appVersion.match(/MSIE (.+?);/)[1])
	}@*/;

	window.addEvent('domready', function(){
	
		// no js for IE 5
		if (is_ie && (is_ie.version < 7)) {return false;}

		// flag js support
		$$('body')[0].addClass('js');
		
		/* slider */
		$$('.digest').each(function(objElm){
			objElm.addClass('closed');
			var header = objElm.getElement('h3');
			var inner = objElm.getElement('div');
			var myVerticalSlide = new Fx.Slide(inner, {
				duration: 'long',
				transition: 'cubic:out',
				link: 'cancel'
			}).hide();
			var toggler = new Element('a', {
		  	'href': '#',
		  	'text': 'VIEW'
			}).inject(header);
			header.addEvents({
	    	'click': function(e){
	      	e = new Event(e).stop();
	      	if (objElm.hasClass('closed')) {
	      		objElm.removeClass('closed');
	      		myVerticalSlide.slideIn();
	      		toggler.set('text','CLOSE');
	      	} else {
	      		objElm.addClass('closed');
	      		myVerticalSlide.slideOut();
	      		toggler.set('text','VIEW');
	      	}
	      }
			});
		});
		
		/* revolver */
		var pause = false;		
		function revolver() {
			if (pause == false) {
				var req = new Request({
					method: 'get',
					url: '/_functions/php/latest/',
					onSuccess: function(responseHTML) {
						temp = new Element('div');
						temp.set('html', responseHTML);
	
						lastItemText = $$('#realTimeTweets ul li .entry-content').getLast().get('text');
						lastItemAge = $$('#realTimeTweets ul li .published').getLast().get('text');
						newItemText = temp.getElements('li .entry-content')[0].get('text');
						newItemAge = temp.getElements('li .published')[0].get('text');
	
						if (lastItemText != newItemText) {
							$$('#realTimeTweets ul')[0].adopt(temp.childNodes);
							$$('#realTimeTweets li').getLast().setStyle('position', 'relative').setStyle('left', '-406px').setStyle('top', '76px');
							$$('#realTimeTweets li').each(function(objElm){
								objElm.set('morph', {
									duration: 'long',
									unit: 'px',
									transition: 'cubic:out'
								});
							});
							$$('#realTimeTweets li').getLast().set('morph', {
								duration: 'long',
								unit: 'px',
								transition: 'cubic:out',
								onComplete: function() {
									$$('#realTimeTweets ul')[0].set('html', responseHTML);
								}
							});
							$$('#realTimeTweets li')[0].morph({top: -76, left: 203});
							$$('#realTimeTweets li')[1].morph({top: 0, left: -203});
						} else if (lastItemAge != newItemAge) {
							$$('#realTimeTweets ul')[0].set('html', responseHTML);
						}
					},
					noCache: true
				}).send();
			}
		}
		if ($('realTimeTweets')) {
			$('realTimeTweets').addEvents({
				'mouseover' : function() {pause = true;},
				'mouseout' : function() {pause = false;}
			});
			setInterval(revolver,7500);
		}
		
		/* autocompleter */
		if ($$('#formSearch .text')[0] && window.Autocompleter) {
			var tokens = ['AA', 'Abbey', 'Absolut', 'AC Milan FC', 'Accenture', 'Adidas', 'AIG', 'Air France', 'ALDI', 'Allianz', 'Allstate', 'Amazon', 'American Airlines', 'American Apparel', 'American Express', 'Amstel', 'Apple', 'Aprilia', 'Aquafina', 'Aral', 'Arby\'s', 'Argos', 'Arsenal FC', 'Ask', 'Aston Martin', 'AT&T', 'Auchan', 'Audi', 'Aveda', 'Avon', 'AXA', 'Bacardi', 'Baidu', 'Baileys', 'Ballantine\'s', 'Bang & Olufsen', 'Bank of America', 'Bank of China', 'Barcelona FC', 'Barclays', 'BBC', 'BBVA', 'Beck\'s', 'Beeline', 'Best Buy', 'Birds Eye', 'BlackBerry', 'BMI', 'BMW', 'Bombay Sapphire', 'Boots', 'Borders', 'Bose', 'BP', 'British Airways', 'Brylcreem', 'BskyB', 'BT', 'Bud Light', 'Budweiser', 'Burberry', 'Burger King', 'Cannon', 'Canon', 'Carling', 'Carlsberg', 'Carrefour', 'Carte Noire', 'Cartier', 'Chanel', 'Channel 5', 'Chase', 'Chelsea FC', 'Chevrolet', 'Chevron', 'China Construction Bank', 'China Mobile', 'Chivas', 'Churchill', 'Cirque de Soleil', 'Cisco', 'Citi', 'Cobra', 'Coca-Cola', 'COI', 'Coke', 'Colgate', 'Comparethemarket', 'Confused', 'Contrex', 'Corona', 'Cosmopolitan', 'Costa', 'Crest', 'Currys', 'Dasani', 'Debenhams', 'Dell', 'Dermalogica', 'Diet Pepsi', 'Digital UK', 'Direct Line', 'Discovery', 'Dixons', 'DKNY', 'Dolce and Gabbana', 'Douwe Egberts', 'Dove', 'Dr. Pepper', 'Dunhill', 'Duracell', 'Easyjet', 'eBay', 'Ecotricity', 'Elle', 'Emirates', 'Energy Saving Trust', 'Esprit', 'Esso', 'Estee Lauder', 'Evian', 'Exxon', 'Famous Grouse', 'Fanta', 'Fendi', 'Ferrari', 'Ferrari F1', 'FHM', 'Financial Times', 'First Direct', 'Folgers', 'Ford', 'Freeview', 'G-Star', 'Gaggia', 'Galaxy', 'Game', 'Gap', 'Garnier', 'Gatorade', 'GEICO', 'GHD', 'Gillette', 'Goldman Sachs', 'Google', 'Gordon\'s', 'GQ', 'Grolsch', 'Guardian', 'Gucci', 'Guinness', 'H&M', 'Haribo', 'Harley Medical Group', 'Havana Club', 'HBO', 'Heineken', 'Heinz', 'Hennessy', 'Hermes', 'HMV', 'Home Depot', 'Honda', 'Hovis', 'HP', 'HSBC', 'Hugo Boss', 'Hyundai', 'IBM', 'ICBC', 'IKEA', 'ING', 'Intel', 'iPhone', 'iPod', 'iTunes', 'ITV', 'Jack Daniel\'s', 'Jacobs', 'John Lewis', 'Johnnie Walker', 'Jose Cuervo', 'KFC', 'Kilgour', 'KitKat', 'Kronenbourg 1664', 'L\'Oreal', 'Lancome', 'Last.fm', 'Lavazza', 'learndirect', 'Lego', 'Levissima', 'Lexus', 'Liverpool FC', 'Lloyds', 'Lotus', 'Louis Vuitton', 'Lucozade', 'Luella Bartley', 'Lurpak', 'LV', 'Maclaren F1', 'Malibu', 'Manchester United FC', 'Marie Claire', 'Marks and Spencer', 'Mars', 'Max Factor', 'Maxwell House', 'Maybelline', 'McCain', 'McDonald\'s', 'Mercedes', 'MetLife', 'Microsoft', 'Microsoft Xbox', 'Microsoft Xbox 360', 'Miller Lite', 'Mobil', 'Moet & Chandon', 'Moneysupermarket', 'Mountain Dew', 'Movistar', 'MTS', 'MTV', 'Mulberry', 'Nero', 'Nescafe', 'Nespresso', 'Next', 'Nicorette', 'Nike', 'Nintendo DS', 'Nintendo Game Boy', 'Nintendo GameCube', 'Nintendo Wii', 'Nissan', 'Nivea', 'NME', 'Nokia', 'NPower', 'NTT DoCoMo', 'O2', 'Olay', 'Old Navy', 'Oracle', 'Oral-B', 'Orange', 'Orangina', 'Panasonic', 'Perrier', 'PetroChina', 'Peugeot', 'Pizza Express', 'Pizza Hut', 'play.com', 'Poland Spring', 'Porsche', 'Powerade', 'Prada', 'Pret a Manger', 'Pringles', 'Proctor and Gamble', 'Puma', 'Pure Life', 'RAC', 'Radio 1', 'Ralph Lauren', 'RBC', 'RBS', 'Real Madrid FC', 'Red Bull', 'Reebok', 'Renault', 'Repsol', 'Richard James', 'Rolex', 'Ryanair', 'Samsonite', 'Samsung', 'San Miguel', 'Sandals', 'Santander', 'SAP', 'Secret', 'Shell', 'Shiseido', 'Siemens', 'Signal', 'Skoda', 'Skol', 'Sky TV', 'Smirnoff', 'Solarcentury', 'Sony', 'Sony Ericsson', 'Sony Playstation 2', 'Sony Playstation 3', 'Sony PSP', 'Specsavers', 'Sporting Bet', 'Sprite', 'Standard Chartered', 'Starbucks', 'State Farm', 'Stella Artois', 'Stella Mcartney', 'SUBWAY', 'Sumitomo Life Insurance', 'T-Mobile', 'Taco Bell', 'Tag Heuer', 'Tampax', 'Target', 'TD', 'Tesco', 'Texaco', 'Thomas Cook', 'Three', 'Tim Horton\'s', 'Topshop', 'Toyota', 'Vauxhall', 'Verizon Wireless', 'Versace', 'Virgin Atlantic', 'Visa', 'Vittel', 'Vodafone', 'Vogue', 'Volkswagen', 'Volvic', 'Wagamama', 'Waitrose', 'Wal-Mart', 'Wella', 'Wells Fargo', 'Wendy\'s', 'Wired magazine', 'WKD', 'Yahoo!', 'Yellow Pages', 'Zara', 'Zurich'];
			var el = $$('#formSearch .text')[0];
			// remove default text from inputs
			el.addEvents({
				'focus' : function() {
					if (this.hasClass('searchReturnedValue')) {return false;}
					if (this.value == this.defaultValue) {this.value = ''; this.addClass('focus');}
				},
				'blur' : function() {
					if (this.hasClass('searchReturnedValue')) {return false;}
					if (this.value === '') {this.value = this.defaultValue;this.removeClass('focus');}
				}
			});
			new Autocompleter.Local(el, tokens, {
				'minLength': 1, // We need at least 1 character
				'selectMode': 'type-ahead', // Instant completion
				'multiple': true // Tag support, by default comma separated
			});
		}
		
	});
	
	window.addEvent('load', function(){
	
		// detect and flag if no image support
		if ($('isd')) {
			var x = $('isd').offsetWidth;
			if (x != '1') {
				$$('body')[0].addClass('noImgs');
			}
		}
		
	});
	
	/*
	window.addEvent('resize', function(){
	
		if (is_ie && (is_ie.version < 6)) {return false;}
		if ($('chatHistory')) {
			resLayout();
		}
	
	});
	
	window.addEvent('beforeunload', function(){
	
		// no js for IE 5
		if (is_ie && (is_ie.version < 6)) {return false;}
		
	});*/
	
// end namespace
})();