/*
 * Zajistuje zobrazovani embedovanych u magazinu a prechody mezi nima
 */
function magazinesToggle() {
    var magazin = [];
    magazin[1] = '<div class="magazin-issuu-embed" id="magazin-issuu-embed-01"><object style="width:300px;height:182px" ><param name="movie" value="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf?mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=111002093842-ea2cddbb2097489bba3be11002cdec27&amp;docName=studenta_rijen_2011_issuu&amp;username=studenta&amp;loadingInfoText=Studenta%2022%20rijen%202011&amp;et=1324924078809&amp;er=44" /><param name="allowfullscreen" value="true"/><param name="menu" value="false"/><embed src="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf" type="application/x-shockwave-flash" allowfullscreen="true" menu="false" style="width:300px;height:182px" flashvars="mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=111002093842-ea2cddbb2097489bba3be11002cdec27&amp;docName=studenta_rijen_2011_issuu&amp;username=studenta&amp;loadingInfoText=Studenta%2022%20rijen%202011&amp;et=1324924078809&amp;er=44" /></object></div>';
    magazin[2] = '<div class="magazin-issuu-embed" id="magazin-issuu-embed-01"><object style="width:300px;height:182px" ><param name="movie" value="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf?mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=110502120130-88e793b36f024cb0b01ed57be447b8ba&amp;docName=studenta_kveten_2011&amp;username=studenta&amp;loadingInfoText=Studenta%2021%20kveten%202011&amp;et=1304341181116&amp;er=30" /><param name="allowfullscreen" value="true"/><param name="menu" value="false"/><embed src="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf" type="application/x-shockwave-flash" allowfullscreen="true" menu="false" style="width:300px;height:182px" flashvars="mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=110502120130-88e793b36f024cb0b01ed57be447b8ba&amp;docName=studenta_kveten_2011&amp;username=studenta&amp;loadingInfoText=Studenta%2021%20kveten%202011&amp;et=1304341181116&amp;er=30" /></object></div>';
    magazin[3] = '<div class="magazin-issuu-embed" id="magazin-issuu-embed-02"><object style="width:300px;height:182px" ><param name="movie" value="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf?mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=110226113836-f4c1149f33034c16a01cb77cdbc41e23&amp;docName=studenta_20_brezen_2011&amp;username=studenta&amp;loadingInfoText=Studenta%2020%20brezen%202011&amp;et=1300805710434&amp;er=78" /><param name="allowfullscreen" value="true"/><param name="menu" value="false"/><embed src="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf" type="application/x-shockwave-flash" allowfullscreen="true" menu="false" style="width:300px;height:182px" flashvars="mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=110226113836-f4c1149f33034c16a01cb77cdbc41e23&amp;docName=studenta_20_brezen_2011&amp;username=studenta&amp;loadingInfoText=Studenta%2020%20brezen%202011&amp;et=1300805710434&amp;er=78" /></object></div>';
    magazin[5] = '<div class="magazin-issuu-embed" id="magazin-issuu-embed-05"><object style="width:300px;height:181px" ><param name="movie" value="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf?mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=111215214617-49e69ab88c344e5b8e27630288408d84&amp;docName=studenta_prosinec_2011&amp;username=studenta&amp;loadingInfoText=Studenta%2023%20prosinec%202011&amp;et=1324924151271&amp;er=16" /><param name="allowfullscreen" value="true"/><param name="menu" value="false"/><embed src="http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf" type="application/x-shockwave-flash" allowfullscreen="true" menu="false" style="width:300px;height:181px" flashvars="mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Flight%2Flayout.xml&amp;showFlipBtn=true&amp;documentId=111215214617-49e69ab88c344e5b8e27630288408d84&amp;docName=studenta_prosinec_2011&amp;username=studenta&amp;loadingInfoText=Studenta%2023%20prosinec%202011&amp;et=1324924151271&amp;er=16" /></object></div>';
    
    $("#magazin-issuu-embed").show();    

    $("[id^=magazin-issuu-embed-link-]").livequery('click', function() {
        var nr = $(this).attr('id').substr(25,1);
        $("div[id^=magazin-issuu-embed]").fadeOut('slow', function() {
            $(this).replaceWith(magazin[nr]);
            $(this).fadeIn('fast');
        });
              
    });
}

/*
 * Auto resize pro <textarea>
 */
