/*
*
* scripts.js
*
* @author RMcGirr83 (www.rmcgirr83.org)
* @copyright (c) 2011 by Rich McGirr
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

var imageOpen = '../images/show.png';
var imageClose = '../images/hide.png';

$(function(){

	var source = removeSubdomain(location.hostname);
	$('a[href^="http://"]').filter(function() {return this.hostname && removeSubdomain(this.hostname) !== source;}).attr('target', '_blank');
	
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
		  var cacheImage = document.createElement('img');
		  cacheImage.src = arguments[i];
		  cache.push(cacheImage);
		}
	};
	$.preLoadImages(imageOpen, imageClose);
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$("div#quicklogin").hide();
	
	$("a.loginbox").click(function() {
		$("#quicklogin").fadeIn(100, function() {
			$('input#username').focus();
		});	
		// Remove the login icon
		$("li#loginout").delay(500).fadeOut(200);
		// Fade in the form
		return false; // stop browser from jumping when # link is clicked
	});
	
	$(window).scroll(function () { 
		$("#atBottom").show(); 
	});
	//clicking "to top" swings us to top instead of jumping
    $('a[href=#wrap]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow', 'swing');	
        return false;
    });
   $('.subforum_list').hide();
   $("span[id*='sf_']").each(function(){
      var sid = parseInt(this.id.replace("sf_", ""));
      $(this).show();
      if($('#f_'+sid).children().hasClass('unread'))
      {
         $(this).find('a:first').css('color', '#AA0000');
      }
   });
   $("span[id*='f_']").each(function(){
		if($(this).children().hasClass('unread'))
		{
			$(this).find('a:first').css('color', '#AA0000');
		}
	});
   // close forum
   $('[id*=forum]').each(function(){
      var forumid = $(this).attr('id');
      if ($.cookie(forumid) == 'yes' && $(this).is(':visible')) {
         $(this).slideToggle('fast');
         var fixid = $(this).attr('id').replace('forum','');
         $('a#forum_' + fixid).html('<img src="'+imageOpen+'" alt="Expand" />').attr('title','Expand');
      }      
    });
	$("a.tipTip, img.tipTip").tipTip({maxWidth:"300px"});
	//$('img').css('display', 'none').fadeIn(2000, 'swing');
	$('#g-plusone-sltr').attr('data-size', 'medium');
	$('#g-plusone-sltr').attr('data-count', 'false');
	$('#g-plusone-sltr').attr('data-href', 'http://www.rmcgirr83.org/');
	
	var po = document.createElement('script'); 
	po.type = 'text/javascript'; 
	po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);	
		
});
function ShowSubForums(id)
{
   var id = parseInt(id);
   $('#sf_'+id).fadeOut('fast', function(){
		$('#f_'+id).fadeIn('slow');
	});
}
function HideSubForums(id)
{
   var id = parseInt(id);
   $('#f_'+id).fadeOut('fast', function(){
		$('#sf_'+id).fadeIn('slow');
	});
}
toggle = function (id) {
   $('#forum' + id).slideToggle('fast', function () {
      if ($('#forum' + id).is(':visible')) {
         $.cookie('forum' + id, null);
         $('a#forum_' + id).html('<img src="'+imageClose+'" alt="Contract" />').attr('title','Contract');
      }
      if ($('#forum' + id).is(':hidden')) {
         $.cookie('forum' + id, 'yes');
         $('a#forum_' + id).html('<img src="'+imageOpen+'" alt="Expand" />').attr('title','Expand');
      }
   })
}

function removeSubdomain(hostname, keep) {
    return hostname.split(".").slice(-(keep || 2)).join(".")
}
