jQuery('[class^="dhlog."]').live('click', function(e) {
    var nsus = true;
    var dest = jQuery(this).attr('href');
    if ( dest == undefined )
        dest = jQuery(this).parents('form').attr('action');
    var cls = jQuery(this).attr('class').split(' ');
    for (c in cls) {
        if (cls[c].indexOf('dhlog.') > -1) {
            cls = cls[c];
            break;
        }
    }
    jQuery.ajax({
        async:  false,
        url: '/analytics/click/',
        data: {
            'data': cls,
            'x': e.pageX - jQuery('#body')[0].offsetLeft, 
            'y': e.pageY - jQuery('#body')[0].offsetTop, 
            'dest': dest
        },
        success:    function(result) {
            if (result == 1) {
                nsus = false;
                href = jQuery(e.target).parent('a').find('p.url').html();
                if (href.substring(0,7) != 'http://')
                    href = 'http://'+href;
                try {
                    var win = window.open(href, '_blank');
                    win.focus();
                    w = jQuery(win).width();
                } 
                catch(err) {
                    jQuery(e.target).parent('a').attr('href', href);
                    nsus = true;
                }
            }
        }
    });
    return nsus;
});
jQuery('.adcontainer').live('loadAds', function(e) {
    while ( jQuery(document).data('ads').length > 0 && jQuery(this).data('ads').length < jQuery(this).data('adcount') ) {
        jQuery(this).data('ads').push( jQuery(document).data('ads').shift() );
    }
    var ads = []
    if (jQuery(this).data('ads').length) {
        ads = ['<p class="sponsored">Sponsored results for ',
            jQuery(document).data('keywords'),
            '</p>'];
        jQuery.each(jQuery(this).data('ads'), function(idx,ad) {
            ads.push(['<div class="ads"><a class="', 
                ad[4], '" href="', 
                ad[1], '" rel="nofollow" target="_blank"><h6>',
                ad[2], '</h6><p class="desc">', 
                ad[3], '</p><p class="url">', 
                ad[0], '</p></a><div class="clear"></div></div>'].join(''));
        });
    }
    jQuery(this).html(ads.join(''));
    jQuery(this).unbind('loadAds');
});
jQuery('.adcontainer').live('init', function(e, adcount, keyhash) {
    jQuery(this).data('adcount', adcount);
    jQuery(this).data('ads', []);
    if ( jQuery(document).data('ads_status') == undefined ) {
        jQuery(document).data('ads_status','fetching');
        jQuery(document).data('ads_waitlist', []);
        //issue request for ads, load into data container
        jQuery.get('/adserve/'+keyhash, function(data) {
            if (data['related']) {
                related = ['<div class="headline">',
                	'<h2>Related searches</h2>',
                	'<div class="clear"></div></div>',
                    '<div class="list"><ul>'];
                for (i in data['related']) {
                    related.push(['<li><a href="/search?search__keyword=',
                        data['related'][i][1],
                        '">',
                        data['related'][i][0],
                        '</a></li>'].join(''))
                }
                related.push('</ul></div>');
                jQuery('#dhrelated').html(related.join(''));
            }
            jQuery(document).data('keywords', data['keywords']);
            jQuery(document).data('ads', data['ads']);
            jQuery(document).data('ads_status','finished');
            //handle pending containers
            jQuery.each(jQuery(document).data('ads_waitlist'), function(idx,obj) {
                obj.trigger('loadAds');
            });
        });
    }
    if ( jQuery(document).data('ads_status') == 'finished' ) {
        //fetch has already completed, grab the ads now
        jQuery(this).trigger('loadAds');
    } else if ( jQuery(document).data('ads_status') == 'fetching' ) {
        //fetch function hasn't completed, add to waitlist
        jQuery(document).data('ads_waitlist').push(jQuery(this));
    }
});

