window.loaded_fonts = {};

var Expo = {
	
	hexvals: ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"],
	
	parse_rgba: function(rgba) {
		rgba = rgba.split(',');

		var c, current, filterValue = "";

		for (c = 0; c < rgba.length; c++) {
			if (c !== 3) {
				// RGB
				current = parseInt(rgba[c], 10);
				if (current < 0) {
					current = 0;
				} else if (current > 255) {
					current = 255;
				}
				// division gives us the first hex component and the modulo gives us the second
				filterValue += (this.hexvals[parseInt((current / 16), 10)] + this.hexvals[parseInt((current % 16), 10)]);
			} else {
				// the alpha
				current = parseFloat(rgba[c], 10);
				if (current < 0) {
					current = 0;
				} else if (current > 1) {
					current = 1;
				}
				current = current * 255;

				// Get the first hex component
				filterValue = (this.hexvals[parseInt((current / 16), 10)] + this.hexvals[parseInt((current % 16), 10)]) + filterValue;
			}
		}

		return filterValue;
	},
	
	font_loaded: function(font) {
		if(window.loaded_fonts.hasOwnProperty(font) && window.loaded_fonts[font] === true) {
			return true;
		} else {
			window.loaded_fonts[font] = true;
			return false;
		}
	},
	
	load_fonts: function(fonts) {
		if(Browser.Engine.webkit) {
			fonts.each(function(font) {
				if(!Expo.font_loaded(font)) {
					var e = document.createElement("link");
					e.href = '/media/fonts/css/' + font + '.css';
					e.type= "text/css";
					e.rel = "stylesheet";
					document.getElementsByTagName("head")[0].appendChild(e);
				}
			});
		} else {
			fonts.each(function(font) {
				if(!Expo.font_loaded(font)) {
					var e = new Element("script", {
						src: '/media/fonts/js/' + font + '.js',
						type: "text/javascript",
						events: {
							load: function() {
								$$('.textblock').each(function(item) {
									item.getElements('h3, p').each(function(el) {
										if(el.getStyle('font-family').match(font)) {
											Cufon.replace(el);
										}
									});
								});
							}
						}
					}).inject(document.head);
				}
			});
		}
	}
};

function fix_browsers() {
	if(Browser.Engine.trident) {
		$$('.textblock').each(function(block) {
			var opa = block.rel;
			if(typeof opa !== 'undefined' && opa.match(/rgba/)) {
				var rgba = opa.match(/(([0-9]{1,3})\s*,\s*){3}((0(\.[0-9]+)?)|1(\.0)?)/);
				
				if (rgba !== null && typeof rgba[0] === 'string') {
					var filtered = Expo.parse_rgba(rgba[0]);
					var fl = 'progid:DXImageTransform.Microsoft.gradient(startColorstr=#FILTERED,endColorstr=#FILTERED);'.replace(/FILTERED/g, filtered);
					block.setStyles({
						background: 'transparent',
						zoom: '1',
						filter: fl,
						'-ms-filter': fl
					});
				}
			}
			
		});
	} else if(Browser.Engine.gecko && Browser.Engine.version < 19 ) {
		$$('.textblock').each(function(block) {
			var opa = block.get('rel');
			if(opa.match(/rgba/)) {
				var rgba = opa.match(/([0-9]+), ([0-9]+), ([0-9]+), ([0-9\.]+)/),
					img = '/image/pixel/' + rgba[1] + '/' + rgba[2] + '/' + rgba[3] + '/' + (127-Math.round(rgba[4]*127));

				block.setStyle('background', 'transparent url(' + img + ') repeat');
			}
			
		});
	} else {
		$$('.textblock').each(function(b) { b.removeAttribute('rel'); });
	}
}


window.addEvent('domready', function() {
	$$('.textblock').addClass('scroll');
	if(fonts.length > 0) {
		Expo.load_fonts(fonts);
	}

	fix_browsers();

	custom_scrollbars();
});

