//livequery plugin for search highlighting

/* Copyright (c) 2007 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version: 1.0.2
 * Requires jQuery 1.1.3+
 * Docs: http://docs.jquery.com/Plugins/livequery
 */
(function($){$.extend($.fn,{livequery:function(type,fn,fn2){var self=this,q;if($.isFunction(type))fn2=fn,fn=type,type=undefined;$.each($.livequery.queries,function(i,query){if(self.selector==query.selector&&self.context==query.context&&type==query.type&&(!fn||fn.$lqguid==query.fn.$lqguid)&&(!fn2||fn2.$lqguid==query.fn2.$lqguid))return(q=query)&&false;});q=q||new $.livequery(this.selector,this.context,type,fn,fn2);q.stopped=false;$.livequery.run(q.id);return this;},expire:function(type,fn,fn2){var self=this;if($.isFunction(type))fn2=fn,fn=type,type=undefined;$.each($.livequery.queries,function(i,query){if(self.selector==query.selector&&self.context==query.context&&(!type||type==query.type)&&(!fn||fn.$lqguid==query.fn.$lqguid)&&(!fn2||fn2.$lqguid==query.fn2.$lqguid)&&!this.stopped)$.livequery.stop(query.id);});return this;}});$.livequery=function(selector,context,type,fn,fn2){this.selector=selector;this.context=context||document;this.type=type;this.fn=fn;this.fn2=fn2;this.elements=[];this.stopped=false;this.id=$.livequery.queries.push(this)-1;fn.$lqguid=fn.$lqguid||$.livequery.guid++;if(fn2)fn2.$lqguid=fn2.$lqguid||$.livequery.guid++;return this;};$.livequery.prototype={stop:function(){var query=this;if(this.type)this.elements.unbind(this.type,this.fn);else if(this.fn2)this.elements.each(function(i,el){query.fn2.apply(el);});this.elements=[];this.stopped=true;},run:function(){if(this.stopped)return;var query=this;var oEls=this.elements,els=$(this.selector,this.context),nEls=els.not(oEls);this.elements=els;if(this.type){nEls.bind(this.type,this.fn);if(oEls.length>0)$.each(oEls,function(i,el){if($.inArray(el,els)<0)$.event.remove(el,query.type,query.fn);});}else{nEls.each(function(){query.fn.apply(this);});if(this.fn2&&oEls.length>0)$.each(oEls,function(i,el){if($.inArray(el,els)<0)query.fn2.apply(el);});}}};$.extend($.livequery,{guid:0,queries:[],queue:[],running:false,timeout:null,checkQueue:function(){if($.livequery.running&&$.livequery.queue.length){var length=$.livequery.queue.length;while(length--)$.livequery.queries[$.livequery.queue.shift()].run();}},pause:function(){$.livequery.running=false;},play:function(){$.livequery.running=true;$.livequery.run();},registerPlugin:function(){$.each(arguments,function(i,n){if(!$.fn[n])return;var old=$.fn[n];$.fn[n]=function(){var r=old.apply(this,arguments);$.livequery.run();return r;}});},run:function(id){if(id!=undefined){if($.inArray(id,$.livequery.queue)<0)$.livequery.queue.push(id);}else
$.each($.livequery.queries,function(id){if($.inArray(id,$.livequery.queue)<0)$.livequery.queue.push(id);});if($.livequery.timeout)clearTimeout($.livequery.timeout);$.livequery.timeout=setTimeout($.livequery.checkQueue,20);},stop:function(id){if(id!=undefined)$.livequery.queries[id].stop();else
$.each($.livequery.queries,function(id){$.livequery.queries[id].stop();});}});$.livequery.registerPlugin('append','prepend','after','before','wrap','attr','removeAttr','addClass','removeClass','toggleClass','empty','remove');$(function(){$.livequery.play();});var init=$.prototype.init;$.prototype.init=function(a,c){var r=init.apply(this,arguments);if(a&&a.selector)r.context=a.context,r.selector=a.selector;if(typeof a=='string')r.context=c||document,r.selector=a;return r;};$.prototype.init.prototype=$.prototype;})(jQuery);