function fitToContent(/* JQuery */text, /* Number */maxHeight) {
        var adjustedHeight = text.height();
        var relative_error = parseInt(text.attr('relative_error'));
        if (!maxHeight || maxHeight > adjustedHeight) {
                adjustedHeight = Math.max(text[0].scrollHeight, adjustedHeight);
                if (maxHeight)
                        adjustedHeight = Math.min(maxHeight, adjustedHeight);
                if ((adjustedHeight - relative_error) > text.height()) {
                        text.css('height', (adjustedHeight - relative_error) + "px");
                        // chrome fix
                        if (text[0].scrollHeight != adjustedHeight) {
                                var relative = text[0].scrollHeight - adjustedHeight;
                                if (relative_error != relative) {
                                        text.attr('relative_error', relative + relative_error);
                                }
                        }
                }
        }
}

function autoResizeText(/* Number */maxHeight) {
        var resize = function() {
                fitToContent($(this), maxHeight);
        };
        $("textarea").attr('relative_error', 0);
        //$("texterea").css('scrollbar', 'no');
        //$("texterea").attr('visibility', 'hidden');
        $("textarea").each(resize);
        $("textarea").keyup(resize).keydown(resize);
}

/*
 * Resi hlasovani. Priradi potrebnou akci a zajisti vykresleni odpovedi
 */
function ratingBlog() {
    $("a.RatingUp:not(.RatingUpDisabled), a.RatingDown:not(.RatingDownDisabled)").click(function() {
        //var href = $(this).attr('href');        
        var boxIdPrefix = 'blog-rating-box-';
        var box = $('[id^=' + boxIdPrefix + ']');
        var boxId = box.attr('id');
        var id_blog = boxId.substr(boxIdPrefix.length, boxId.length - boxIdPrefix.length);
        var href = '';
        if ($(this).hasClass('RatingUp')) {
            href = baseUrl + '/' + 'rate-up' + '/' + id_blog;
        }
        else if ($(this).hasClass('RatingDown')) {
            href = baseUrl + '/' + 'rate-down' + '/' + id_blog;
        }
        $.get(href, null, function(data) {
            //@todo ID elementu, ktery ma zmizet by se melo ziskat pres $(this) a nejaky .parent call            
            box.fadeOut('fast', function() {
                box.replaceWith(data);
                box.fadeIn('fast');
            });
        })
        return false;
    })
}

/**
 * Vyhleda vsechny pozice banneru na strance a vrati je jako pole
 */
function getBannersPositions() {
    var bannersPositions = $('div[id^=banner]');
    var bannersPositionsToArray = [];
    bannersPositions.each(function(index) {
        //<div> ma id napr. banner300x250-content1 a potrebujem z toho to 300x250-content1
        bannersPositionsToArray[index] = $(this).attr('id').replace('banner', '');
    })
    return bannersPositionsToArray;
}

/**
 * Zameni bannery za nove bannery
 * Dostava asoc. pole 'pozice' => 'html kod'
 */
function swapBanners(bannersHtml) {
    for (var position in bannersHtml) {
        var bannerDiv = $('div[id=banner' + position + ']');
        //pokud by se mel banner nahradit google adwords kodem, tak ho nenahrazujem
        //zpusobuje to totiz presmerovani stranky na neco, co se nenacte - BUG
        //to same u banneru tahanych pres track.adform.net
        if ((bannersHtml[position].search('google_ad_client') == -1)
            && (bannersHtml[position].search('track.adform.net') == -1)
        ) {
            // Osetreni proti "trepani" pri zmeneni banneru
            var originalHeight = bannerDiv.height();
            bannerDiv.css('height', originalHeight); // boxu se nastavi puvodni velikost banneru
            var banner = $(bannersHtml[position]); // novy banner
            bannerDiv.html(banner.html());
        }
    }
}

/**
 * Pokud na strance je Google Analytics kod, zavola na nej _trackPageview
 * Pouziva si pri trackovani AJAX requestu
 * Duvodme pro zavedeni je fakt, ze na localhostu neni Google Anal kod a hazelo
 * by to tim padem chybu
 */

function trackPageview(url) {
    if (typeof(pageTracker) !== 'undefined') {
        pageTracker._trackPageview(url);    
    }
}

$(document).ready(function() {
    magazinesToggle();
    //autoResizeText(); //auto zvetsovani <textarea>
    //$("div[id*=tabs]").tabs(); //zavedeni tabu
    ratingBlog(); //hlasovani u blogu   
    //console.log($('#blog .list-item-big').height());

    //ooter-slider
    //podminka se muze oddelat, pokud bude footer-slider na vsech strankach
    if(!($("#footer-slider").length == 0)) { //element nalezen
       $("#footer-slider").footerSlider();
    }
    if(!($("#partners").length == 0)) { //element nalezen
        $("#partners").partners();
    }
});
