var interval=0;
var active = "";
var next = "";
foo = new Object();
foo.activeIndex=0;

$(document).ready(function(){
    if($.browser.msie && parseInt($.browser.version) <= '7'){
        Cufon.now();
    }
});

function randNumber(min,max){

    var adjustedHigh = (parseFloat(max) - parseFloat(min)) + 1;
    var numRand = Math.floor(Math.random() * adjustedHigh) + parseFloat(min);

    return numRand;
}

$(document).ready(function(){
    
    $('.field_field_adres_www .value:contains("http:")').each(function(){
        var project_url = $(this).html();
        $(this).html('<a href="'+project_url+'" title="'+($('#node-content > h1').html())+'" class="target" rel="nofollow">'+project_url+'</a>');
    });
    
    Cufon.replace('#main-menu a, h1, h2, h3, .navigation-element', {
        fontFamily: 'CenturyGothic',
        hover: true
    });

    $(".smyk").hide();
    jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
        this.each(function() {
            var shaker = $(this);
            var origLeft = parseInt(shaker.css('left'),10);
            var origTop = parseInt(shaker.css('top'),10);
            for (var x=1; x<=intShakes; x++) {
                shaker.animate({
                    top: origTop + intDistance + 'px'
                    },(intDuration/intShakes))
                .animate({
                    left: origLeft + (intDistance * -1) + 'px'
                    },(((intDuration / intShakes) / 4)))
                .animate({
                    top: origTop + (intDistance * -1) + 'px'
                    },(((intDuration / intShakes) / 3)))
                .animate({
                    left: origLeft + intDistance + 'px'
                    },((intDuration/intShakes)/2))
                .animate({
                    left: origLeft
                },(((intDuration/intShakes)/4)))
                .animate({
                    top: origTop
                },(((intDuration/intShakes)/4)));
            }
        });
        return this;
    }

    $.fn.IETextShadow = function(options){
        var opts = $.extend({
            ignore: ''
        }, options);
        $(this).not('input').not(opts.ignore).each(function() {
            var html, w, h;
            $context = $(this);
            $context.each(function() {
                var shadow = this.currentStyle['text-shadow'].split(' ');
                var fontSize = $(this).css('fontSize');
                var x = shadow[0];
                var y = shadow[1];
                var color = shadow[3];
                html = $(this).html();
                w = parseInt($(this).width());
                h = parseInt($(this).height());
                $(this).css({
                    'width':w,
                    'height':h,
                    'position':'relative'
                });
                $(this).empty().html('<span class="sh-v">' + html + '</span><span class="sh-s" style="font-size:' + fontSize + ';margin-left:' + x + ';margin-top:' + y + ';color:' + color + ';">' + html + '</span>');
                $(this).find('.sh-v').css({
                    'width':w,
                    'height':h
                })
                .next('.sh-s').css({
                    'width':w,
                    'height':h
                });
            });
        });
    };
    $('#frontpage-featured-box').append('<div class="cycle-nav"></div>');
    $('#frontpage-featured-box div.items').cycle({
        fx: 'scrollLeft',
        speed: 1000,
        timeout: 7000,
        pager: '.cycle-nav'
    });
    $('.custom-cycle-box').cycle({
        fx: 'fade',
        speed: 500,
        timeout: 2000
    });
    $('ul.cycle').after('<div id="showcase-pager"></div>').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 5000,
        pager: '#showcase-pager'
    });

    if($("#modliszka").length){
        $("#content").prepend('<div id="modliszka-sound"></div>');
        $("body").delegate('#modliszka','mouseover mouseleave', function(e){
            if (e.type == 'mouseover') {
                $('#modliszka-sound').css('backgroundImage','url(/template/portfolio/images/sounds/sound-'+randNumber(1, 10)+'.png)');
                $("#modliszka-sound").css('left',randNumber(-50, 200)).css('top',randNumber(30, 50)).fadeIn('medium').stop(true,false).shake(20, 1, 50);
            } else {
                $("#modliszka-sound").fadeOut('medium').stop(true,false);
            }
        });
    }

    $('#blog-list .item').hover(function(e) {
        e.stopPropagation();
        $('#blog-list .item').not($(this)).stop(true,true).animate({
            'opacity':0.2
        }, 200);
    },function(e) {
        e.stopPropagation();
        $('#blog-list .item').stop(true,true).animate({
            'opacity':1
        }, 200);
    });

    $('ol.level-1').hide();
    $('#main-menu > li').mouseover(function(){
        $(this).find('ol').stop(true,false).slideDown('medium');
    });
    $('#main-menu > li').mouseout(function(){
        $(this).find('ol').slideUp('medium');
    });

    $(".valign").vAlign();
    $(".hand").mouseover(function(){
        $(this).css("cursor","pointer");
    });

    $('#datagrid > table').attr('cellspacing','1');
    $('#datagrid tbody tr:odd').addClass('odd');
    $('#datagrid tbody tr:even').addClass('even');
    $('textarea,input').focus(function(){
        if($(this).hasClass('text')){
            $(this).addClass('focus');
        }
    });
    $('textarea,input').blur(function(){
        if($(this).hasClass('text') && $(this).hasClass('focus')){
            $(this).removeClass('focus');
        }
    });
    $('#datagrid > table > thead > tr > th').click(function(){
        var foo = $(this).find('a').attr('href');
        if(!(typeof foo=='undefined')){
            window.location.href=foo;
        }
    });
    $('#datagrid > table > tbody > tr > td').mouseover(function(){
        $(this).parent().addClass('row-over');
    });
    $("#datagrid > table > tbody > tr > td").mouseout(function(){
        $(this).parent().removeClass('row-over');
    });
    $("#datagrid > table > thead > tr > th").mouseover(function(){
        var foo = $(this).find("a").attr("href");
        if(!(typeof foo=='undefined')){
            $(this).addClass('hover');
        }
    });
    $("#datagrid > table > thead > tr > th").mouseout(function(){
        if($(this).hasClass('hover')){
            $(this).removeClass('hover');
        }
    });
    $("#data-rows-body tr td:last-child").css('width','90px');
    $(".close-parent").click(function(){
        $(this).parentNode.css('opacity',1).animate({
            opacity: 0.0
        }, 1000).hide();
    });
    $("input:submit").attr("id","submit");

    $("a[href$='jpg']").css('border','0');
    $("a > img").parent().css('border','0');
    $("a[href$='jpg'], a[href$='png'], a[href$='gif'], a[href$='jpeg'], a[href$='bmp'], a[href$='JPG'], a[href$='PNG'], a[href$='GIF'], a[href$='JPEG'], a[href$='BMP'], a[rel=group], .lightbox, .shadowbox, .fancybox").fancybox({
        'titlePosition' : 'over',
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic',
        'easingIn' : 'easeOutBack',
        'easingOut' : 'easeInBack'
    });

    $('.grass').mouseover(function(){
        $(this).stop(true,false).shake(5, 1, 1);
    });

    $('.biedronka').hover(function(){
        $(this).find('.cloud-baloon').stop(true,true).fadeIn('medium');
    },function(){
        $(this).find('.cloud-baloon').stop(true,true).fadeOut('medium');
    });

    $('.blog-image').mouseover(function(){
        $(this).stop(true,false).animate({
            borderColor:'349EDC'
        },300);
    });
    $('.blog-image').mouseleave(function(){
        $(this).stop(true,false).animate({
            borderColor:'69BF0F'
        },300);
    });
    $('.motyl-01').mouseover(function(){
        $(this).find('.butterfly').stop(true,false).animate({
            top:'-5',
            left:'5'
        },500);
        if(!$.browser.msie){
            $(this).find('.shadow').stop(true,false).animate({
                top:'5',
                left:'-5',
                opacity:0.3
            },500);
        }
    });
    $('.motyl-01').mouseleave(function(){
        $(this).find('.butterfly').stop(true,false).animate({
            top:0,
            left:0
        },500);
        if(!$.browser.msie){
            $(this).find('.shadow').stop(true,false).animate({
                top:0,
                left:0,
                opacity:1
            },500);
        }
    });
    $('.motyl-02, .motyl-03').mouseover(function(){
        $(this).find('.butterfly').stop(true,false).animate({
            top:'-5',
            left:'-5'
        },500);
        if(!$.browser.msie){
            $(this).find('.shadow').stop(true,false).animate({
                top:'5',
                left:'5',
                opacity:0.3
            },500);
        }
    });
    $('.motyl-02, .motyl-03').mouseleave(function(){
        $(this).find('.butterfly').stop(true,false).animate({
            top:0,
            left:0
        },500);
        if(!$.browser.msie){
            $(this).find('.shadow').stop(true,false).animate({
                top:0,
                left:0,
                opacity:1
            },500);
        }
    });
    $("#drupal-sites").jcarousel({
        wrap: 'circular',
        vertical:true,
        visible:1,
        scroll:1,
        auto:3
    });

    $("#promoted-box").mouseover(function(){
        $(this).stop(true,false).animate({
            left:'0px'
        },100);
    });
    $("#promoted-box").mouseout(function(){
        $(this).animate({
            left:'-'+parseInt($("#promoted-box-content").width())+'px'
            },100);
    });

    $(".target").attr("target","_blank");
    $("#portfolio-tree-lines").pulse({
        pulseTime:2000,
        interval:5000
    });

    var floater = function(){
        $('#balloon-01').animate( {
            'marginTop': '20px'
        }, 2000,'linear',function(){
            setTimeout(floater,10);
        } );
        $('#balloon-01').animate( {
            'marginTop':'0px'
        }, 2000,'linear',function(){
            setTimeout(floater,10);
        });
        $('#balloon-02').animate( {
            'marginTop': '10px'
        }, 1000,'linear',function(){
            setTimeout(floater,10);
        } );
        $('#balloon-02').animate( {
            'marginTop':'0px'
        }, 1000,'linear',function(){
            setTimeout(floater,10);
        });
    }

    floater();

});

