
function NetworkSlider(slider, content){
    var s = $('#'+slider);
    var c = $('#'+content);
    var m = c.attr("scrollWidth") - c.width();
    var handleSlide = function(e, ui) {
        clearInterval(si);
        c.attr({scrollLeft: ui.value * (m / 100)});
    };
    s.slider({ slide:handleSlide });

    var scrollCount = 0;
    var si = setInterval(function(){
        if( scrollCount < m )
            c.attr({scrollLeft: scrollCount++});
    }, 100);
}

function DivLayout(fixedDiv, fixedDivWidth, variableDiv, gutterWidth){
    var fd = $(fixedDiv);
    var fw = fixedDivWidth;
    var vd = $(variableDiv);
    var gw = gutterWidth;
    var setWidths = function(){
        fd.width(fw);
        vd.width( vd.parent().width() - (vd.outerWidth()-vd.width()) - fd.outerWidth() - gutterWidth );
    };
    setWidths();
    $(window).resize(setWidths);
}

function DivJustify(containerID, elClass, minMarginWidth){
    var c = containerID;
    var d = elClass;
    var minw = minMarginWidth != null ? minMarginWidth : 0;
    var dw = $(c + ' ' + d + ':first').outerWidth() + minw * 2;

    var setMargins = function(e){
        var numColumns = Math.floor( $(c).width() / dw );
        var mw = Math.floor(($(c).width() - (dw*numColumns) -1) / (2*numColumns));
        $(e).css({'margin-left':(mw+minw)+'px', 'margin-right':(mw+minw)+'px'});
    };

    setMargins( c + ' ' + d );
    $(window).resize(function(){ setMargins( c + ' ' + d ); });
}

function AdPopup(containerID) {
    var apow = $('#addPopup').outerWidth();
    var apoh = $('#addPopup').outerHeight();

    var hidePopup = function(){
        $('#addPopup').hide();
    };

    var showTimer = null;
    $(containerID).mouseenter(function(e){
        var e = this;
        showTimer = setTimeout(function(){
            var ww = $(window).width();
            var os = $(e).offset();
            var x = os.left + $(e).outerWidth() + 5;
            var y = os.top - 50;

            if( ww < x + apow + 5 )
                x = os.left - apow - 5;
            if( $(window).scrollTop() > y )
                y = $(window).scrollTop() + 5;
            else if( ($(window).scrollTop() + $(window).height()) < (y + apoh ) )
                y = $(window).scrollTop() + $(window).height() - apoh - 5;
            $('#addPopup').css({'top': y, 'left': x});
            $('#addPopup').fadeIn(500);
            $('#addPopup').html('<img src="/_images/ajax-loader.gif" style="margin:100px 50px" />');
            $('#addPopup').load('/go/ajax/horse/'+ $(e).find('input').val() +'/');
        }, 1000);
    });
    $(containerID).mouseleave(function(e){
        clearTimeout(showTimer);
        hidePopup();
    });
}

function LoadMainImage(el) {
    $('#mainImageImg').attr('src', el.src);
    $('#mainImageImg').css({top:0, left:0});
}

function searchAds(url)
{
    window.location.href = '/' + url + '/search/' + $('#searchBox').val().replace(/\s/gi, '-') + '/1/';
    return false;
}

function searchContext(inputEl, route)
{
    var searchBox = $('#searchContext_' + route);
    var position = $(inputEl).position();
    searchBox.css({ top:position.top+20, left:position.left });
    searchBox.html('<option>'+ inputEl.value +'</option>');
    searchBox.show();
}

function selectTab(tabID)
{
    $('.ulColumns', '#viewByCategories').hide();
    $(tabID).show();
}

function searchVideos(catId, adCatId)
{
    window.location.href = '/equine-videos/search/' + $('#searchBox', '#search_'+catId+'_'+adCatId).val().replace(/\s/gi, '-') + '/1/';
    return false;
}

function searchEvents()
{
    window.location.href = '/events/search/' + $('#searchBox', '#searchEvents').val().replace(/\s/gi, '-') + '/1/';
    return false;
}
