// www.itelligent.com.mx, Agosto 2010

$(function () {
    $('#busqueda').append(
        $('<div id="comprar" style="width: 165px; height: 35px; position: absolute; bottom: 190px; right: 0px; "></div>')
    );
})


var purchase = {
    hide: function () {
        $('#comprar')
            .find('div')
                .fadeOut('fast', function () {
                    $(this).remove();
                });
    },
    show: function (propiedades, url) {
        $('#comprar')
            .html(
                $('<div style="display: none; cursor: pointer; " class="' + (propiedades.pedido == '' ? 'no_encontrado' : 'encontrado') + '">' + (propiedades.pedido == '' ? 'PURCHASE' : 'VIEW PURCHASE') + '</div>')
                    .click((function (purchase) { return function () {
                        purchase.profile(propiedades, url)
                    }})(this))
            )
            .find('div')
                .fadeIn('fast');
    },

    profile: function (propiedades, url, onlyprofile, onpurchase) {
        referencia = propiedades.width < propiedades.height ? 'height' : 'width';
        referencia_otro = referencia == 'width' ? 'height' : 'width';
        
        var w = 420;

        propiedades[referencia] = parseInt(propiedades[referencia], 10);
        propiedades[referencia_otro] = Math.ceil(parseInt(propiedades[referencia_otro], 10) * w /propiedades[referencia]);
        propiedades[referencia] = w;

        tb_show(
            '',
            url + '&height=' + (propiedades.height + 190),
            null,
            function () {
                $('#portafolio_perfil .previo').append(
                    $('<img style="display:none; " src="' + url.replace('portafolio.perfil', 'portafolio.explorar_imagen') + '&a=' + w + '">')
                        .load(function () {
                                if($(this).data('marca'))
                                    return;

                                $(this).data('marca', true)

                                $(this).fadeIn('fast');
                        })
                        .each(function () {
                            if(this.complete && !$(this).data('marca'))
                                $(this).load();
                        })
                );

                $('#portafolio_perfil .controles .agregar')
                    .click(function () {
                        $.ajax({
                            type: "GET",
                            url: "index.php",
                            data: "js=true&mod=carrito_c.agregar&" + url.replace(/index.php\?mod=[\w\.]+&/, '') + '&f=' + $('#portafolio_perfil .precio select option:selected').attr('value'),
                            complete: (function (target, pedido) { return function (data) {
                                try {
                                    data = eval ('(' + data.responseText + ')')
                                }
                                catch(e) {
                                    if(confirm('Operation fail. Try again?'))
                                        return $(target).click();

                                    return false;
                                }

                                if(data === false) {
                                    alert('Operation no available');
                                    return false;
                                }

                                var sp = $('#menu_carrito').find('span')

                                if(sp.length == 0)
                                    $('#menu_carrito a').append(' <span>(1)</span>')
                                else
                                    sp.html('(' + data + ')')

                                if(onpurchase)
                                    return onpurchase(data > 0);

                                location.href = 'index.php?accion=carrito';

                                return false;
                            }})(this, propiedades.pedido)
                        });

                        return;
                    })
            },
            onlyprofile
        )
    },
    stop: function (onlyprofile) {
        tb_remove(onlyprofile);
    },

    isActive: function () {
        return $('#TB_window').length > 0;
    }
}
