$( document ).ready( function() {    

    $(".hidethis").hide();
    
    $('#modal').jqm({
        ajax:        '@href',
        modal:       true,
        trigger:     $('.jqm-trigger'),
        closeClass:  'jqm-close'
    });
    
    $('a[rel="prettyPhoto"]').prettyPhoto({ theme: 'facebook' });
        
    // show javascript only features
    $('.js-only').show();
    
    
    $('.trigger').click(function(){
        var target = $(this).attr("href")
        $(target).show();
        return false;
	});
    
    
    // disable typekit from windows for fonts under 20px
    var os      = $.client.os;
    var browser = $.client.browser;        
    
    if ( os == "Windows" && browser =="Chrome" || browser =="Explorer" ) {
        $("h1, h2, h3, h4, h5, h6, a, a *, td, th, .search-report").not("#blog h2 a").each(function(){
                        
            var fontSize = parseInt( $(this).css("font-size") );
            
            if ( fontSize < 22  ) {
                $(this).css("font-family","'helvetica neue', arial");
                //$(this).css("color","red");
            } else {
                //$(this).css("color","blue");
            }    
        });
    }
    
    
    
    // blog rating widget
    $(".rating-ratio").hide();

    $('.rating-widget').hover(function() {
        $(this).find(".rating-ratio").fadeIn(110);
    }, function() {
        $(this).find(".rating-ratio").fadeOut(110);
        //$(this).find(".rating-ratio").delay(300).fadeOut(200);
    });
    
    $(".rating-widget .bottom a").not(".logIn_false *").click(function(){
        $(this).parents(".bottom").find("a").removeClass("selected");
        $(this).addClass("selected");
        return false;
	});
    
    // tooltips
    $(".mt-trigger").click(function(){
        var theTarget = $(this).attr("href");
        $( theTarget ).show();
        return false;
    });
    
    
    $(".tt-close").click(function(){
        $(this).parents(".modal-tip").hide();
        return false;
    });
    
    
    // toggle links
    $(".replace-link").click(function(){
        
        var displayItem = $(this).attr("href"); 
        $(this).hide();
        $(displayItem).fadeIn("10");                   
        return false;
    });
    
    // Utility nav "hot zones"
    $('.block-link').each(blockLink);

    // move modal markup from original
    // location to just inside body
    $('.modal').prependTo($('body'));        

    
    
    // Auto-clear pre-populated search fields
    $('.mini-search .mini-search-field, .jumbo-search .jumbo-search-field').autoclear();
        
    
    $('.expandableList').expandableList();
    
    $('.tabs').tabNavigation();
    
    $('.panels').panelNavigation();
    
    $('.tooltip').toolTips();
    
    $('.placehold').each(placeholder);
    
    $("a[rel='external']").externalLinks();
    
        
    $(".conditional-item input").click(function(){
        $(this).parents(".element").next().hide();
        $(this).parents(".element").next().next().fadeIn();
    });
    
    
    $('#products-overview a.sh-trigger').click(function(event){
        event.preventDefault();
        $(this).parents('.ag-item').find('.sh-content').toggle();
    });
    
    
    // should be made into a plugin
    var registrationForm = $('.registration-process, .checkout-process');
    var labels           = $('label', registrationForm);
    var fields = [];

    labels.each(function(){
        var label        = $(this);
        var labelFor     = label.attr("for");
        var defaultValue = label.attr("title");

        // if the label does not have a for attribute or does not have a default value
        if (!labelFor || !defaultValue) {
            return;
        }

        var field = $(".field[id='" + labelFor + "']", registrationForm)
            .filter('input[type=text], textarea');
            
        // if an input field by that name does not exist
        if (field.length == 0) {
            return;
        }
        
        field.removeClass('inactive');
        if (!field.hasClass('active')) {
            field.addClass('active');
        }

        field.focus(function () {
            if ($(this).hasClass('active')) {
                return;
            }

            if ($(this).val() == defaultValue) {
                $(this).removeClass('inactive');
                $(this).addClass('active');
                $(this).val('');
            }
        });

        field.blur(function () {
            if ($(this).val() == '') {
                $(this).removeClass('active');
                $(this).val(defaultValue);
                $(this).addClass('inactive');
                return;
            }
            
            $(this).addClass('active');
        });
        field.blur();
        
        field.bind('clear', function () {
            if ($(this).hasClass('inactive')) {
                $(this).val('');
            } 
        });

        fields.push(field);
    });
    
    $('form', registrationForm).submit(function(){
        for(x in fields) {
            fields[x].trigger('clear');
        }
    });



    // credentials tooltip hovers
    $(".ag-item .ag-item-credentials li").each(function () {
        var hoverTip = $(this).attr("title");
        if (hoverTip != "") {
            $(this).append('<div class="tip">'+hoverTip+'</div>').addClass("hoverTip").removeAttr('title');
        }
    });
    
    $(".ag-item .ag-item-credentials li, .ag-item .premium").hover( 
        function () { 
            $(this).children(".tip").fadeIn("fast");
        }, 
        function () {  
            $(this).children(".tip").fadeOut("fast");
        }
    );
    
    
    // aggregator sorting controls
    $('.ag-sort h3 a').toggle(function() {
    	$(this).parents(".ag-sort-container").children("ul").fadeIn("fast");
    }, function() {
    	$(this).parents(".ag-sort-container").children("ul").fadeOut("fast");
    });
    
    $(".ag-sort-container ul a").click(function(){
        $(this).parents(".ag-sort-container").children("ul").fadeOut("fast");
        var sortBy = $(this).html();
        $(this).parents(".ag-sort-container").find("h3 a").html(sortBy).click();
        
    });
    

    // Jumbo Search advanced options drawer
    $(".jumbo-search .search-meta").toggle(function() {
    	$(this).parents(".jumbo-search").children(".advanced-search-options").slideDown("fast");
    	$(this).addClass("search-meta-expanded");
    }, function() {
    	$(this).parents(".jumbo-search").children(".advanced-search-options").slideUp("fast");
    	$(this).removeClass("search-meta-expanded");
    });
    
    
    
    // help topics show/hide
    //.show-extra-links
    //.extra-links

    $(".show-extra-links").toggle(function(){
        $(this).html("Hide").parents("ul").children(".extra-links").show("fast");
        return false;
    },function(){
        $(this).html("View All").parents("ul").children(".extra-links").hide("fast");
        return false;
    });
    
    
    
    
    
    $('div.readmore').each(contentExpander);
    
    
    
    // overlay on blog
    $(".bg-grid .bg-overlay").hide();
    
    
	/*	removed this because it felt "broken"
	
	$(".bg-grid .bg-entry").hoverIntent({
		over: showSummary, 
		timeout: 0, 
		out: hideSummary
	});*/
	
	$('.bg-grid .bg-entry').hover(function() {
		$(this).find(".bg-overlay").show();
	}, function() {
		$(this).find(".bg-overlay").hide();
	});
	
	
	// blog click actions
	$(".bg-grid .bg-entry,.bg-grid .bg-entry *").css('cursor', 'pointer').click(function(){
        var url = $(this).find('a').attr('href');
        window.location = url;
    });	
    
    
    // channel selector
    $(".channel-selector h4").click(function(){
        $(this).parents(".channel-selector").children("ul").fadeIn("fast");            
    	});
    
    
    // selects that have urls in value
    $('.url-menu').change(function(event){ location.href = $(this).val(); });
    
	// disable submit buttons when blog article has been submitted 
	$('.disabled').click(function(e) {
	    e.preventDefault();
	    
	});
});

