$(document).ready(function() {

/*
**  Bookmark
*/
    var bmurl = 'http://www.webcammmm.com/';
    var bmttl = 'Webcammm: Live cams';
    function addFav2() {
      if(!bmurl)bmur
      if(!bmttl)bmtt
      if((typeof window.sidebar == 'object')&&(typeof window.sidebar.addPanel == 'function')){
    		window.sidebar.addPanel(bmttl, bmurl,'');
    	} else if (typeof window.external == 'object'){
    		window.external.AddFavorite(bmurl, bmttl);
      } else if (window.opera && document.createElement){
        var a=document.createElement('A');
        if(!a){
    			return false;
    		}
        a.setAttribute('rel','sidebar');
        a.setAttribute('href',bmurl);
        a.setAttribute('title',bmttl);
        a.click();
      } else {
    		return false;
    	}
      return true;
    }

    $('#bookmark').click(
        function() {
        	if (addFav2()==false) {
        		alert('Close this message and press CTRL-D\r\nto add this site to your favorites.');
        	}
    	}
    );
    
/*
**  Fade out non-selected languages
*/
    $('a.live_cam_filter_img').fadeTo(1000, 0.4);
    
    
/*
**	Payment window
*/
  $("a.payment").click(function(event){
    event.preventDefault();
    window.open(this.href,'webcammm_payment','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1, scrollbars=1');
    return false;
  });

/*
**	Demo window
*/
  $("a.demo").click(function(event){
    event.preventDefault();
    window.open(this.href,'webcammm_demo','width=800,height=640,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1, scrollbars=1');
    return false;
  });

/*
**  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) {}
        
        
    //  Gather languages
        $('#language a.live_cam_filter_img_selected').each(function(){
            q.push(
                'filter_lang[]=' +
                this.id.substr( this.id.lastIndexOf('_')+1 ));
        });
    //  Gather sex
        $('img.filter_sex_selected').each(function(){
            q.push(
                'filter_sex[]=' +
                this.id.substr( this.id.lastIndexOf('_')+1 ));
        });
    //  Gather breasts
        $('#cup_size a.live_cam_filter_selected').each(function(){
            q.push(
                'filter_cup[]=' +
                this.id.substr( this.id.lastIndexOf('_')+1 ));
        });
    //  Gather figure
        $('#figure a.live_cam_filter_selected').each(function(){
            q.push(
                'filter_figure[]=' +
                this.id.substr( this.id.lastIndexOf('_')+1 ));
        });
        
    //  Update content
        $('#loading').show();
        
        $('#livecams').load(
            '/ajax_webcams.php?' + q.join('&'),
            null,
            function(r, s, x) {
                //$('#loading').hide();
                $('#loading').hide();
            }
        );
    //  Update paging
        $('#paging').load('/ajax_paging.php?' + q.join('&'));
        
    }




/*
**	Language filter
*/
    $('#language a.live_cam_filter_img_selected, #language a.live_cam_filter_img').click(function(){
        var bSel = $(this).hasClass('live_cam_filter_img_selected');
        if (bSel) {
            $(this).removeClass('live_cam_filter_img_selected').addClass('live_cam_filter_img').fadeTo(200, 0.4);
        }
        else {
            $(this).removeClass('live_cam_filter_img').addClass('live_cam_filter_img_selected').fadeTo(200, 1.0);
        }
    //  Set new timeout
        filter_timer();
        return false;
    });

/*
**  Sex filter
*/
    $('img.filter_sex_selected, img.filter_sex').click(function(){
        var bSel = $(this).hasClass('filter_sex_selected');
        var sSrc = $(this).attr('src');
        var lPos = sSrc.lastIndexOf('.') - 1;
        
        if (bSel) {
            $(this).removeClass('filter_sex_selected').addClass('filter_sex');
            sSrc = sSrc.substr(0, lPos) + '0' + sSrc.substr(lPos+1);
            $(this).attr('src', sSrc);
        }
        else {
            $(this).removeClass('filter_sex').addClass('filter_sex_selected');
            sSrc = sSrc.substr(0, lPos) + '1' + sSrc.substr(lPos+1);
            $(this).attr('src', sSrc);
        }
    //  Set new timeout
        filter_timer();
        return false;
    });

/*
**  Breast + Body filter
*/
    $('a.live_cam_filter_selected, a.live_cam_filter').click(function(){
        var bSel = $(this).hasClass('live_cam_filter_selected');
        if (bSel) {
            $(this).removeClass('live_cam_filter_selected').addClass('live_cam_filter');
        }
        else {
            $(this).removeClass('live_cam_filter').addClass('live_cam_filter_selected');
        }
    //  Set new timeout
        filter_timer();
        return false;
    });

/*
**  Cover flow
*/
    var flow_images  = Array();
    $('#flow-inner div').each(function(){
        flow_images.push($(this));
    });
    var flow_num_img = flow_images.length;
    var flow_cur_img = 1;
    var flow_nxt_img = null;
    var flow_actions = Array();
    var flow_running = false;
    var flow_anm_tim = 1000;
    if (flow_num_img>0) {
        flow_images[0].css('left', 0).css('opacity', 1);
    }
    if (flow_num_img==1) {
        $('#flow-next').hide();
        $('#flow-prev').hide();
    }
    function flow_start() {
        if (flow_running) return;
        flow_running = true;
        var action = flow_actions.shift();
        var cur_img = flow_cur_img;
        var nxt_img = cur_img + action;
        if (nxt_img > flow_num_img) nxt_img = 1;
        if (nxt_img < 1) nxt_img = flow_num_img;
        if (action==1) {
        //  next (move new in from right)
            flow_images[nxt_img - 1].css('left', 400);
            var move_cur_to = -400;
        }
        else {
        //  prev (move new in from left)
            flow_images[nxt_img - 1].css('left', -400);
            var move_cur_to = 400;
        }
        flow_images[nxt_img - 1].animate(
            {
                'left': 0,
                'opacity': 1,
                'queue': true,
            },
            flow_anm_tim,
            'swing'
        );
        flow_images[cur_img - 1].animate(
            {
                'left': move_cur_to,
                'opacity': 0,
                'queue': true,
            },
            flow_anm_tim,
            'swing',
            function() {
                flow_cur_img = nxt_img;
                flow_running = false;
                if (flow_actions.length) {
                    flow_start();
                }
            }
        );
    }
    $('#flow-prev').click(function(){
        flow_actions.push(-1);
        flow_start();
        return false;
    });
    $('#flow-next').click(function(){
        flow_actions.push(1);
        flow_start();
        return false;
    });
    
    
});