$(document).ready(function() {
		
	/*
	 * External link handling
	 */
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});
	
	$('#email_run_better').click(function() {
		var add1 = 'mailt';
		var add2 = 'o:runbetter@x';
		var add3 = 'iotech.c';
		var add4 = 'om';
		$(this).attr('href', add1 + add2 + add3 + add4);
	});
	
	/**
	 * http://www.sohtanaka.com/web-design/simple-tabs-w-css-jquery/
	 */
		var hash = window.location.hash;
		$(".tab_content").hide(); //Hide all content
		if(hash == '') {
			$("ul.tabs li:first").addClass("active").show(); //Activate first tab
			$(".tab_content:first").show(); //Show first tab content
		} else {
			var possible_tabs = $('.tab_container .tab_content');
			var found = false;
			$(possible_tabs).each(function() {
				// loop thru all possible tabs and see if we can find our tab variable from the query string
				if(hash.search($(this).attr('id')) > 0) {
					found = true;
				}
			});
			// only do something if we have a valid tab.
			if(found) {
				$(hash).show();
				$(hash + '_link').addClass("active").show();
				scrollTo(0,0);	
			} else {
				$("ul.tabs li:first").addClass("active").show(); //Activate first tab
				$(".tab_content:first").show(); //Show first tab content
			}
		}
				
		//On Click Event
		$("ul.tabs li").click(function() {

			$("ul.tabs li").removeClass("active"); //Remove any "active" class
			$(this).addClass("active"); //Add "active" class to selected tab
			$(".tab_content").hide(); //Hide all tab content

			var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
			window.location.hash = activeTab;

			$(activeTab).fadeIn(); //Fade in the active ID content
			
			load_sidebar();
			
			return false;
		});
		
		
		/**
		 * load the sidebar
		 */
		function load_sidebar() {
			// grab the active tab text and replace spaces. this is going to be the name of the file we are going to include.
			var site = $('#site_id').text();
			var file = $(".tabs li.active").text().toLowerCase().replace(' ', '_');
			if($('#related_wrap').length > 0) {
				$('#related_wrap').load(site + 'inc/sidebar/' + file + '.php', function(response, status, xhr) {
					if (status == "error") {
						// don't load anything if error
					    $("#related_wrap").html(' ');
					  }
				});
			}
		}
		
		load_sidebar();
		
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
		
		$("#download_drop_link").hover(function(){
			$("#download_drop_link ul").css("margin-left", "0");
		},
		function() {
			$("#download_drop_link ul").css("margin-left", "-9999px");
		});
		
		
		
		/**
		 * Chick tabs
		 */
			$('.tab_box_content').hide();
			$('#tab_top div').fadeTo('fast','.5');
			$('.tab_box_content:first').show();
			$('#tab_top div:first').addClass('selected');
			$('#tab_top div:first').fadeTo('slow', '1');
			
			$('#tab_top .img_wrap').click(function() {
				if($(this).hasClass('selected')) {
					
				} else {
					$('#tab_top .img_wrap').removeClass('selected');
					$(this).addClass('selected');
					$('.tab_box_content').hide();
					$('#tab_top div').fadeTo('fast', '.5');
					// get name of tab then display content
					var x = $(this).attr('id').substr(0, $(this).attr('id').indexOf('_'));
					$('#' + x + '_tab').show();
					$(this).fadeTo('fast', '1');
					$(this).css('borderBottom', 'none');
				}
			});
				
			
	
});



function tco_popup(url) {
	newwindow=window.open(url,'name','height=700,width=900');
	if (window.focus) {newwindow.focus()}
	return false;
}