// functions for overlay summary on blog
function showSummary(){ $(this).children(".bg-overlay").fadeIn("fast");  }
function hideSummary(){ $(this).children(".bg-overlay").hide(); }    




function contentExpander(index){
	var content = $(this).find('p:first').html();
	var shortContent = content.substr(0,250) + '...';
	
	$(this).append('<p class="show-hide-links"><a href="javascript:;" class="trigger">Show full bio</a><p>');
	$(this).find('p:first').html(shortContent);
	
	$(this).find('.trigger').toggle(
	    function(){
	        $(this).text('Hide full bio').parents('.readmore').find('p:first').html(content);
	    },
	    function(){
	        $(this).text('Show full bio').parents('.readmore').find('p:first').html(shortContent);
	    }
	);
};

function placeholder(){
    var p = $(this).attr('placeholder');
    
    $(this).val(p).addClass('ph-on');
    $(this).focus(function(){
        if($(this).val() == p){
            $(this).val('').removeClass('ph-on');
        };
    }).blur(function(){
        if($(this).val() == ''){
            $(this).val(p).addClass('ph-on');
        };
    });
};


function blockLink(){
    var block = $(this);
    var trigger = $('a.block-link-src', block);
    var title = trigger.text();
    var url = trigger.attr('href');
    
    block.css('cursor', 'pointer').attr('title', title).click(function(){
        window.location = url;
    }).hover(function(){
        trigger.hover();
    });
};