$(function() {
	//browser info for if and when we need to tweak
	var browser_version = $.browser.version,
		ie6, ie7, ie8, msie, 
		mozilla =	$.browser.mozilla,
		safari  =	$.browser.safari
		opera   =	$.browser.opera;
	
	switch (browser_version) {
		case '6.0':
			ie6 = msie = true;
			//alert('ie6');
			//alert('msie');
			break;
		case '7.0':
			ie7 = msie = true;
			//alert('ie7');
			//alert('msie');
			break;
		case '8.0':
			ie8 = msie = true;
			//alert('ie8');
			//alert('msie');
			break;
	}

	//tweak search box for Mac Firefox
	if(mozilla && navigator.userAgent.indexOf('Macintosh') > -1) {
		//console.log('mac FF');
		$('input.textfield_search').css({
			'padding':'0 .3em',
			'height':'1.6em'
		});
	}
	
	//default search value and behavior
	$('#form_search input:text').each(function(){
		var search_data = {
				default_color: '#C5C5C5',
				focus_color: '#000',
				default_value : this.value
			};
		
		$(this).focus(function() {
			this.style.color = search_data.focus_color;
			if(this.value === search_data.default_value) {
				this.value = '';
			}
		});
		$(this).blur(function() {
			if(this.value === '') {
				this.style.color = search_data.default_color;
				this.value = search_data.default_value;
			}
		});
	});
	
	/*
	//custom dropdown menu scope search
	$('#form_search ul.s_drop_down').hover(
		function() { //over
			$('ul', this).show();
		},
		function(){ //out
			$('ul', this).hide();
		}
	);
	$('#form_search ul.s_drop_downm li a').click(function(){
		//get category # from custom dropdown
		var category = $(this).attr('category'),
			text = $(this).text();
		
		//populate hidden form field with category #
		//and upate custom dropdown menu text
		$('#selectedsearch').val(category);
		$('#selectedsearch2').html(text);
		
		$('ul.s_drop_downm').hide();
		
		return false;
	});
	*/
	
	//if we're on signup or login page, focus username field
	var url = window.location.href;
	if( url.indexOf('login.php') > 1 || url.indexOf('signup.php') > 1 ) {
		$('#username').focus();
		
		$('#forgotpass_link').click(function() {
			$('#forgotpass').slideToggle();
		})
	}
	
	//if we're on search results page, populate search query in search box
	if ( url.indexOf('search.php') > 1 ) {
		var query_string,
			searched_for,
			parts,
			query,
			category,
			category_name;
		
			query_string = window.location.search;						//full query string
			searched_for = query_string.substring(3);					//don't need ?q=
			parts = searched_for.split('&');							//dont' need anything after the first &
			query = parts[0];											//query is first elem
			query = decodeURIComponent(query.replace(/\+/g, ' '));		//replace + with a space for multiple word searches
			query = query.replace(/^\s*/, '');							//remove leading space
			category = parts[1].replace('category=', '');				//category # for keeping persistent dropdown menu name
			category_name = $('[category=' + category + ']').text(); 	//category name extracted from category #
		
		$('#form_search input:text').val(query).css('color', '#000');
		$('#selectedsearch2').html( (category_name) ? category_name : 'All Categories' );
		
		
		//highlight search term
		//get the query from url
		var query = window.location.href,
			matches = query.match(/[?&]q=([^&]*)/);
		//console.log(matches);
		if (!matches) {
			return;
		}
		var terms = decodeURIComponent(matches[1].replace(/\+/g, ' ')),
			regex = new RegExp('(' + terms + ')', 'gi');
			
		//walk the *content* dom and replace matches
		$(".postage *").each(function() {
			if ($(this).children().size() > 0) {
				return;
			}
			
			var html = $(this).html(),
				newhtml = html.replace(regex, '<span class="highlight_word">$1</span>');

			$(this).html(newhtml);
		});
		
		$('span.highlight_word').livequery(function() {	
			$(this).addClass('qterm');
		});
		
	}
	
	
	//highlight current section in nav bar
	var current_header = $('div#content h2').html().toLowerCase();	
		
	switch(current_header) {
		case 'view all':
			$('div.main_nav').each(function() {
				$(this).find('ul li:first').addClass("current");
			});
			break;
		case 'accessibility':
			$('div.main_nav').each(function() {
				$(this).find('ul li:nth-child(2)').addClass("current");
			});
			break;
		case 'design':
			$('div.main_nav').each(function() {
				$(this).find('ul li:nth-child(3)').addClass("current");
			});
			break;
		case 'html/css':
			$('div.main_nav').each(function() {
				$(this).find('ul li:nth-child(4)').addClass("current");
			});
			break;
		case 'miscellaneous':
			$('div.main_nav').each(function() {
				$(this).find('ul li:nth-child(5)').addClass("current");
			});
			break;
		case 'programming':
			$('div.main_nav').each(function() {
				$(this).find('ul li:nth-child(6)').addClass("current");
			});
			break;
		case 'tech news':
			$('div.main_nav').each(function() {
				$(this).find('ul li:nth-child(7)').addClass("current");
			});
			break;
		case 'usability':
			$('div.main_nav').each(function() {
				$(this).find('ul li:nth-child(8)').addClass("current");
			});
			break;
	}
	
	
	//advanced search TODO look to redesign search to incorporate category search
	//this is quick/easy incorporation sucks
	$('#advanced_search_link').click(function() {
		if ($('#advanced_search_dropdown').is(":hidden")){
			$('#advanced_search_dropdown').css('visibility', 'visible');
		} else {
			$('#advanced_search_dropdown').css('visibility', 'hidden');
		}
		return false;
	});
	
});