/*
 * hrefID jQuery extention
 */
$.fn.extend({ hrefId: function() { return $(this).attr('href').substr($(this).attr('href').indexOf('#')); } });

/*
 * Scripts
 *
 */
jQuery(function($) {

 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		forms : {
			labels : function(){
				var $elements = $('form.newsletter-a p input, form#top-search p input, #free-phone-box form p input');
				$elements.each(function(){
					if($(this).val() !== '') $(this).prevAll('label:first').hide();
				}).focus(function(){
					$(this).prevAll('label:first').hide();
				}).blur(function(){
					if($(this).val() === '') $(this).prevAll('label:first').show();
				});
			}
		},
		fixes : {			
			firstChildElements : function(){				
				var $elements = $('#nav li:first, #nav-foot li:first, #nav-side li:first, div.comments-a div.comment:first');
				$elements.each(function(){
					$(this).addClass('first');
				});
			},
			blog : function(){
				// no comments/trackbacks + alternative
				var $comments = $('div.comments-a');
				$comments.each(function(){
					if($(this).find('div.comment').length == 0){
						var fixed = $(this).html().replace(/<\/h2>/i,'</h2><p class="empty">') + '</p>';
						$(this).html(fixed);
					} else {
						$(this).find('div.comment:odd').addClass('alt');
					}
				});
				
				// show/hide comments/trackbacks
				var $links = $('div.post-a p.info a.comments, div.post-a p.info a.trackbacks');
				$links.click(function(){
					$($(this).hrefId()).toggle();
					if($(this).is('.comments')) $($(this).hrefId()).next('div.add-comment-a:first').toggle();
					return false;
				});
				
				// single post (show trackbacks and comments)
				if($('div.post-a').length == 1){
					$('div.comments-a, div.add-comment-a').show();
				}
			},
			// fades in intro text :)
			intro_fade : function(){
				$("#intro p").hide();
				$("#intro p").fadeIn(2000);
			},
			// spits out declared var of page name in header image
			dynamicPageName : function(){
				//alert(pageName);
				
				if(pageName){
					$("#pageNameHere").text(pageName);
					}
				
				if(pageName == ''){
					$("#pageNameHere").html("technology solutions. <span style='color:#fec100;'>guaranteed</span>");
					//$("#pageNameHere").text('technology solutions. guaranteed');
					}
				
			
			},
			separators : function(){
				$('ul[class*=cols]').each(function(){
					var matches = /cols([0-9]+)/i.exec($(this).attr('class'))
					var row = matches[1];
					$(this).find('li').each(function(i){
						if((i+1) % row == 0) $(this).after('<li class="separate"><a href="#top">Back to top</a></li>');
					});
				});
			}
		}		
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.forms.labels();
	Engine.fixes.blog();
	Engine.fixes.separators();
	Engine.fixes.firstChildElements();	
	Engine.fixes.intro_fade();
	Engine.fixes.dynamicPageName();
});


