/**
 * Include sIFR 
 */
document.write('<script src="/js/sifr/sifr.js" type="text/javascript"></script>');

/**
 * Load jQuery
 */
google.setOnLoadCallback(function(){

    _initMainNavHover();
    _initSeoFooter();
    _initSwapInput();
    _initH1FontReplacement();
    _initPngFix();
    //    _initTrial();
    _initWhiteTabs();
	_initExternalLinks();
});

function _initExternalLinks()
{
	$('a[rel*="external"]').click(function(){
		window.open(this.href);
		return false;
	});
}
function _initWhiteTabs()
{
    //Don't activate for tabs on collect, manage, share
    if ($('ul.nojs').length > 0)
		return;

    hash = location.hash.replace(/^.*#/, '');
    tabload(hash);

    $(".tabs li a").click(function(event){
	    hash = this.hash.replace(/^.*#/, '');
	    tabload(hash);
    });
}
function tabload(hash)
{
    
    $(".tab_content").hide(); //Hide all content
    if (hash.length < 1)
		hash = "";

    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    var delimiterIndex = hash.indexOf('-');
    
    if (hash.length > 0 && delimiterIndex != -1) {
	    hash = hash.substr(0,delimiterIndex);
	    var linkAnchor = hash + "-tab";
	    $("ul.tabs li a[href=#" + linkAnchor + "]").parent().addClass("active").show(); //Activate first tab
	    $("#"+hash).show(); //Show first tab content
	}
    else {
	    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
	    $(".tab_content:first").show(); //Show first tab content
	}
}

function _initPngFix()
{
    load_ifixpng();
    $('#regForm img').ifixpng();
    $('.herofreetrial h4').ifixpng();
	$('.certs img').ifixpng();
}
/**
 *  attach listeners to the footer
 */
function _initSeoFooter()
{
    var delimiter = '<!-- more !-->';
    var delimiterFound = false;
    if($("#seo-text").html().indexOf(delimiter) != -1)
    {
	var foundMore = $("#seo-text").html().split(delimiter);
	$("#seo-text").empty();
	$(foundMore[0]).each(function()
	{
	    $(this).addClass('teaser');
	    $("#seo-text").append($(this));
	});
	$(foundMore[1]).each(function()
	{
	    if (!$(this).hasClass('controls'))
		$(this).addClass('fulltext');

	    $("#seo-text").append($(this));
	});
    }
    else
	$("#seo-text").children().each(function()
	{
	    if ($(this).hasClass('controls'))
		return;
	    if (delimiterFound)
		$(this).addClass('fulltext');
	    else
	    if ($(this).html().indexOf(delimiter) != -1)
	    {
		delimiterFound = true;
		var foundMore = $(this).html().split(delimiter);
		$(this).html("<span class='teaser'>"+foundMore[0]+"</span><span class='fulltext'>"+foundMore[1]+"</span>");
	    }
	});
    $("#seo-text .readmore").click(function()
    {
	$(this).hide();
	$("#seo-text .collapse").slideDown();
	$("#seo-text .fulltext").slideDown();
	return false;
    });
    $("#seo-text .collapse").click(function()
    {
	$(this).hide();
	$("#seo-text .readmore").slideDown();
	$("#seo-text .fulltext").hide();
	return false;
    });
}

/**
 * Main nav hover (for IE6)
 */
function _initMainNavHover()
{
    if ($("#nav .active").length == 0)
    {
	$("#nav .home").addClass("homehover");
    }
	
    var hoverClass = 'lihover';
    $("#nav > li").hover(
	function(){
	    if (!$(this).hasClass("home")) {
		$(this).addClass(hoverClass);
		$("ul", this).fadeIn("fast");
		$("ul li",this).hover(
		    function()
		    {
			$(this).css("background","#FFF");
		    },
		    function()
		    {
			$(this).css("background","none");
		    }
		    );
	    }
	    else
	    {
		$(".home").addClass("homehover");
	    }
			    
	},
	function(){
	    if (!$(this).hasClass("home")) {
		$(this).removeClass(hoverClass);
		$("ul", this).fadeOut("fast");
	    }
	    else
	    {
		$(this).removeClass("homehover");
	    }
			
	}
	);
    
}
/**
 * Clears input fields with a default string
 */
function clearInput(id, string){
    if($(id).val() == string) {
	$(id).html('');
    }
    else if($(id).val() == ''){
	$(id).html(string);
    }
    return true;
}
/**
 * H1 Font Replacement
 */
function _initH1FontReplacement()
{
    if(typeof sIFR == "function"){
	sIFR.replaceElement(named({
	    sSelector:".sifrize",
	    sFlashSrc:"/js/sifr/fsalbert.swf",
	    sColor:"#365ebf",
	    sWmode: "transparent"
	}));
    };
}
/**
 * Search input
 */
function _initSwapInput()
{
    $('.swap').each(function() {
	var defaultval = this.value;
	$(this).focus(function() {
	    if(this.value == defaultval) {
		this.value = '';
	    }
	});
	$(this).blur(function() {
	    if(this.value == '') {
		this.value = defaultval;
	    }
	});
    });
}
/**
 * Trial Slider
 */
function _initTrial()
{
    if ($(".herofreetrial").length == 0)
	return;


    $(".herofreetrial").click(function(){
	trialSlideDown();trialclicked = true
    });
    $(".herofreetrial").hover(trialSlideDown,
	function()
	{
	    if (!trialclicked)
		$(".herofreetrial>div").animate({
		    top:"-186px"
		},400);
	});
}
function trialSlideDown()
{
    $(".herofreetrial>div").animate({
	top:"50px"
    },400);
    return false
}
var trialclicked = false;

/**
 * generic method for creating a carousel
 */
function createCarousel(section)
{
    if (!jQuery().jCarouselLite)
    {
	loadCarouselLite();
    }
	
    var panelCount = $(section + " .carousel li").length;
    var goArray = [];
    for (var i = 0; i < panelCount; i++)
    {
	goArray.push(section + " ."+i );
    }
    $(section + " .carousel").jCarouselLite({
	btnGo: goArray
    });
};