(function ($) {
    $.fn.pulse = function(o) {
        this.each(function() {
            var elem = $(this);
            pulseTime = o.pulseTime ? o.pulseTime : 2000;
            interval = o.interval ? o.interval : 4000;
            if(elem!=null) {
                setInterval(function() {
                    elem.is(":hidden") ? elem.fadeIn(pulseTime) : elem.fadeOut(pulseTime)
                    },interval);
            }
        });
    };
})(jQuery);

//alphanumeric rules in input fields - jQuery validation
$(document).ready(function(){
    $('input.field-code').alpha({
        nocaps:true,
        allow:"_",
        ichars:"ąęśżźćńłó[]{};:?.,-<>=+`~!@#$%^&*()"
    });
    $('input.system-code').alpha({
        nocaps:true,
        allow:"-_",
        ichars:"ąęśżźćńłó[]{};:?.,<>=+`~!@#$%^&*()"
    });
    $('input.user-login').alphanumeric({
        nocaps:true,
        allow:"-_.",
        ichars:"ąęśżźćńłó[]{};:,?<>=+`~!@#$%^&*()"
    });
    $('input.url-alias-code').alphanumeric({
        nocaps:true,
        allow:"-_,.",
        ichars:"ąęśżźćńłó[]{};:?<>=+`~!@#$%^&*()"
    });
    $('input.url-path').alphanumeric({
        nocaps:true,
        allow:"-_,/"
    });
    $('input.numeric').numeric();
});

$(document).ready(function(){
    DD_roundies.addRule('.zaokraglona-ramka', '5px');
    if($.browser.msie && $.browser.version <= '6'){
        DD_belatedPNG.fix('img');
        $(".smyk").hide();
    }
});

