function gtkMore()
{
    $('.good-to-know > p > a.hide, .good-to-know > p > span').show();
    $('.good-to-know > p > a.show, .good-to-know > p > span.dots').hide();
}
function gtkLess()
{
    $('.good-to-know > p > a.hide, .good-to-know > p > span').hide();
    $('.good-to-know > p > a.show, .good-to-know > p > span.dots').show();
}

function getFormArray(query)
{
    serializedArray = $(query).serializeArray();
    json = {};
    for (i in serializedArray)
    {
        json[serializedArray[i].name] = serializedArray[i].value;
    }
    return json;
}

var captcha_visible = false;

function loadCaptcha()
{
    $.post(base_url,{action:'getCaptcha'},function(d)
    {
        if(d.captcha)
        {
            $('#captcha-block .captcha').html(d.captcha);
            $('#captcha-block').slideDown(500, function() {$('.add-comment-form #submit').val('Dodaj');});
            captcha_visible = true;
        }
    },'json');
}

function wrongInput(element)
{
    $(element).addClass('wrong-input');
    setTimeout(function() {$(element).removeClass('wrong-input');}, 2000);
    return false;
}

function clearCommentForm()
{
    $('#comment-parent').val(0);
    $('#comment-username').val('');
    $('#comment-text').val('');
    captcha_visible = false;
    $('#captcha-block').hide();
    $('#comment-captcha').val('');
    $('#comment-terms').attr('checked', false);
}

function addCommentFormPrepare()
{
    $('.add-comment-form #submit').click(function()
    {
        if($.trim($('#comment-username').val()) == '')
        {
            return wrongInput('#comment-username');
        }
        if($.trim($('#comment-text').val()) == '')
        {
            return wrongInput('#comment-text');
        }
        if($('#comment-terms').is(':checked') == false)
        {
            return wrongInput('.comment-terms-wrapper');
        }

        if(captcha_visible == false)
        {
            loadCaptcha();
        }
        else
        {
            if($.trim($('#comment-captcha').val()) == '')
            {
                return wrongInput('#comment-captcha');
            }
            params = getFormArray('.add-comment-form');
            params.action = 'add-comment';

            $.post(base_url, params, function(d)
            {
                if(d.status == true)
                {
                    clearCommentForm();
                    if(d.comments != undefined)
                    {
                        $('.comments-view').html(d.comments);
                        window.location.hash = '#komentarz-dodany';
                        $('.comment-just-added').slideDown(1000);
                    }
                }
                else
                {
                    if(d.error != undefined)
                    {
                        switch(d.error)
                        {
                            case 'bad-captcha':
                                return wrongInput('#comment-captcha');
                                break;
                        }
                    }
                }
            }, 'json');
        }
        return false;
    });
}

function commentPlus(comment_id)
{
    $.post(base_url, {action:'comment-plus',comment_id:comment_id}, function(d)
    {
        if(d.status==true)
        {
            if(d.rank != undefined)
            {
                $('#comment-'+comment_id+' > li > div > small > .rank').replaceWith(d.rank);
            }
        }
    }, 'json');
}

function commentMinus(comment_id)
{
    $.post(base_url, {action:'comment-minus',comment_id:comment_id}, function(d)
    {
        if(d.status==true)
        {
            if(d.rank != undefined)
            {
                $('#comment-'+comment_id+' > li > div > small > .rank').replaceWith(d.rank);
            }
        }
    }, 'json');
}

function commentResponse(comment_id)
{
    $('#comment-parent').val(comment_id);
    user = $('#comment-'+comment_id+' > li > span').html();
    msg = 'Udzielasz odpowiedzi użytkownikowi <b>'+user+'</b>. <a href="javascript: void(0)" onclick="cancelCommentResponse()">Anuluj odpowiedź</a>.';

    $('#comment-text').val('@'+user+': ');
    $('.comment-info').html(msg).slideDown(500);
    $(".scl-tabs").tabs("select","skomentuj");
    window.location.hash = 'skomentuj-tabs';
    $('#comment-username').focus();
}

function cancelCommentResponse()
{
    $('#comment-parent').val(0);
    $('.comment-info').html('');
}

function getSelection()
{
  return (!!document.getSelection) ? document.getSelection() : (!!window.getSelection) ? window.getSelection() : document.selection.createRange().text;
}

function commentQuote(comment_id)
{
    quote = $('#comment-'+comment_id+' > li > p').html().replace(/<br\s*\/?>/mg,"\n");
    sel = $.trim(getSelection());

    if(sel != '' && quote.indexOf(sel) != -1) quote = sel;
    
    $('#comment-text').val('"'+quote+"\"\n");
    $(".scl-tabs").tabs("select","skomentuj");
    window.location.hash = 'skomentuj-tabs';
    $('#comment-username').focus();
}

