$(document).ready(function(){

    //lightbox functions
    var functions = function ightboxFunctions(){
        $('.closebox a.lbAction').click(function(){
            $.fn.fancybox.close()
        });
		
        /* VALIDATING CONTACT FORM */
        $('#textAreaContact').keyup(function(){
            var text = $('#textAreaContact')[0].value.length;
            var max = 512;
            $('.max_number').html(max - text);
            if(max - text < 0){
                $('.max_number').css('color', 'red');
            }else{
                $('.max_number').css('color', '#ccc');
            }
        });
    }
	
    //lightbox init
    if ($("a.lbOn").fancybox) {
        $("a.lbOn").fancybox({
            'padding': 1,
            'autoDimensions': false,
            'height': 355,
            'width': 522,
            'overlayOpacity': 0.8,
            'overlayColor' : '#000',
            'callbackOnShow': functions,
            'hideOnContentClick': false
        });
    }

    //lightbox init betaid
    if ($("a.lbOn-betaid").fancybox) {
        $("a.lbOn-betaid").fancybox({
	    'hideOnOverlayClick': false,
            'width' : 600,
            'height' : '70%',
            'autoScale' : false,
            'type' : 'iframe',
            'onClosed': function(){                
                window.location.reload(true);
            }
        });
    }

    if ($("a.lbOn_mini").length) {
        $("a.lbOn_mini").click( function() {
            $('#zimio-alert').remove();
            var html = '<div id="zimio-alert" class="novedades-zimio"><img src="/zimio/img/mini-spinner.gif" /></div>';
            $('#content').prepend(html);
            $.get(this, function(data) {
                html = '<a href="#" onclick="$(\'#zimio-alert\').fadeOut(400, function() {$(this).remove() } )" class="close"><img src="/zimio/css/img/btn-close.png" alt="close"></a>';
                html+= data;

                $('#zimio-alert').html(html);
                setTimeout("$(\'#zimio-alert\').fadeOut(400, function() {$(this).remove() } )", 6000);
            } );
            return false; 
        } );
    }
    
    //alerts init for comment error
    if (getUrlVars()["comment_error"]) {
        $('#zimio-alert').remove();
        var html = '<div id="zimio-alert" class="novedades-zimio"><img src="/zimio/img/mini-spinner.gif" /></div>';
        $('#content').prepend(html);

        html = '<a href="#" onclick="$(\'#zimio-alert\').fadeOut(400, function() {$(this).remove() } )" class="close"><img src="/zimio/css/img/btn-close.png" alt="close"></a>';
        html+= '<p>'+$.url.decode(getUrlVars()["comment_error"])+'</p>';

        $('#zimio-alert').html(html);
        setTimeout("$(\'#zimio-alert\').fadeOut(400, function() {$(this).remove() } )", 6000);
        return false; 
    }
});

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
