﻿function unob(o, e) {
    var link = '';
    for (i = 0; i < (e.length / 2); i++)
        link += String.fromCharCode((e.charCodeAt(i * 2) - 97) * 16 + e.charCodeAt(i * 2 + 1) - 97);
    document.location = link;        
    return false;
}

/*
showpopup

popid = ID of popup
pos = 4 char position
off = optional x,y offset
relx = position relative to this in X
rely = position relative to this in Y


rely is optional, defaults to relx

4 pos char code = (l|m|r)(l|m|r)(t|m|b)(t|m|b)
i.e. mmbt = Middle popup Middle relx Bottom popup Top rely

showpopup('popid','mmtb',{x:0,y:-10},this,this);
    
    
*/

function showpopup(popid, pos, off, relx, rely) {
    var p = document.getElementById(popid);

    var r;
    
    if (typeof relx == 'string')
        r = document.getElementById(relx);
    else
        r = relx;

    x = 0;

    for (e = r; e; e = e.offsetParent) {
        x += e.offsetLeft;
    }

    switch (pos.substring(0, 1)) {
        case 'm':
            x -= p.offsetWidth / 2;
            break;
        case 'r':
            x -= p.offsetWidth;
            break;   
    }

    switch (pos.substring(1, 2)) {
        case 'm':
            x += r.offsetWidth / 2;
            break;
        case 'r':
            x += r.offsetWidth;
            break;
    }

    rely = rely || relx;
    
    if (typeof rely == 'string')
        r = document.getElementById(rely);
    else
        r = rely;

    y = 0;

    for (e = r; e; e = e.offsetParent) {
        y += e.offsetTop;
    }

    switch (pos.substring(2, 3)) {
        case 'm':
            y -= p.offsetHeight / 2;
            break;
        case 'b':
            y -= p.offsetHeight;
            break;
    }
    
    switch (pos.substring(3, 4)) {
        case 'm':
            y += r.offsetHeight / 2;
            break;
        case 'b':
            y += r.offsetHeight;
            break;
    }

    if (off) {
        x += off.x;
        y += off.y;
    }
    
    p.style.left = x + 'px';
    p.style.top = y + 'px';
    p.className = p.className.replace('popup', 'popup-active');
}

function hidepopup(id) {
    var p = document.getElementById(id);
    p.className = p.className.replace('popup-active', 'popup');
}

function postComment() {
    var $f = $(this).parents('form');
    if ($f.length != 1) return false;
    $.post(
        '/comments/post',
        $f.serialize(),
        function (data, textStatus) {
            var $s = $f.parents('span');
            var $empty = $s.parents('.commenting').children('div.nocomments');
            if ($empty.length) {
                $empty.empty();
                $empty.remove();
            }
            $s.html(data);
            LoadAjaxCaptcha();
            $s.find('input[type=submit]').click(postComment);
            $s.find('a').click(cancelComment);
        }
    );
    return false;
}

function cancelComment() {
    initCommenting();
    return false;    
}

function initCommenting(data) {

    var $cmnt = $('div.commenting');
    var $span = $cmnt.children('span');
    if ($span.length != 1) return;

    if (data) $cmnt.data('data', data);

    $span.html('<button>Post a comment</button>');

    $span.find('button').click(function () {
        var $span = $(this).parent();
        var data = $span.parents('.commenting').data('data');
        $span.load('/comments/post?data=' + data, null, function () {
            var $t = $(this);
            $t.find('input[type=submit]').click(postComment);
            $t.find('a').click(cancelComment);
            LoadAjaxCaptcha();
        });
        return false;
    });
}

$(document).ready(function() {
    $.ajaxSetup({ cache: false });
});

function InitGalleryCarousel() {
    $(document).ready(function() {

        var $f = $('.gallerycarousel:first');
        if ($f.data('init')) return;
        $f.data('init', true);

        $('.gallerycarousel').data('currimg', 0);

        $('.gallerycarousel .thumbs div').click(function() {
            var $t = $(this);
            var i = $t.attr('imgnum');
            var $g = $t.parents('.gallerycarousel');

            //            $g.children('img:eq(' + $g.data('currimg').toString() + ')').hide('slow');
            //            $g.data('currimg', i);
            //            $g.children('img:eq(' + i.toString() + ')').show('slow');

            $g.children('img:eq(' + $g.data('currimg').toString() + ')').fadeOut('slow', function() {
                $g.data('currimg', i);
                $g.children('img:eq(' + i.toString() + ')').fadeIn('slow');
            });
        });

        $('.gallerycarousel .gnavprev').click(function() {
            var $t = $(this).parents('.gallerycarousel').children('.thumbs');
            $t.children('div:last-child').hide();
            $t.children('div:last-child').prependTo($t);
            $t.children('div:first-child').show('slow');
        });

        $('.gallerycarousel .gnavnext').click(function() {
            var $t = $(this).parents('.gallerycarousel').children('.thumbs');
            $t.children('div:first-child').hide('slow', function() {
                $t.children('div:first-child').appendTo($t);
                $t.children('div:last-child').show();
            });
        });

    });
};

function LoadAjaxCaptcha() {
    var $c = $('#captchaph');
    if (!$c.length) return;

    $.getScript('recaptcha/api/js/recaptcha_ajax.js', function () {
        Recaptcha.create($c.attr('pubkey'), 'captchaph', { theme: 'red' });
    });
}
