function jSubmit(form_id, url)
{
	var values = $(form_id).serialize();
	$.post(url, values, function (data) {
		$('#facebox_content').html(data);		
		}
	);
	return false;
}

function fSubmit(form_id, url, selector)
{
	var values = $(form_id).serialize();
	
	$.post(url, values, function (data) {
		$(selector).html(data);
		}
	);
	return false;
}
	 
function article_box_show(index, element)
{
	$('#main_ajax #ajax-buttons a').each(function (){
		$(this).removeClass('active');	
	});
	$(element).addClass('active');
	
	$('#main_ajax .ajax-content').each(
		function () {
			$(this).removeClass('visible');
		}
	);
	$('#ajax-'+index).addClass('visible');
}

function jReload(url, selector)
{
	$(selector).load(url);
	return false;
}


function jShowFacebox(url) {

    $.get(url, '', function(data, status) {

        $.facebox(data);

    });

}

function jCloseFacebox()
{
	jQuery(document).trigger('close.facebox')
}

var topics = [];

function findInArray(array, value) {

    for (var i = 0; i < array.length; i++)
        if (array[i] == value)
        return i;

    return -1;
}

function onShoutbox_up() {
    var s = parseInt($('#shoutbox_id').val());
    var index = findInArray(topics, s);
    if (index >= 0 && topics.length > 1) {
        if (index > 1)
            $('#shoutbox_id').attr('value', topics[index - 1]);
        else
            $('#shoutbox_id').attr('value', topics[topics.length - 1]);    
        onShoutbox_reload(true);
    }
    else {
        onShoutbox_reload(true);
    }    
}

function onShoutbox_down() {
    var s = parseInt($('#shoutbox_id').val());
    var index = findInArray(topics, s);
    if (index >= 0 && topics.length > 1) {
        if (index < topics.length - 1)
            $('#shoutbox_id').attr('value', topics[index + 1]);
        else
            $('#shoutbox_id').attr('value', topics[0]);       
        onShoutbox_reload(true);
    }
    else {
        onShoutbox_reload(true);
    }  
}

function onShoutbox_form()
{
	if($('#shoutbox_form textarea').attr('value') != 'Napisz co myślisz...')
	{
		var values = $('#shoutbox_form').serialize();
		$.post(shoutbox_backend_url, values, function (data) {
			shoutbox_handle_data(data);
			
		}, 'json');
		$('#shoutbox_form textarea').attr('value', 'Napisz co myślisz...');
	}	
}

function shoutbox_handle_data(data)
{
    shoutbox_total = data.total;
    topics = data.topics;
	$('#scroller').empty();
	$('#scroller').html('<div class="items"></div>');
	for(var i=0; i<data.data.length; i++)
	{
	    var d = data.data[i];
	    $('#scroller div.items').append('<div><span><a href="' + d.url + '">' + d.nick + '</a></span><p>' + d.text + '</p></div>');
    }
    var contentHeight = $('#scroller .items').height();

    
    $('#scroller').css({ overflow: 'hidden' });    
    contentHeight = Math.max(175, contentHeight);
    contentHeight = Math.min(475, contentHeight);
    $('#scroller').css({ height: contentHeight + 'px' });
    $('#shoutbox_id').attr('value', data.shoutbox);
    $('.box_shoutbox h4').html(data.shoutbox_text);
    if (scrollbarApi) {
        scrollbarApi.reload();
    }
}

function onShoutbox_reload(shoutbox) {

    if (!shoutbox) {
        $.getJSON(shoutbox_backend_url, function(data) {
            shoutbox_handle_data(data);
        });
    }
    else {
        $.getJSON(shoutbox_backend_url + '?shoutbox=' + $('#shoutbox_id').val(), function(data) {
            shoutbox_handle_data(data);
        });
    }
}

function cCorners(div, w, h, src, settings) {
    $(div).css({
        backgroundImage: "url('" + src + "')",
        backgroundRepeat: 'no-repeat',
        width: w + 'px',
        height: h + 'px'
    });
    $(div).html('');
    curvyCorners(settings, div);
}

function facebook_onlogin_ready()
{
	FB.Connect.requireSession( function() {
		jShowFacebox('http://www.3dwhite.pl/Klub-3dwhite/Logowanie/ajx,content.html');
	});
}
	
function refresh_page() {
	 window.location = 'http://www.3dwhite.pl/logout.php';
	//location.reload(true);
}


$(document).ready(
function() {
	
	/*$.getScript("http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php", function () {
		
		var api_key = "4845ac29767f93235731df746f9f42fd";
		var channel_path = "http://3dwhite.eu.istemp.pl/xd_receiver.htm";
		FB_RequireFeatures(["XFBML"], function() {
			FB.init(api_key, channel_path);
		});
		
		facebook_onload(true);
	});	
	*/
    var settings = {
        tl: { radius: 10 },
        tr: { radius: 10 },
        bl: { radius: 10 },
        br: { radius: 10 },
        antiAlias: true
    };

    $('.img_inside').each(function() {
        curvyCorners(settings, this);
    }
	);


    $('.article_img').each(function() {
        var w = $('img', this).width();
        var h = $('img', this).height();
        var src = $('img', this).attr('src');
        var dw = $(this).width();

        if (typeof src != 'undefined') {
            var div = this;
            var img = new Image();
            img.style.position = 'absolute';
            img.style.left = '-2000px';
            img.onload = function() {
                cCorners(div, this.width, this.height, this.src, settings);
                this.style.display = 'none';
                $(div).css({ clear: "both", width: dw + 'px' });
            }
            img.src = src;

        }
        else {
            var backgroundImage = $(this).css('background-image');
            var match = /url\((.*)\)/.exec(backgroundImage);
            if (match.length == 2) {
                var div = this;
                var img = new Image();
                img.style.position = 'absolute';
                img.style.left = '-2000px';
                var src = match[1];

                if (src.charAt(0) == '"') {
                    src = src.substr(1, src.length - 2);
                }
                img.onload = function() {
                    cCorners(div, this.width, this.height, this.src, settings);
                    this.style.display = 'none';
                    $(div).css({ clear: "both", width: dw + 'px' });
                }
                img.src = src;
                //document.body.appendChild(img);
            }
            else {
                curvyCorners(settings, this);
            }
        }
    });

    $('.required').blur(function() {
        this.value = trim(this.value);
        $(this).removeClass('error');
        if (!this.value.length) {
            $(this).addClass('error');
        }
    });
    $('.required').focus(function() {
        $(this).removeClass('error');
    });

    $('a[rel*=facebox]').facebox();

    $('#logo_3dw').each(
        function() {
            $(this).click(function() { document.location = '/'; });
            $(this).css('cursor', 'pointer');
        }
    );
}
);

