var caniuse = function() {
		
	document.write('<style>#info_faq {display:none;}</style>');
		
	var search_ids = {};
	var search_query = false;
	
	//This data comes from data.js
	var eras,agents,statuses,data;
	
	var opts = {};
	
	var browser_compare = true;
	
	var opt_ids = ['agents','eras','cats','alts','statuses'];
	
	var table_arr, sort, summary, era_count, detailed, featcount, orig, bw_shaded;

	var summ_html = '';
	var first_load = true;
	var browseroptList = [];
	
	function makePermaLink(feat) {
		return '<a class="permalink" href="#feat=' + feat + '" title="Link to just this table">#<\/a> ';
	}

	function makeCheckBox(val,label,uncheck) {
		var ch = uncheck?'':' checked';
		return '<label><input type="checkbox" onclick="caniuse.MC()" value="' + val + '"' + ch + '>'
			+ label + '<\/label>';
	}

	function makeBrowserCheckBox(val,label,uncheck) {
		var ch = uncheck?'':' checked';
		return '<label><input type="checkbox" onchange="caniuse.MC(0,this)" value="' + val + '"' + ch + '>'
			+ label + '<\/label>';
	}

	
	function makeRadioButton(val,label,name,uncheck) {
		var ch = uncheck?'':' checked';
		return '<label><input type="radio" onclick="caniuse.MC()" name="' + name + '" value="' + val + '"' + ch + '>'
			+ label + '<\/label>';
	}
	
	function makeSelectOptions(items) {
		var list = ''
		$.each(items,function(val,label) {
			list += '<option value="'+val+'">'+label+'</option>';
		});
		return list;
	}
	
	function makeFieldset(id,legend,options,with_all) {
		var all_opt = with_all?'<label><input type="checkbox" onclick="caniuse.selAll(this);" value="All" checked>All<\/label>':'';
	
		return '<fieldset id="' + id + '">'
			+ '<legend>' + legend + '<\/legend>'
			+ all_opt
			+ options
			+ '<\/fieldset>';
	}
	
	function getAgentData(item) {
		var opt = $(item);
		
		var optval = opt.val();

		var optbits = optval.split('|');
		
		return {
			b_id:	optval,
			name:	optbits[0],
			ver:	optbits[1],
			browser: agents[optbits[0]].browser,
			full_version: fixVersion(optbits[1])
		}
		
	}
	
	function makeComparison() {
		
		if(caniuse.curOption && !caniuse.curOption.checked) {
			$.each(browseroptList,function(i) {
				if(this == caniuse.curOption) {
					browseroptList.splice(i,1);
				}
			});
		} else if(caniuse.curOption) {
			browseroptList.push(caniuse.curOption);		
		}
		
		if(caniuse.curOption && browseroptList.length > 2) {
			var oldest = browseroptList.shift();
			$(oldest).get(0).checked = false;
		}
	
		var compared_agents = [];
		
		var all_opts = $('#comp_opts .browser_opts :checkbox:checked');
		
		if(all_opts.length < 2) {
			$('#content').empty();
			return;
		} 
		
		$(all_opts).each(function(i,item) {
			compared_agents[i] = getAgentData(this);
		});
	
		var c = compared_agents;

		var new_url = '#compare=y&b1=' + c[0].b_id + '&b2=' + c[1].b_id;
		
		document.location.href = new_url;
		
		var compHTML = [];
		
		var score1=0, score2=0;
		
		var info = {
			n:'No',
			y:'Yes',
			a:'Partial',
			u:'Unknown'
		};
		
		$.each(data,function(feat_id,feat) {
				
			var show = false;
				
			$.each(feat.categories,function() {
				if(opts.cats[this] && opts.statuses[feat.status]) {
					show = true;
				}
			});
			
			if(!show) return;
		
			if(feat_id == 'summary') return;
			
			var stat1 = feat.stats[c[0].name][c[0].ver];
			var stat2 = feat.stats[c[1].name][c[1].ver];
			
			if(stat1 == 'j' || stat1 == 'p') {
				stat1 = 'n';
			} else if(stat1 == 'a') {
				score1 += .5;
			} else if(stat1 == 'y'){
				score1++; 
			}
			
			if(stat2 == 'j' || stat2 == 'p') {
				stat2 = 'n';
			} else if(stat2 == 'a') {
				score2 += .5;
			} else if(stat2 == 'y'){
				score2++; 
			}
			
			if(stat1 != stat2) {
				link = '#feat=' + feat_id;
			
				compHTML.push(
					'<tr>'
					+ '<th><a href="'+link+'" onclick="caniuse.feat=\''+feat_id+'\';caniuse.setMode(\'wciu\');return false;">' + feat.title + '</a></th>'
					+ '<td class="' + stat1 + '">' + info[stat1] + '</td>'
					+ '<td class="' + stat2 + '">' + info[stat2] + '</td>'
					+ '</tr>'
				);
			}
		
		});

		var b1 = c[0].browser + ' ' + c[0].full_version;
		var b2 = c[1].browser + ' ' + c[1].full_version;
		
		var winner = b1, loser = b2, draw = false;
		
		if(score1 < score2) {
			winner = b2;
			loser = b1;
		} 
		
		if(score1 == score2) {
			draw = true;
		}
		
		var wscore = Math.max(score1,score2);
		var lscore = Math.min(score1,score2);
		
		var multiple = Math.round((wscore/lscore)*10)/10;
		
		if(multiple == 1) draw = true;
		
		var pre = 'Based on this information, the feature support in ' + winner;
		
		if(!draw) {
			var score_info = pre + ' is <strong>' + multiple + ' times better</strong> than in ' + loser;
		} else {
			var score_info = pre + ' and ' + loser + ' is equally good.'; 
		}
		
		var caption = '<caption>' + score_info + '</caption>';
		var thead = '<table id="browser_comparison">' + caption + '<thead><tr><th></th><th class="'+c[0].name+'">'+b1+'</th><th class="'+c[1].name+'">'+b2+'</th></tr></thead>';
		var tbody = compHTML.join('');

		
		var output = thead + tbody;

		$('#content').html(output);

	}
	
	function getBrowserOptions() {
	
		var optList = '';

		var given_browser1 = $.getURLParam('b1');
		var given_browser2 = $.getURLParam('b2');
	
		$.each(agents,function(ag_name,ag_data) {
			
			optList += '<fieldset class="browser_opts"><legend>' + ag_data.browser +'</legend>';
			
			$.each(ag_data.versions,function(i,ver_num) {
				if(i > 0 && ver_num == ag_data.versions[i-1]) return;
			
				var browser_id = ag_name + '|' + ver_num;
				var uncheck = (given_browser1 != browser_id && given_browser2 != browser_id);
			
				optList += makeBrowserCheckBox(browser_id,fixVersion(ver_num),uncheck);
			});

			optList += '</fieldset>';
			
		});
		
		return optList;
	}

	function makeCompareOptions() {
		var optList = '', st_opts = '', cat_opts = '';
		
		resetData();
		
		$.each(statuses,function(st_id,st_name) {
			st_opts += makeCheckBox(st_id,st_name);
		});
		
		
		var done_cats = {};
		// Make unique list of categories
		$.each(data,function(feat_id,feat) {
			$.each(feat.categories,function() {
				if(done_cats[this]) return false;
				
				cat_opts += ('<p><input type="checkbox" onclick="caniuse.MC()" value="' + this + '" checked><a onclick="return caniuse.MC(\'' + this + '\')" href="#">' + this + '<\/a><\/p>');
				done_cats[this] = this;
			});
		});
		
		var opts1 = getBrowserOptions();
		
		$('#options').html(
			'<p class="browser_compare_note">Select two browsers to compare</p>'
			+ makeFieldset('cats','Category',cat_opts,true)
			+ makeFieldset('statuses','Status',st_opts,true)
			+ '<form id="comp_opts">' + opts1 + '</form>'
		);
		
		//Uncheck unofficial
		$('#statuses input[value="All"],#statuses input[value="unoff"]').removeAttr('checked');

		//Add checked opts to already checked array
		$('#comp_opts .browser_opts :checkbox:checked').each(function() {
		
			if(this.value == $.getURLParam('b1'))  {
				browseroptList.push(this);
			}
			if(this.value == $.getURLParam('b2')) {
				caniuse.curOption = this;
			}
		});
	}
	
	

	function makeOptions() {

		var ag_opts = '';
		var cat_opts = '';
		var era_opts = '';
		var st_opts = '';
		var done_cats = {};
		
		$.each(opt_ids,function() {
			opts[this] = {};
		});
		
		resetData();

		$.each(agents,function(ag_id,ag_data) {
			ag_opts += makeCheckBox(ag_id,ag_data.browser);
		});
		
		$.each(eras,function(era_id,era_name) {
			era_opts += makeCheckBox(era_id,era_name);
		});
		
		$.each(statuses,function(st_id,st_name) {
			st_opts += makeCheckBox(st_id,st_name);
		});
		
		// Make unique list of categories
		$.each(data,function(feat_id,feat) {
			$.each(feat.categories,function() {
				if(done_cats[this]) return false;
				
				cat_opts += ('<p><input type="checkbox" onclick="caniuse.MC()" value="' + this + '" checked><a onclick="return caniuse.MC(\'' + this + '\')" href="#">' + this + '<\/a><\/p>');
				done_cats[this] = this;
			});
		});
		
		var optHTML = (''
			+ makeFieldset('cats','Category',cat_opts,true)
			+ makeFieldset('agents','Web Browser',ag_opts,true)
			+ makeFieldset('eras','Time period',era_opts,true)
			+ makeFieldset('statuses','Status',st_opts,true)
			+ makeFieldset('alts','Alternatives',
				makeCheckBox('p',' Accept plug-ins',true) 
				+ makeCheckBox('j',' Accept JS solution',true) 
			)
			+ makeFieldset('search','Search','<input type="text" name="search"><p></p>')
			+ makeFieldset('sort','Sort','<select name="sort" onchange="caniuse.MC()">'
				+ makeSelectOptions({
					'score':'Most supported first',
					'rscore':'Least support first',
					'alpha':'Alphabetical',
					'ralpha':'Reverse Alphabetical'
				})
				+ '</select>'
				+ '<p></p>'
			)
			+ makeFieldset('misc','Other options',
				makeCheckBox('detailed',' Detailed tables')
				+ makeCheckBox('bw_shaded',' Accessible colors',true)
			)
		);
		
		$('#options').html(optHTML);
		
		//Uncheck unofficial
		$('#statuses input[value="All"],#statuses input[value="unoff"]').removeAttr('checked');
	}
	
	function setOpt(item,sel_cat,option) {
		var allChecked = true;
	
		var remNums = [];
	
		// Loop through all options to see what's selected
		$('#' + item + ' input').each(function(i,checkbox) {
		
			var val = $(checkbox).val();
			
			// Category is selected
			if(sel_cat) {
				
				var is_sel = sel_cat == val;
				if(val == 'Summary') is_sel = true;
	
				opts.cats[val] = is_sel;
				checkbox.checked = is_sel;
				
				return;
			} 
			
			if(checkbox.checked) {
				opts[item][val] = true;
			} else {
				opts[item][val] = false;
				
				if(option) {
					delete option[val];
					if(item == 'eras' && val != 'All') {
						remNums.push(i-1);
					}
				}
				allChecked = false;
			}
		});
		
		if(item == 'eras') {
		
			remNums.reverse();
			//Remove from agents too
			$.each(agents,function(ag_name,ag_data) {
				$.each(remNums,function() {
					ag_data.versions.splice(this,1);
				});
			});
		}
		
		
		// Check/uncheck "All" option
		var all = $('#' + item + ' input[value="All"]');
		if(all.length) all[0].checked = allChecked;
	}
	
	function fixVersion(ver) {
		ver = ver+'';
		if(ver.indexOf('.') != -1) {
			return ver;
		} else if(ver.indexOf('x') != -1) {
			return ver.replace('x','.*');
		} else {
			return ver + '.0';
		}
	}
	
	function setFixedHead() {
		// Remove if created before
		$('table#clone').remove();

		var main_table = $('table#no_details');
		
		var clone = main_table.clone();
		clone.attr('id','clone');
		
		clone.css({
			'left':main_table.offset().left,
			'width':$('table#no_details').width()
		});
		$('#content').append(clone);


		$(window).bind('scroll resize',function () { 
			var head_left = main_table.offset().left;
			var head_pos = window.scrollY - main_table.offset().top;
			clone.css('visibility',(head_pos > 0)?'visible':'hidden');
			clone.css('left',head_left);
		});
	}

	function makeHead() {
		var colspan = detailed?'':' colspan="2"';

		var head = '<thead><tr><th class="first"' + colspan + '><\/th>';

		$.each(agents,function(ag_id,ag_data) {
			var browser = detailed?ag_data.browser:ag_data.abbr;
			head += ('<th class="' + ag_id + '">' + browser + '<\/th>');			
		});

		head += '<\/tr><\/thead>';
		return head;
	}
	
	function makeBody(item,feat_id) {
		var body = '';
		
		item.support_score = 0;
		item.result = {};
		item.headcol = false;
		
		var era_num = 0;
		
		$.each(eras,function(era_id, era_name) {
			body += makeEraRow(era_id,era_name,item,era_num,feat_id);
			era_num++;
		});

		body = detailed?'<tbody>' + body + '<\/tbody>':body;
		
		return body;
	}
	
	function makeFoot(result) {
		if(summary) return '';
	
		var foot = '<tfoot><tr><td colspan="5">Conclusion: ';
		
		var str;

		//Conclusion
		$.each(result,function() {
			str = this.join(', ');
		});
		
		if(str) {
			foot += '<span class="n">Not ready<a href="#not_ready_note">*</a>.<\/span> Waiting for ' + str + ' to expire';
		} else {
			foot += '<span class="y">Ready to be used now!<\/span>';
		}
		
		foot += '<\/td><\/tr><\/tfoot>';
		
		return foot;
	}
	
	function getTableClass() {
		var cl = [];
		$.each(opts.alts,function(alt_id,alt_selected) {
			if(alt_selected) {
				cl.push('alt_' + alt_id);
			}
		});
		return cl.join(' ');
	}
	
	function makeTableHTML(item,feat_id) {
	
		var body = makeBody(item,feat_id);
	
		if(!detailed) {
			return body;
		}
	
		var table_class = getTableClass();
	
		var status = statuses[item.status] || item.status;
	
		var spec = item.spec?'<p class="spec"><span class="status ' + item.status + '"> - ' + status + '</span>':'';
		
		var linklist = '';
		if(item.links) {
			linklist += '<dl><dt>Resources:<\/dt>';
			
			$.each(item.links,function() {
				linklist += '<dd><a href="' + this.url + '">' + this.title + '<\/a><\/dd>';
			});

			linklist += '<\/dl>';
		}
		
		var description = '<p class="description">' + item.description + '<\/p>';
		
		var notes = item.notes?'<p class="notes"><b>Note:<\/b> ' + item.notes + '<\/p>':'';
	
		//TABLE
		var feat_html = '<div id="'+feat_id+'" class="' + item.categories.join(' ') + '">'
			+ '<h3>' + makePermaLink(feat_id) + '<a href="' + item.spec + '" title="View specification">' + item.title + '<\/a><\/h3>'
			+ spec 
			+ description	
			+ linklist
			+ '<table class="' + table_class + '">'
			+ makeHead()
			+ body
			+ makeFoot(item.result)
			+ '<\/table>'
			+ notes
			+ '<\/div>';
			
		return feat_html;
	}
	
	function makeTable(feat_id, item) {

		summary = (feat_id=='summary');

		var show = false;
		
		// Decide whether or not to show this feature
		$.each(item.categories,function() {
			if(search_query.length) {
				//This feature is in the search list
				if(search_ids[feat_id]) {
					show = true;
				}
			} else if(feat_id == caniuse.feat) {
				//Only showing this feature
				show = true;
			} else if(!caniuse.feat && (opts.cats[this] && opts.statuses[item.status] || summary)) {
				//Not only this feature, in selected category, in selected status, or summary table
				show = true;
			}
		
		});

		if(!show) return;

		// Increase feature count for summary calculation
		if(!summary) {
			featcount++;
		}

		var feat_html = makeTableHTML(item,feat_id);
		
		summ_html = '';
		
		var sorter;			
		if(sort == 'score' || sort == 'rscore') {
			sorter = item.support_score;
		} else {
			sorter = item.title.toLowerCase().match(/[a-z]+/);
		}

		if(!summary) {
			table_arr.push([sorter,feat_html]);
		} else {
			summ_html = feat_html;
		}
	}
	
	function makeSummaryCells(era_num) {

		var rowdata = '';
		
		$.each(agents,function(ag_id,ag_data) {
			var ver_key = ag_data.versions[era_num];
			
			var fract = ag_data.summary[era_num]/featcount;
			
			var red, green, blue;
			
			if(bw_shaded) {
				red = green = blue = Math.round(fract*150) + 104;
			} else {
				if(fract <= 0.5) {
					red = 255;
					green = fract*255*2;
				} else {
					red = (-1*fract)*255+(255*1.5);
					green = 255;
				}
				red = Math.round(red);
				green = Math.round(green);
				blue = 50;
			}
			
			var cell = '<td class="sum_cell" style="background-color:rgb(' + red + ',' + green + ',' + blue + ')">';
			
			var rank = Math.round(fract * 100) + '%';
			
			rowdata += (cell + fixVersion(ver_key) + ': ' + rank + '<\/td>');
			
		});
		
		return rowdata;
	}
	
	function makeEraRow(era_id,era_name,item,era_num,feat_id) {
		var column = '';

		if(!detailed && !item.headcol) {
			column = item.headcol = '<th class="feat_cell" rowspan="' + era_count + '">'
			+ makePermaLink(feat_id)
			+ '<a href="' + item.spec + '" title="View specification">' + item.title + '<\/a>'
			+ (item.status?' <span class="' + item.status + '" title="' + statuses[item.status] + '">[' + item.status + ']</span>':'')
			+ '</th>';
		} 

		var rowdata = '<th>' + era_name + '<\/th>';

		if(summary) {
			rowdata += makeSummaryCells(era_num);
		} else {
			$.each(agents,function(ag_id,ag_data) {
				var versions = ag_data.versions;
				
				var ver_key = versions[era_num];
				
				var stat = item.stats[ag_id][ver_key];
				
				var result = item.result;
				
				var span_attr = '';
				var show_cell = true;
				if(era_num < versions.length-1 && ver_key === versions[era_num+1]) {
					span_attr = ' rowspan="2"';
					ag_data.done_span = true;
				} else if(era_num > 0  && ver_key === versions[era_num-1] && ag_data.done_span) {
					show_cell = false;
					ag_data.done_span = false;
				}
				
				var cell = '<td class="' + stat + '"' + span_attr + '>' + fixVersion(ver_key) + '<\/td>';
				
				if(show_cell) {
					rowdata += cell;
				}
				
				//If P is on and has p, make stat be p
				if(stat.indexOf('p') != -1 && opts.alts.p) stat = 'y';
				if(stat.indexOf('j') != -1 && opts.alts.j) stat = 'y';
				
				if(typeof ag_data.summary[era_num] == 'undefined') {
					ag_data.summary[era_num] = 0;
				}
				
				if(stat.match(/[npj]/) !== null) {
					if(!result[era_id]) result[era_id] = [];
					result[era_id].push(ag_data.browser + ' ' + ver_key);
				} else if(stat.indexOf('a') != -1) {
					item.support_score += 0.75;
					ag_data.summary[era_num] += 0.75;
				} else if(stat.indexOf('u') == -1) {
					item.support_score++;
					ag_data.summary[era_num]++;
				}
			});
		} 
		
		return '<tr class="' + era_id + '">' + column + rowdata + '<\/tr>';
	}
	
	function resetData() {
		var copy = function(o) {return $.extend(true, {}, o)};
	
		// Get original data
		eras = copy(caniuse.eras);
		agents = copy(caniuse.agents);
		statuses = copy(caniuse.statuses);
		data = copy(caniuse.data);
	}
	
	function filterData(sel_cat) {
		
		resetData();
		

		if(browser_compare) {
			setOpt('cats',sel_cat,0);
			setOpt('statuses',0,statuses);
			return;
		}
		
		// Get selected option data
		sort = $('#sort select').val();
	
		setOpt('agents',0,agents);
		setOpt('cats',sel_cat,0);
		setOpt('alts');
		setOpt('eras',0,eras);
		setOpt('statuses',0,statuses);
		
		// Get new era count
		era_count = 0;
		for(er in eras) { era_count++; }
		
		// Reset summary statistics
		$.each(agents,function(ag_id,ag_data) {
			ag_data.summary = [];
		});
		
		// Empty table array
		table_arr = [];
		
		// Reset feature count
		featcount = 0;
		
		makeURL();
		
		if(bw_shaded) {
			$('body').addClass('access');
		} else {
			$('body').removeClass('access');
		}
	}
	
	function makeURL() {
		//Make a paramater URL from selected options
		
		//Don't change if feature is given or first time page is loaded
		if(caniuse.feat || first_load) {
			first_load = false;
			return false;
		}
	
		var param_str = '';
	
		$.each(opts,function(opt_id,opt_obj) {
			var value_list = [];
			for(val in opt_obj) {
				if(opt_obj[val]) {
					value_list.push(val);
					if(val == 'All') break;
				}
			}
			if(value_list.length) {
				param_str += '&' + opt_id + '=' + value_list.join(',');
			}
		});
	
		// Add sort order
		if(sort != 'score') {
			param_str += '&sort=' + sort;
		}
		
		detailed = miscOption('detailed');
		bw_shaded = miscOption('bw_shaded');
		
		if(!detailed) {
			// Add detail info
			param_str += '&nodetails=1';
		}
		
		if(bw_shaded) {
			// Add detail info
			param_str += '&bw_shaded=1';
		}
	
		param_str = '#' + param_str.substr(1);
		
		var old_url = document.location.href;
		
		if(old_url.indexOf('#') == -1) {
			old_url = '#' + old_url;
		}
		
		var url = old_url.replace(/#.*/,param_str);
		
		if(url != old_url) { 
			document.location.href = url;
		}
	}
	
	function sortTables() {
		var listSort = function(a,b) {
			return b[0] - a[0];
		};

		if(sort == 'score' || sort == 'rscore') {
			table_arr.sort(listSort);
		} else if(sort == 'alpha' || sort == 'ralpha') {
			table_arr.sort();
		}

		if(sort == 'ralpha' || sort == 'rscore') {
			table_arr.reverse();
		}
	}
	
	function makeContentHTML() {
		// Sort tables
		sortTables();
	
		var html_str = '';
		
		// Add tables
		$.each(table_arr, function() {
			html_str += this[1];
		});
		
		// Add summary
		html_str += summ_html;
		
		// Make no-details table
		if(!detailed) {
			html_str = '<table id="no_details" class="' + getTableClass() + '">' + makeHead() + html_str + '</table>';
		}
		
		// Start with "show all" link if only one feature is displayed
		if(caniuse.feat) {
			html_str = '<p id="showall"><a href="#" onclick="caniuse.reset();return false;">Show all tables</a></p>' + html_str;
		}
		
		// Hide if undefined
		html_str = html_str=='undefined'?'':html_str;
		
		if(table_arr.length == 0 
			&& !$.getURLParam('eras') 
			&& !$.getURLParam('agents')
			&& !$.getURLParam('statuses')
		) {
			html_str = '<p style="text-align:center">Um...dude.</p>';
		}
		
		// Set HTML
		$('#content').get(0).innerHTML = html_str;

		// Set Fixed header if no details
		if(!detailed) {
			setFixedHead();
		}
	}
	
	function makeModeOptions() {
		var modeOpts = $('<div id="mode_opts">\
			<button id="but-wciu" onclick="caniuse.setMode(this)">When can I use...</button>\
			<button id="but-b_compare" onclick="caniuse.setMode(this)">Browser comparison</button>\
		</div>');
		$('#options').before(modeOpts);
	}
	
	function makeContent(sel_cat) {

		// Filter data
		filterData(sel_cat);

		if(browser_compare) {
			makeComparison();
			return;
		}

		// Make table for each feature
		$.each(data, makeTable);
		
		// Make and show the generated table(s)
		makeContentHTML();
		
		return false;
	}
	
	function setOption(item) {
		var opt = $.getURLParam(item);
	
		if(!opt) return;
		
		var opts = opt.split(',');
	
		$('#' + item + ' input').each(function() {
			var checkbox = this;
		
			$.each(opts,function(i,cur_opt) {
				if(cur_opt == 'All' || $(checkbox).val() == cur_opt) {
					checkbox.checked = true;
					return false;
				} else {
					checkbox.checked = false;
				}
			});
		});
	}
	
	function miscOption(value,check) {
		var opt = $('#misc input[value="' + value + '"]').get(0);
		if(arguments.length > 1) {
			opt.checked = check;
		} else {
			return opt.checked;
		}
	}
	
// http://a.deveria.com/caniuse/index-test.html#agents=All&eras=now&cats=CSS3,CSS2,Summary&statuses=wd
	
	function setParamOptions() {
		$.each(opt_ids,function() {
			setOption(this);
		});
		
		caniuse.feat = $.getURLParam('feat');
		
		detailed = !$.getURLParam('nodetails');
		bw_shaded = $.getURLParam('bw_shaded');
		
		miscOption('detailed',detailed);
		miscOption('bw_shaded',bw_shaded);

		caniuse.hideopts = $.getURLParam('hideopts');
		
		if($.getURLParam('sort')) {
			caniuse.sort = $.getURLParam('sort');
			$('#sort select').val(caniuse.sort);
		}
	}
	
	function setSearch() {
		var timer;
		$('#search input').focus(function() {
			var inp = this;
			
			timer = setInterval(function() {
				var str = inp.value;
				
				if(search_query == str) return;
				search_query = str;

				search_ids = {};
				if(!str) return;
				
				var counter = 0;
				
				$.each(data,function(feat_id,feat) {
					var haystack = (feat.title + ' ' + feat.description).toLowerCase();
					var s = str.toLowerCase();
					var re = new RegExp('^' + s + '|[^a-zA-Z]' + s);
				
					if(re.exec(haystack) !== null) {
						search_ids[feat_id] = true;
						counter++;
					}
					
				});
				
				$('#search p').text(counter + ' result' + (counter==1?'':'s') + ' found');
				
				makeContent();
				
			},200);
		}).blur(function() {
			clearInterval(timer);
			if(!this.value.length) {
				makeContent();
				$('#search p').text('');
			}
		});
	}
	
	return {
		start:function() {
		
			$('#nojs, #info_faq').hide();
			
			$('h3:contains("FAQ"):first').toggle(function() {
				$('#info_faq').show();
			},function() {
				$('#info_faq').hide();
			});
			
		
			$('h3:contains("Notes"):first').toggle(function() {
				$('#info_notes').hide();
			},function() {
				$('#info_notes').show();
			});
			//Set options based on parameters
		
			if(caniuse.hideopts) {
				$('#legend,#options,#info_notes,#info_related').hide();
			}
			
			makeModeOptions();
			
			browser_compare = !!$.getURLParam('compare');
			
			var mode = 'wciu';
			
			makeOptions();

			if(!browser_compare) {
				setParamOptions();
				setSearch();
			} else {
				makeCompareOptions();
				mode = 'b_compare';
			}
			
			$('#but-'+mode).addClass('cur_mode');

			makeContent();

		},
		MC:function(val,item) {
			caniuse.curOption = item;
			makeContent(val);
			return false;
		},
		selAll:function(item) {
			var opts = $(item).parents('fieldset').find('input');
		
			opts.each(function() {
				this.checked = item.checked;
			});
			
			makeContent();
		},
		reset:function() {
			caniuse.feat = false;
			makeContent();
			$('#legend,#options,#info_notes,#info_related').show();
			$('#showall').remove();
			
		},
		setMode:function(cur_button) {
			var mode;

			if(typeof(cur_button) == 'string') {
				browser_compare = (cur_button == 'b_compare');
				cur_button = $('#but-'+cur_button);

			} else {
				mode = $(cur_button).attr('id').split('-')[1];
				browser_compare = (mode === 'b_compare');
			}

			$('#mode_opts button').removeClass('cur_mode');
			$(cur_button).addClass('cur_mode');
			
			if(!browser_compare) {
				makeOptions();
// 				setParamOptions();
				setSearch();
			} else {
				makeCompareOptions();
			}
			
			makeContent();
		},
		MakeComparison:function() {
			makeComparison();
		}
	};
}();

$(caniuse.start);