/*
    Copyright (c) 2009 18girlsex.com / pornographicfilth.com

    All rights reserved.

    Do not copy any part of this Javascript file. It is our intellectual property
    and any theft will be prosecuted to the maximum extent of the law.

    I am available for design and programming jobs. If you like what you see and
    want me to help you with your ideas, please contact me via email or see GFY.com
    where my nick is pornographicfilth
*/

var tick = 0;
var pause = false;

function putImage(i, x, y, json) {
    var behind = $('<div class="behind" id="behind' + i + '" />');
    var mask = $('<div class="mask" id="mask' + i + '" />');
    var img = $('<img src="' + json.preview_url + '" />');
    behind.append(img);
    var overlay = $('<img src="overlay.png" /><div class="cap">' + json.desc + '</div>');
    var link = $('<a href="' + json.gallery_url + '" />');
    link.append(overlay);
    mask.append(link);
    if(false && json.desc != undefined) {
        mask.tooltip({tip: '#tip', effect: 'slide', lazy: true, opacity: 0.95,
            offset: [0, -45], position: 'center right',
            onBeforeShow: function() {
                pause = true;
                $('#tip').html(json.desc);
                return true;
            },
            onHide: function() {
                pause = false;
            }
        });
    }
    if(json.desc != undefined) {
        img.attr('alt', json.desc);
        img.attr('title', json.desc);
        overlay.attr('alt', json.desc);
        overlay.attr('title', json.desc);

    }
    behind.css('left', x);
    behind.css('top', y);
    mask.css('left', (x - 10));
    mask.css('top', (y - 10));
    $(document.body).append(behind);
    $(document.body).append(mask);
}

function arrange(num, json, offset, x, y, r) {
    var i;
    var angle = 2 * Math.PI / num;
    for (i = 0; i < num; i++) {
        putImage(i + offset, x + r * Math.cos(i * angle) - 75, y + r * Math.sin(i * angle) - 75, json.galleries[i + offset]);
    }
}

function rearrange(num, offset, x, y, r, ao) {
    var i, behind, mask, xx, yy;
    var angle = 2 * Math.PI / num;
    for (i = 0; i < num; i++) {
        xx = x + r * Math.cos(i * angle + ao) - 75;
        yy = y + r * Math.sin(i * angle + ao) - 75;
        behind = $('#behind' + (i + offset));
        mask = $('#mask' + (i + offset));
        behind.css('left', xx);
        behind.css('top', yy);
        mask.css('left', (xx - 10));
        mask.css('top', (yy - 10));
    }
}

function init(json) {
    arrange(10, json, 0, $(window).width() / 2, $(window).height() / 2, 340);
    arrange(16, json, 10, $(window).width() / 2, $(window).height() / 2, 540);
    setInterval(function() { if (!pause) { tick++; resize(); } }, 100);
    $('#centreback').hover(function() {
        $('.ctext').fadeIn('fast');
        $('#centreback').fadeOut('slow');
    });
    setTimeout(function () {
        $('.ctext').fadeIn(500);
        $('#centreback').fadeOut(1000);
    }, 2200);
}

function resize() {
    rearrange(10, 0, $(window).width() / 2, $(window).height() / 2, 340, tick * 0.003);
    rearrange(16, 10, $(window).width() / 2, $(window).height() / 2, 540, tick * -0.002);
    var c = $('.centre');
    c.css('left', ($(window).width() - c.attr('width')) / 2);
    c.css('top', ($(window).height() - c.attr('height')) / 2);
    c = $('.ctext');
    c.css('left', ($(window).width() - c.attr('clientWidth')) / 2);
    c.css('top', ($(window).height() - c.attr('clientHeight')) / 2);
    c = $('#bottomright');
    c.css('left', $(window).width() - c.attr('clientWidth') - 4);
    c.css('top', $(window).height() - c.attr('clientHeight') - 4);
}

$(document).ready(function() {
    $.ajax({
        url: '/teen.json',
        dataType: 'script',
        success: function(d) {
                init(data);
                resize();
        },
        error: function(req, stat, err) {
            $.getJSON('/failover.json', function(json) {
                init(json);
                resize();
            });
        }
    });
});

$(window).resize(resize);


