$(document).ready(function() {
    if ( typeof currentpage != "undefined" ) {
        if (currentpage == 'cam_list' || currentpage == 'freechat') {
            var cljs = new camlistjs();
            cljs.getActualModelStatus();
            if (currentpage == 'cam_list') {
                //cljs.processSelector();
            }
        }
    }
});

camlistjs = function() {
    var parent = this;
    var haltAllActions = false;

    this.processSelector = function() {
        if (pageProcessRefresh == false) {
            parent.thumbOverlay();
        } else {
            parent.getActualModelThumbnailBlock();
            parent.thumbOverlay();
        }
    }

    this.getActualModelStatus = function() {
        if ( typeof actualModelStatus != "undefined" ) clearTimeout(actualModelStatus);
        $.ajax({
            type: 'POST',
            url: 'api/index.php',
            data: { ctrl: 'actual_hardcore', type: 'banner', cc: cc },
            dataType: 'html',
            success: function(data) {
                $('#mod_live').html(data);
                actualModelStatus = setTimeout(function(){ parent.getActualModelStatus(); }, 10000);
            },
            complete: function() {
                //delete data;
            }
        });
    }

    this.thumbOverlay = function() {
        if (haltAllActions == false) {
            $('.camlistthumbnailoverlay.camlistcaption').hoverIntent(function(i) {
                parentBlock = this.id.split('-')[1];
                currentPageStatus = parent.currentBlockStatus(parentBlock);
                if (currentPageStatus != '-1') {
                    parent.getOverlayOnModelThumbnailBlock(this.id, $('#' + parentBlock + ' input.camid-camsource').val());
                }
                //parentBlock = null;
                currentPageStatus = null;
            },
            function() {
                $('#box' + this.id).attr('class', 'cover');
                $('#box' + this.id).unbind('mouseenter mouseleave');
            });
            $('#' + this.id).mouseout(function() {
              $('#box' + this.id).attr('class', 'cover');
            });
        }
    }

    this.processIncludes = function() {
        $('#freechatpagination .loginimagethumb').hoverIntent(function(i, j) {
            parentBlock = $(this).parent().attr('id');
            thisid = 'overlay-' + parentBlock;
            values = $('#' + parentBlock + ' input.camid-camsource').val();
            camidVal = values.split('-')[0];
            camsourceVal = values.split('-')[1];

            $.ajax({
                type: 'POST',
                url: 'index.php?ctrl=cam_list&action=post',
                data: { camid: camidVal, camsource: camsourceVal },
                dataType: 'data',
                beforeSend: function(data) {
                },
                success: function(data) {
                    $('#box' + thisid).attr('class', 'cover camlistboxcaption');
                    $('#' + parentBlock + ' div').eq(9).attr('class', 'nameflag' + data);

                    if (data == '2') {
                        $('#box' + thisid + ' .camlistlive_mc').attr('style', 'margin-top:45px;');
                        $('#box' + thisid + ' .camlistprofile_mc').attr('style', 'display:none;');
                        $('#box' + thisid + ' .camlistlive_mc').click(function(){
                            window.location='index.php?ctrl=hardcorechat&size=1&type='+camsourceVal+'&id='+camidVal;
                        });
                    } else {
                        haltAllActions = true;
                        msgbox('msgExclamation', 'msgOkCancel', 'Page out of sync', 'Your page is out of sync. Click OK to return to Livechat. ', 'index.php?ctrl=cam_list');
                    }
                },
                complete: function() {

                },
                error: function(xhr, status, e) {
                }
            });

        },
        function() {
            $('#box' + thisid).attr('class', 'cover');
            $('#box' + thisid).unbind('mouseenter mouseleave');
        });
        $('#' + this.id).mouseout(function() {
          $('#box' + this.id).attr('class', 'cover');
        });
    }

    this.getOverlayOnModelThumbnailBlock = function(thisid, values) {
        camidVal = values.split('-')[0];
        camsourceVal = values.split('-')[1];
        parentBlock = thisid.split('-')[1];
        $.ajax({
            type: 'POST',
            url: 'index.php?ctrl=cam_list&action=post',
            data: { camid: camidVal, camsource: camsourceVal },
            dataType: 'data',
            beforeSend: function(data) {
            },
            success: function(data) {
                $('#box' + thisid).attr('class', 'cover camlistboxcaption');
                currentPageStatus = parent.currentBlockStatus(parentBlock);
                $('#' + parentBlock + ' div').eq(9).attr('class', 'nameflag' + data);
                if (data == '0' && currentPageStatus == '0') {
                    $('#box' + thisid + ' .camlistprofile_mc').click(function(){
                        window.location='index.php?ctrl=freechat&size=1&type='+camsourceVal+'&id='+camidVal;
                    });
                    $('#box' + thisid + ' .camlistlive_mc').click(function(){
                        window.location='index.php?ctrl=hardcorechat&size=1&type='+camsourceVal+'&id='+camidVal;
                    });
                } else if (data == '2' && currentPageStatus == '2') {
                    $('#box' + thisid + ' .camlistlive_mc').attr('style', 'margin-top:45px;');
                    $('#box' + thisid + ' .camlistprofile_mc').attr('style', 'display:none;');
                    $('#box' + thisid + ' .camlistlive_mc').click(function(){
                        window.location='index.php?ctrl=hardcorechat&size=1&type='+camsourceVal+'&id='+camidVal;
                    });
                } else {
                    msgbox('msgExclamation', 'msgOkOnly', 'Page out of sync', 'Your page is out of sync. Click OK to refresh. ', 'index.php?ctrl=cam_list');
                }
            },
            complete: function() {
                currentPageStatus = null;
            },
            error: function(xhr, status, e) {
            }
        });
    }

    this.getActualModelThumbnailBlock = function() {
        if ( typeof actualModelThumbnailBlock != "undefined" ) clearTimeout(actualModelThumbnailBlock);
        actualModelThumbnailBlock = setTimeout(function() {
        $.ajax({
            type: 'GET',
            url: 'index.php?ctrl=cam_list&action=get',
            processData: true,
            data: {},
            dataType: 'text',
            cache: false,
            success: function(json) {
                parent.writeActualModelThumbnailBlock(json);
            },
            complete: function() {
                if (pageProcessRefresh == false && currentpage != 'freechat') {
                    parent.writeActualModelThumbnailCount();
                }
                delete json;
            },
            error: function(xhr, status, e) {
            }
        });

        parent.getActualModelThumbnailBlock();
        }, 30000);
    }

    this.writeActualModelThumbnailBlock = function(json) {
        var json = JSON.parse(json);
        $.each(json, function(i) {
            (i <= 11)  ? blockDiv = '#topdiv'+ i : blockDiv = '#bottomdiv'+ (i - 12);
            if ( $(blockDiv + ' input').val() != json[i].nickname + json[i].status )  {
                $(blockDiv + ' .modelthumbnail input').val(json[i].nickname + json[i].status);
                $(blockDiv + ' .camid-camsource').val(json[i].cam_id + '-' + json[i].cam_source);
                $(blockDiv + ' .modelname').html(json[i].nickname).parent().attr('class', 'nameflag'+json[i].status);

                if (json[i].status == '-1')
                {
                    $(blockDiv + ' .model-offline').show();
                    $(blockDiv + ' .cover .camlistboxcaption').hide();
                }
                else
                {
                    if (json[i].status == '0') {
                        $(blockDiv + ' .chat').click(function(){
                            window.location='index.php?ctrl=freechat&size=1&type='+json[i].cam_source+'&id='+json[i].cam_id;
                        });
                    } else {
                        $(blockDiv + ' .chat').click(function(){
                            window.location='index.php?ctrl=hardcorechat&size=1&type='+json[i].cam_source+'&id='+json[i].cam_id;
                        });
                    }
                    $(blockDiv + ' .model-offline').hide();
                    $(blockDiv + ' .cover .camlistboxcaption').show();
                }

                var flags = json[i].attributes.langs.split(',');
                var flagHtml = '';
                for (ctr = 0; ctr < flags.length; ctr++)
                {
                    flagHtml = flagHtml + '<img src="images/flag_'+flags[ctr]+'.gif" />';
                }

                $(blockDiv + ' .modelflag').html(flagHtml);
                var imagePreview = (json[i].preview == 'images/noimage.jpg') ? 'http://img.6-chats.com/gallery/no_picture_available.jpg' : json[i].preview;
                $(blockDiv + ' img .thumbnail').attr('src', imagePreview);

                $(blockDiv + ' .model-price').html(json[i].attributes.price_category+' &euro;');
                $(blockDiv + ' .info').click(function(){
                    showInfo(json[i].cam_id,json[i].cam_source);
                });
                $(blockDiv + ' .info .basic').attr('rel', 'index.php?ctrl=tooltip&id='+json[i].cam_id+'&type='+json[i].cam_source);
            }
        });
        recallTooltip();
        parent.writeActualModelThumbnailCount();
    }

    this.writeActualModelThumbnailCount = function() {
        fcTotal = 0 + $("#search_placeholder .nameflag0").length;
        $('#freechat_total').html(fcTotal.toString());
        hcTotal = 0 + $("#search_placeholder .nameflag2").length;
        allOnlineCams = fcTotal + hcTotal;
        string = $('.hardcoretitleholder h2').html().split('(')[0];
        msg = string.replace(/\s+$/,"");
        $('.hardcoretitleholder h2').html(msg+' ( '+ allOnlineCams.toString() +' )');
    }

    this.currentBlockStatus = function(parentBlock) {
        currentPageStatus = $('#'+parentBlock).attr('class').split('-')[1];
        if (currentPageStatus == 'status0') {
            return '0';
        } else if (currentPageStatus == 'status2') {
            return '2';
        } else {
            return '-1';
        }
    }
}
