/*
* http://www.booking-monitor.com
* Copyright (c) 2009 Alexander Wiederhold (http://www.booking-monitor.com)
* Licensed under the booking monitor license (http://www.booking-monitor.com/license).
*/

function BM() {
    var bm = this;

    bm.chBuy = function(e) {
        $('[id^=price-]').hide();
        $('#price-' + $(e).val()).show();

        var c = $('.buy [name=price]:checked');

        if ($(c).hasClass('buy-rent'))
            c = 'buy-rent';
        else if($(c).hasClass('buy-hosted'))
            c = 'buy-hosted';
        else if($(c).hasClass('buy-credit'))
            c = 'buy-credit';
        else if($(c).hasClass('buy-buy'))
            c = 'buy-buy';

        $('.buy [name=price]:checked').attr('checked', false);
        var g = $('.buy [name=price].' + c + ':visible').attr('checked', true);
        bm.price();

        $(e).blur();
    }

    bm.price = function(e) {
        if ($(e).attr('name') == 'support')
            $('[name=support]').not(e).attr('checked', false);

        var a = $('.buy :input:checked');
        var o = 0, m = 0;

        $.each(a, function(i, v) {
            if($(v).hasClass('buy-one')) {
                o += parseFloat($(v).attr('rel'));
            }
            else if ($(v).hasClass('buy-monthly')) {
                m += parseFloat($(v).attr('rel'));
            }
        });

        $('#buy-one').text(bm.nFormat(o));
        $('#buy-monthly').text(bm.nFormat(m));
    }

    bm.nFormat = function(n) {
        if (!n)
            return '0,00';

        n = (Math.round(n * 100) / 100) + '';
        a = n.split('.');
        a1 = a[0];
        a2 = a.length > 1 ? (',' + a[1]) : ',00';
        var r = /(\d+)(\d{3})/;

        while (r.test(a1))
            a1 = a1.replace(r, '$1' + '.' + '$2');

        if (a2.length == 2) a2 += '0';

        a = a1 + a2;

        return a;
    }

    bm.director = function(t)
    {
        var page = $('#z-director-page').val();

        if (typeof t != 'undefined') {
            switch (t) {
                case 'first': page = 1; break;
                case 'last': page = 0; break;
                case 'next': page++; break;
                case 'prev': page > 1 ? page-- : page; break;
            }
        }

        var d = $('#z-director-order').val()
            + '|' + $('#z-director-direction').val()
            + '|' + $('#z-director-items').val()
            + '|' + page;

        var p = rq;
        p['datatype'] = 'json';
        p['director'] = d;

        $.ajax({
            url: '/',
            type: "post",
            dataType: "json",
            data: p,
            success: function(r) {
                if(typeof r.redirect != 'undefined')
                    location.href = r.redirect;

                if (typeof r.container != 'undefined')
                    $('#' + r.container).html(r.content);

                if(typeof r.message != 'undefined')
                    bm.message(r.message);

                if (typeof r.director != 'undefined') {
                    $('#z-director-items-current').html(r.director.start + '-' + r.director.end);
                    $('#z-director-items-current-total').html(r.director.items);
                    $('#z-director-page').val(r.director.page);
                }
                else {
                    $('#z-director-items-current, #z-director-items-current-total').css('color', 'red');
                }
            }
        });
    }

    bm.click = function(e){
        if ($(e).hasClass('delete'))
            bm.deleteConfirm(e);
        else if($(e).hasClass('chactive'))
            bm.ajaxAction(e);
        else if($(e).hasClass('newsletter'))
            bm.ajaxAction(e);
        else if($(e).hasClass('status'))
            bm.ajaxAction(e);
        else if($(e).hasClass('ajax-create'))
            bm.ajaxAction(e);
        else if($(e).hasClass('confirm'))
            bm.ajaxAction(e);
        else if($(e).hasClass('send'))
            bm.ajaxAction(e);
        else
            location.href = $(e).attr('href');
    }

    bm.ajaxAction = function(e) {
        $.ajax({
            url: $(e).attr('href'),
            type: 'post',
            dataType: 'json',
            success: function(r) {
                if(typeof r.message != 'undefined')
                    z.message(r.message);

                if (typeof r.container != 'undefined')
                    $('#' + r.container).html(r.content);
                else
                    $(e).html(r.content);
            }
        });
    }

    bm.remove = function(url) {
        $.ajax({
            url: url,
            type: "post",
            dataType: "json",
            success: function(r) {
                if (typeof r.container != 'undefined')
                    $('#' + r.container).html(r.content);
            }
        });
    }

    bm.redirect = function(target) {
        location.href = target;
    }

    bm.download = function() {
        $('#download').submit();
        return false;
    }

    bm.lcSelect = function(e) {
        $(e).focus();
        $(e).select();
    }

    bm.genLicense = function() {
        $('#licensegen').submit();
    }
}

bm = new BM();
z = bm;

$(document).ready(function() {
	$('#logo').click(function() {
		location.href = '/';
	});

	$('.required').append(' <span class="rqs">*</span>');

	$('.ui-state-default').hover(
		function(){
			$(this).addClass('ui-state-hover');
		},
		function() {
			$(this).removeClass('ui-state-hover');
		}
	);

    $('#place-tabs').tabs({ fx: { opacity: 'toggle' } });

    $('div.buy :input').click(function(){
        bm.price(this);
    });

    $('.license-key').click(function() {
        bm.lcSelect(this);
    });

    bm.price();
});
