//var base_url = "http://osrodki-konferencyjne.pl/";

jQuery.noConflict();

function limitChars(textid, limit, infodiv){
    var text = jQuery('#' + textid).val();
    var textlength = text.length;
    if (textlength > limit) {
        jQuery('#' + infodiv).html(limit);
        jQuery('#' + textid).val(text.substr(0, limit));
        return false;
    }
    else {
        jQuery('#' + infodiv).html((limit - textlength));
        return true;
    }
}

function news_delete(id){
    confirmed = window.confirm("Czy na pewno chcesz usun\u0105ć?");
    if (confirmed){
        jQuery.ajax({
	    type: "POST",
    	        url: base_url + "news_delete.php",
    		data: "news_id=" + id,
                success: function(msg){
		jQuery('#news_list').html(msg);
    	    }
	});
    }
}

jQuery(document).ready(function() {
    jQuery('#news_content').TextAreaExpander(20, 300);
    jQuery('#news_content').keyup(function(){
        limitChars('news_content', 600, 'charLeft');
    });
    jQuery('#news_btn').click(function(){
        jQuery.ajax({
            type: "POST",
            url: base_url + "news_user_logged_in.php",
            data: "",
            success: function(msg){
                if (msg == '0'){
                    alert('Aby podzielić się nowościami musisz wykupić abonament.');
                } else {
                    var news_content = str_replace('&', 'amp;', jQuery('#news_content').val());
                    if (news_content.length > 0){
                        // send news
                        jQuery.ajax({
                            type: "POST",
                            url: base_url + "news_send.php",
                            data: "news_content=" + news_content,
                            success: function(msg){
                                jQuery('#news_list').html(msg);
                            }
                        });
                        // end - send news
                    } else {
                        alert('Zbyt krótka wiadomość!');
                    }
                }
            }
        })
    });
});

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
                f = [].concat(search),
                            r = [].concat(replace),
                                        s = subject,
                                                    ra = r instanceof Array, sa = s instanceof Array;
                                                        s = [].concat(s);
                                                            if (count) {
                                                                    this.window[count] = 0;
                                                                        }
                                                                        
                                                                            for (i=0, sl=s.length; i < sl; i++) {
                                                                                    if (s[i] === '') {
                                                                                                continue;
                                                                                                        }
                                                                                                                for (j=0, fl=f.length; j < fl; j++) {
                                                                                                                            temp = s[i]+'';
                                                                                                                                        repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
                                                                                                                                                    s[i] = (temp).split(f[j]).join(repl);
                                                                                                                                                                if (count && s[i] !== temp) {
                                                                                                                                                                                this.window[count] += (temp.length-s[i].length)/f[j].length;}
                                                                                                                                                                                        }
                                                                                                                                                                                            }
                                                                                                                                                                                                return sa ? s : s[0];
                                                                                                                                                                                                }
                                                                                                                                                                                                