function bt_slider_init(carousel)
{
    $('.bt-slider-btn-left').bind('click', function() {carousel.prev();return false;});
    $('.bt-slider-btn-right').bind('click', function() {carousel.next();return false;});
}

function targeo()
{
    var targeo_js   = document.createElement('script');
    targeo_js.type  = 'text/javascript';
    targeo_js.src   = 'http:'+'/'+'/'+'mapa.targeo.pl/Targeo.html?vn=1_9&v=traffic&k=KreatoriNjZlZWI3MTQ0NzE0ZWVhYmYwNDQyOWMyNTUyNmQ5OGMyNA==';
    var script      = document.getElementsByTagName('script')[0];
    script.parentNode.insertBefore(targeo_js, script);
}

$(document).ready(function()
{
    //$('img').not('.p-cycle img').not('.article > .photo > img').not('.bt-slider img').lazyload({effect:'fadeIn'});
    $('.search > .sType, .search > .submit, .pb_r').hover(function()
    {
        $(this).addClass('_over');
    }, function() {
        $(this).removeClass('_over');
    });
    $('.p-cycle ul').cycle(
    {
        fx:     'fade',
        speed:  'fast',
        timeout: 10000,
        pager:  '.p-cycle > .swapper'
    });
    $('.info-bar ul').cycle(
    {
        fx:     'scrollUp',
        speed:  'slow',
        timeout: 6000,
        next: '.info-bar > .ib-swap',
        cleartype: !$.support.opacity,
        cleartypeNoBg: true
    });

    hash = window.location.hash;
    hash = hash.split('-');
    if(hash[0] == '#komentarz')
    {
        id = hash[1];
        $('#comment-'+id).css('backgroundColor', '#ffa');
    }

    $(".bt-slider > .bt-slider-items").jcarousel(
    {
        initCallback : bt_slider_init
        ,scroll: 1
        ,buttonNextHTML: null
        ,buttonPrevHTML: null
        ,wrap: 'circular'
        ,animation : 250
    });
	swfobject.embedSWF('http:'+'/'+'/'+'r.ekurjerwarszawski.pl/hotel-zubrowka/sylwester_468x60_v2.swf', 'baner-sylwester', '468', '60', '9.0.0');
	swfobject.embedSWF(base_url+'swf/autobus-baner.swf', 'right-banner', '300', '75', '9.0.0');
    
    $('#facebook-frame').html('<iframe src="http:'+'/'+'/'+'www.facebook.com/plugins/likebox.php?id=145036002197083&width=300&connections=15&stream=false&header=false&frame=true&height=340" scrolling="no" frameborder="0" class="facebook-box" allowTransparency="true"></iframe>');
    /*
    $(".bt-slider > .bt-slider-items img").captify({ speedOver: 'fast', speedOut: 'fast', hideDelay: 100, animation: 'slide', prefix: '', opacity: '0.7', className: 'caption-bottom', position: 'bottom', spanWidth: '100%' });
    */
	
	
	
});

function photoreport(a)
{
    if(a=='next')
    {
        current_index++;
        if(current_index>photos.length-1) {current_index=0;}
    }
    if(a=='prev')
    {
        current_index--;
        if(current_index<0) {current_index=photos.length-1;}
    }

    if(lock) return false;
    lock = true;

    var ni = photos[current_index];

    cImg = $('.photo > img');

    url = base_url+'grafika/0-500,'+ni.n+','+ni.e;

    $('<img>').attr('src', url).load(function()
    {
        nh      = this.height;
        lh      = (cImg.height());

        nImg    = $(this).hide().css('top', -(lh));

        $('.photo').append(nImg);
        $('.photo-wrapper').animate({height:nh},750);

        //$('.photo-next, .photo-prev').css('top', -100);//-nh+nh/2-20);
        cImg.fadeOut(1000,function() {$(this).remove();});
        nImg.fadeIn(1000, function()
        {
            $(this).css('top', 0);lock = false;
            $('.photoreport-info > .photo-number').html((current_index+1)+'/'+(photos.length));
        });
    });    
    return false;
}

// analytics.
if((noGA !== undefined && noGA != true) && base_url == 'http'+':'+'/'+'/'+'ekurjerwarszawski.pl'+'/')
{
     var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-16368318-1']);
  _gaq.push(['_trackPageview']);
  _gaq.push(['_trackPageLoadTime']);

  (function() {
    var ga = document.createElement('script');ga.type = 'text/javascript';ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https:'+'/'+'/'+'ssl' : 'http:'+'/'+'/'+'www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga, s);
  })();
}

