/*
**  Hovering over a sexcam shows popup link
*/
function init_hover() {
    $('.sexcam-preview-text').each(
        function() {
            $(this).fadeTo(0, 0.6);
        }
    );
    $('.sexcam-preview').hover(
    //  Hover
        function() {
            $(this).children('.sexcam-preview-text').fadeIn('fast');
        },
    //  Out
        function() {
            $(this).children('.sexcam-preview-text').fadeOut('slow');
        }
    );
}


jQuery(document).ready(function() {

    init_hover();

/*
**  Global vars
*/
    var filter_refresh_time     = 1200;
    var filter_refresh_timer    = null;

/*
**  Set new refresh timeout
**  and clear previous one
*/
    function filter_timer() {
        if (filter_refresh_timer!=null) {
            clearTimeout(filter_refresh_timer);
        }
        filter_refresh_timer = setTimeout(filter_refresh, filter_refresh_time);
    }
    
/*
**  Load newly filtered content
*/
    function filter_refresh() {
    //  Set timeout handle to null
        filter_refresh_timer = null;
    //  init query string
        var q = [];
        //  get search query if applicable
        try {
            var src = location.search.substr(1).split('&');
            for(var i=0;i<src.length;i++) {
                if (src[i].substr(0,2)=='q=') {
                //  found it!
                    q.push('q='+src[i].substr(2));
                    break;
                }
            }
        }
        catch(e) {}
        
        $.each(
            ['lang', 'figure', 'sex'],
            function() {
                $.each(
                    $('a.filter_'+this+'.active'),
                    function() {
                        q.push(
                            this.id.substr(0, this.id.lastIndexOf('_') ) + '[]=' +
                            this.id.substr( this.id.lastIndexOf('_')+1 ));
                    }
                );
            }
        );
    //  Update content
        $('#loading').show();
        $('#webcams').load(
            'ajax_webcams?' + q.join('&'),
            null,
            function(r, s, x) {
                init_hover();
                $('#loading').hide();
            }
        );
    //  Update paging
        $.get(
            'ajax_paging.php?' + q.join('&'),
            function(data) {
                $('span.paging').each(function() {
                    $(this).html(data);
                });
            }
        );
    }
    
/*
**  When clicking on a filter link, activate the refresh timer
*/
    $('a.filter_lang, a.filter_figure, a.filter_sex').click(function() {
    //  Set or remove class = active
        $(this).toggleClass('active');
    //  Set new timeout
        filter_timer();
        return false;
    });

/*
**  Prettify tables
*/
    $('#profile table tr:even').addClass('even');

/*
**	Payment window
*/
    $("a.payment").click(function(event){
        event.preventDefault();
        window.open(this.href,'webcamschatje_payment','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1');
        return false;
    });


});
