

/* Inicjalizacja */
$j(document).ready(function() {
preloadImgList[preloadImgList.length] = '/img/layout/default/products/musicplay_promo_sel.gif';
preloadImgList[preloadImgList.length] = '/img/layout/default/products/musicplay_sel.gif';

//$j.preloadImages(preloadImgList);
/*
	$j.preloadImages([
        '/img/loading.gif',
    	'/img/layout/default/products/musicplay_promo_sel.gif', 
    	'/img/layout/default/products/musicplay_sel.gif'
	]);
*/
    activateCluetip();
});




function activateCluetip(){

    var ctwidth = '200px';
    if(jQuery.browser.msie){
        if(jQuery.browser.version < 7){
            ctwidth = '190px';
        }
    }    

    $j('.more_tooltip, #cluetip-outer, #cluetip-arrows').unbind('mouseout',onmouseout_cluetip);

    $j('.more_tooltip, #cluetip-outer, #cluetip-arrows').unbind('mouseover',onmouseover_cluetip);

    $j('.more_tooltip, ').cluetip({
        sticky: true,
        cluetipClass: 'rounded noclose',
        width: ctwidth,
        leftOffset: 25,
        showTitle: true,
        arrows: true,
        dropShadow: false,
        local: true,
        fx: {
            open: 'show'
        }
    });

    $j('.more_tooltip_freeactivations, ').cluetip({
        sticky: true,
        cluetipClass: 'rounded noclose',
        width: 250,
        leftOffset: 25,
        showTitle: true,
        arrows: true,
        dropShadow: false,
        local: true,
        fx: {
            open: 'show'
        }
    });
    $j('.more_tooltip_promotion').cluetip({
        sticky: true,
        cluetipClass: 'rounded noclose',
        width: 190,
        topOffset: 15,     
        leftOffset: 10, 
        showTitle: true,
        arrows: false,
        dropShadow: false,
        local: true,
        cursor: 'pointer',
        fx: {
            open: 'show'
        }
    });   
    $j('.more_tooltip_promotion_code, ').cluetip({
        sticky: true,
        cluetipClass: 'rounded noclose',
        width: 250,
        topOffset: 15,     
        leftOffset: 10, 
        showTitle: true,
        arrows: false,
        dropShadow: false,
        local: true,
        cursor: 'pointer',
        fx: {
            open: 'show'
        }
    });    

    $j('.more_tooltip, #cluetip-outer, #cluetip-arrows').mouseout(onmouseout_cluetip);

    $j('.more_tooltip, #cluetip-outer, #cluetip-arrows').mouseover(onmouseover_cluetip);
}


var mydata = '';

var isAnimating = false;
var isNewPrependingData = false;
var isDownloadingPrev = false;
var slideLeftAfterDownload = false;
var isDownloadingNext = false;
var slideRightAfterDownload = false;

function afterAnimateTest(){
    isAnimating = false;
    $$('#more_products_links a.prev img')[0].removeClassName('blocked'); 
    $$('#more_products_links a.next img')[0].removeClassName('blocked');
    if(isNewPrependingData){
        isNewPrependingData = false;
        $j("#more_products_div2").prepend(mydata);
        mydata = '';
        moreProducts.positionLeft -= moreProducts.thumbWidth*moreProducts.lastThumbsPerSlide;
        $("more_products_div2").style.left = (moreProducts.positionLeft) + 'px';
    }
}


function more_products_prev(){
    if(moreProducts.left <= 0 || isAnimating){
        return;
    }
    
    if(!isDownloadingPrev){

        var thumbsPerSlide = moreProducts.thumbsPerSlide;
        
        if(moreProducts.left < thumbsPerSlide){
            thumbsPerSlide = moreProducts.left;
        }
    
        moreProducts.left -= thumbsPerSlide;
        moreProducts.first -= thumbsPerSlide;
        moreProducts.right += thumbsPerSlide;
        moreProducts.last -= thumbsPerSlide;
    
        $$('#more_products_links a.next img')[0].removeClassName('unactive');
        if(moreProducts.left <= 0){
            $$('#more_products_links a.prev img')[0].addClassName('unactive');
        }
    
        moreProducts.positionLeft += moreProducts.thumbWidth*thumbsPerSlide;
        isAnimating = true;
        $$('#more_products_links a.prev img')[0].addClassName('blocked');
        $$('#more_products_links a.next img')[0].addClassName('blocked');

        var animateSpeed = 250*thumbsPerSlide;

        //jesli nie mamy pelnej porcji w buforze, to dopełniamy bufor
        if(moreProducts.minFirst+moreProducts.thumbsPerSlide > moreProducts.first && moreProducts.minFirst > 0){
            isDownloadingPrev = true;
            thumbsPerSlide = moreProducts.thumbsPerSlide;
            if(moreProducts.minFirst < thumbsPerSlide){
                thumbsPerSlide = moreProducts.minFirst;
            }

            moreProducts.minFirst -= thumbsPerSlide;

            $j.post(thumb_url+"first:"+(moreProducts.minFirst)+"/limit:"+thumbsPerSlide,
                function(data, json){
                    mydata = data;
                    isNewPrependingData = true;
                    isDownloadingPrev = false;
                    if(!isAnimating){
                        mydata = '';
                        isNewPrependingData = false;
                        $j("#more_products_div2").prepend(data);
                        moreProducts.positionLeft -= moreProducts.thumbWidth*moreProducts.lastThumbsPerSlide;
                        $("more_products_div2").style.left = (moreProducts.positionLeft) + 'px';
                        if(slideLeftAfterDownload){
                            slideLeftAfterDownload = false;
                            $j('#more_products_links').unblock();
                            more_products_prev();
                        }
                    }
                }
            );
        }

        $j("#more_products_div2").animate({left:moreProducts.positionLeft+'px'}, animateSpeed, function(){afterAnimateTest();});
    } else {
        $j('#more_products_links').block({
            message: '<img src="/img/loading.gif" />',
            css: {border: '0px',backgroundColor: 'transparent'},
            overlayCss: {opacity: '0.7'}
        });
        slideLeftAfterDownload = true;
    }
}

