$(document).ready(function(){
	
	//focus clear fields
	$('.default').each(function(){
		$(this).css({'color' : '#666'});
		var _default = $(this)[0].defaultValue;//document.getElementById($(this).attr('id')).defaultValue;
		$(this).focus(function(){
			$(this).css({'color' : '#000'});
			if($(this).val() == _default){
				$(this).val('');	
			}
		});
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val(_default);	
				$(this).css({'color' : '#666'});
			}
		});						
	});
	//end of clear focus
	
	
	//textarea simple autoexpand
	$('textarea').each(function(){
		$(this).css({overflow: 'hidden'});
		$(this).each(function(){
			if($(this)[0].scrollHeight > $(this).height())	$(this).css({height: $(this)[0].scrollHeight + 50});
		});
		$(this).keyup(expandTextarea);
	});
	
	function expandTextarea(){
		if($(this)[0].scrollHeight > $(this).height()){
			$(this).animate({height: $(this)[0].scrollHeight + 50}, {duration:200, queue:false});
		}
	}
	//end of autoexpand
	

	//generic toggler
	$('.toggler').each(function(){
		$(this).next().hide();
		if($(this).hasClass('selected')){
			$(this).next().animate({height: 'toggle', opacity: 1}, 300);
		}
	});
	$('.toggler').click(function(){	
		var _h = parseInt($(this).next().css('height'));//height();
		if(_h > 0 || _h == NaN){
			$(this).removeClass('selected');
		}else{
			$(this).addClass('selected');
		}
		$(this).next().animate({height: 'toggle', opacity: 1}, 300);
		if($(this).children('a').text() == '[-]'){
			$(this).children('a').text('[+]');
		}else{
			$(this).children('a').text('[-]');
		}
		return false;
	});
	$('.toggler_close').click(function(){
		obj = $(this);
		while($(obj).prev('.toggler').length == 0){
			obj = $(obj).parent();	
		}
		$(obj).prev().trigger('click');
		return false;
	});
	//end of generic toggler
	
	
	//simple alert
	$(".alert").animate({opacity: 1}, 3000).animate({opacity: 0 }, {duration: 600, easing: 'easeInQuad'}).animate({height: 0, paddingTop: 0, paddingBottom: 0, borderWidth: 0, marginBottom: 0, marginTop: 0}, {duration: 500, easing: 'easeInQuad', complete: 
		function(){$(this).remove();}
	});
	//end of alert

	// added jquery functions
	String.prototype.rot13 = function rot13() {
		return this.toString().replace(/[a-zA-Z]/g, function(ch) {
			return String.fromCharCode((ch <= "Z" ? 90 : 122) >= (ch = ch.charCodeAt(0) + 13) ? ch : ch - 26);
		});
	}
	//end of addtions
	
	//decode rot13 emails - helps prevent bots from reading
	$('.email-rot13').each(function(){
		$(this).text($(this).text().rot13());	
		$(this).attr('href', "mailto:" + $(this).text());
	});
	//end of decode rot13
	
	$('.print').click(function(){
		window.print();
		return false;
	});
	
	$('#loc_map area').click(function(){
		$('#loc_addy .active').removeClass('active');
		$($(this).attr('href')).addClass('active');
	});
	
	
	/* ----------------------------------------- */
	
	/* 
	 * JQUERY CSS3 EMULATOR 
	 * author: 	Chris Gonzales, Provenance Digital Media
	 * desc:	allow browsers to use css3 selectors that don't support it, jquery css3 not actual css3
	*/
	
		function jqueryCSS3(file){
		// css file that will be parsed, to make parse and cache css on server, 
		// remove all lines, spaces (except font names), comments and 
		// remove all styles without selectors listed below
		// and use just the parseAsCss3()
		var css_doc = file;
		//chars to check for that exist in css3, mush slashify all chars and separated by |
		var css_selectors = '\[|\\]|\=|\"|\~|\^|\$|\*|\||\:|\(|\)|\>|\+';
		//exceptions to css that already work in most browsers
		var css_exceptions = ':hover|:active|:link|:visited';
		
		//ajax call to load and parse css
		$.ajax({
			url		: css_doc,
			cache	: true,
			success	: parseCleanCss
		});
		
		function parseCleanCss(data){
			
			var css_str = cleanCss(data);
			
			var css_ary = parseCss(css_str);
			
			applyCss(css_ary);
		}
		
		//apply all css to document
		function applyCss(ary){
			//kinda a hack to get ie6 to update display
			$('#container').attr('style', '');
			for(style in ary){
				//alert(ary[style][0] + " --- " + ary[style][1]);
				$(ary[style][0]).attr('style', ary[style][1]);
			}
		}
		
		//parse css function
		function parseCss(str, checklines){	
		
			//if true will skip checking and just push styles into array
			var checklines = checklines ? true : false;
			
			//array that will hold all styles to be applied
			var css_ary = []
			
			tmp_ary = str.split('}');
			
			for(line in tmp_ary){
				var css_line = tmp_ary[line].split('{');
				var selector = css_line[0];
				
				if(checklines == false){
					if( selector.search(RegExp('[' + css_selectors + ']+', 'g')) > -1 && selector.search(RegExp('(' + css_exceptions + ')', 'g')) == -1){
						css_ary.push( css_line );
					}
				}else{
					css_ary.push( css_line );
				}
			}
			
			//$('#debug').text(css_ary.join("\r\n"));		
			return css_ary;
		}
		
		//clean css function
		function cleanCss(str){
			var css_str = str;
			
			//remove comments
			css_str = css_str.replace(/\/\*[\s\S]+?\*\//g, '');
			
			//remove newlines
			css_str = css_str.replace(/[\n]*/g, '');		
			
			//remove duplicate spaces
			css_str = css_str.replace(/[\s]+/g, ' ');
	
			return css_str;
		}
	}
		
		if($.browser.msie)jqueryCSS3('/c/global.css');
	
	/* END OF EMULATOR */
	
	/* ----------------------------------------- */
});