function more_products_next(){
    if(moreProducts.right <= 0 || isAnimating){
        return;
    }

    if(!isDownloadingNext){

        var thumbsPerSlide = moreProducts.thumbsPerSlide;
    
        if(moreProducts.right < thumbsPerSlide){
            thumbsPerSlide = moreProducts.right;
        }
    
        moreProducts.left += thumbsPerSlide;
        moreProducts.first += thumbsPerSlide;
        moreProducts.right -= thumbsPerSlide;
        moreProducts.last += thumbsPerSlide;
    
        $$('#more_products_links a.prev img')[0].removeClassName('unactive');
        if(moreProducts.right <= 0){
            $$('#more_products_links a.next img')[0].addClassName('unactive');
        }

        isAnimating = true;
        $$('#more_products_links a.prev img')[0].addClassName('blocked');
        $$('#more_products_links a.next img')[0].addClassName('blocked');

        moreProducts.positionLeft -= moreProducts.thumbWidth*thumbsPerSlide;
        var animateSpeed = 250*thumbsPerSlide;
        //jesli nie mamy pelnej porcji w buforze, to dopełniamy bufor
        if(moreProducts.maxLast-moreProducts.thumbsPerSlide < moreProducts.last && moreProducts.maxLast < moreProducts.last + moreProducts.right){
            isDownloadingNext = true;
            thumbsPerSlide = moreProducts.thumbsPerSlide;
            if(moreProducts.last + moreProducts.right - moreProducts.maxLast < thumbsPerSlide){
                thumbsPerSlide = moreProducts.last + moreProducts.right - moreProducts.maxLast;
            }

            moreProducts.maxLast += thumbsPerSlide;

            $j.post(thumb_url+"first:"+(moreProducts.maxLast-thumbsPerSlide+1)+"/limit:"+thumbsPerSlide,
                function(data, json){
                    $j("#more_products_div2").append(data);
                    isDownloadingNext = false;
                    if(slideRightAfterDownload){
                        slideRightAfterDownload = false;
                        $j('#more_products_links').unblock();
                        more_products_next();
                    }
                }
            );
        }
        $j("#more_products_div2").animate({left:moreProducts.positionLeft+'px'}, animateSpeed, function(){
            isAnimating = false;
            $$('#more_products_links a.prev img')[0].removeClassName('blocked'); 
            $$('#more_products_links a.next img')[0].removeClassName('blocked');
        });

    } else {
        $j('#more_products_links').block({
            message: '<img src="/img/loading.gif" />',
            css: {border: '0px',backgroundColor: 'transparent'},
            overlayCss: {opacity: '0.7'}
        });
        slideRightAfterDownload = true;
    }
}


voteComment = function(id,type) {
    $j('#comment_'+id+'_vote').html('<img src="/img/loading.gif" alt="Ładowanie..." title="Ładowanie..." />');
    $j('#comment_'+id+'_vote').load("/comments/vote/"+id+"/"+type);
}

toggleComment = function(id) {
    $j("#cmnt_"+id+" div.cmmnt_txt").slideToggle('normal',function() {
        if($j(this).css('display') == 'block') {
            $j("#cmnt_"+id+" div.show_comment a").text("(Ukryj komentarz)");
        } else {
            $j("#cmnt_"+id+" div.show_comment a").text("(Pokaż komentarz)");
        }
    });
}       
 
reloadCaptcha = function() {
    $j('#captcha-image').attr('src','/captcha/image/' + Math.round(Math.random(0)*1000)+1);
}



