(function ($) {

    var ison = true;

    $.fn.fadeinoutPause = function () {
        ison = false;
    };
    $.fn.fadeinoutResume = function (onetime) {
        $(this).loadPicz();
        if (!onetime)
            ison = true;
    };
    $.fn.fadeinoutReplace = function (pic, full) {
        $(this).fadeinoutPause();
        $(this).loadPicz(pic, full);
    }

    var preloadNo = 0;
    var preloaded = 0;

    var cbs = [];

    $.onpreload = function (cb) {
        if (!cb) {
            for (var x in cbs) {
                cb = cbs[x];
                cb();
            }
            return;
        }
        cbs.push(cb);

    }

    $.preloadOne = function (cb) {

        if (cb) {
            cb();
        }
        if (preloadNo == preloaded) {
            if ($.onpreload)
                $.onpreload();
        }
    }


    $.preloadPic = function (pic, cb) {

        var x = new Image();
        preloadNo++;
        x.onload = function () {
            preloaded++;
            $.preloadOne(cb);

        }
        x.src = pic;
        //$('#preloadGeneral').append('<div id="preload'+preloadNo+'" style="background: url('+pic+') no-repeat -9999px" />');

    }



    $.fn.fadeinout = function (fotos, cb) {
        if ($.browser.msie) cb = false;
        var self = $(this);

        //randomizepics
        fotos.sort(function (a, b) {
            if (Math.random() > 0.5)
                return 1;
            return -1;
        });

        if (cb) {

            $.onpreload(function () { self.fadeinout(fotos, false); cb(); });
            for (var x in fotos) {
                //alert(x);
                $.preloadPic(fotos[x].link);
            }
            //alert("preloading");
            return;
        }


        var startpic = 0;

        var self = $(this);

        self.addClass("mainDivF");

        var picno = startpic;



        $.fn.transit = function () {






            var oldpic = picno;

            $("#slix" + oldpic).removeClass("peKb_active");
            picno++;
            if (picno == fotos.length)
                picno = startpic;
            $("#slix" + picno).addClass("peKb_active");
            adjustz();
            //$("#slix"+picno).animate({opacity:1}, 1000);
            //$("#slix"+oldpic).animate({opacity: 0}, 1000);
            $("#slix" + oldpic).css({ opacity: 0 });
            $("#slix" + picno).css({ opacity: 1 }).show();

            //        $(".mainDiv").delay(delay).each(transit);
        }

        var picshow = 1;


        /*$.fn.loadPiczCssBack =     function (pikz, full) {
       
        var pichid = (picshow == 1)? 2: 1;
        $("#div"+pichid+"").removeClass("imgLoaded");
        //$("#div"+pichid+"").hide().css({opacity:0});

        if (!pikz)
        pikz =fotos[picno].link;
            
        //$("#div"+pichid).hide();
        $("#div"+pichid+"").attr("src", "");
        $("#div"+pichid+"").attr("src", pikz);
        
        if (full) {
        $("#div"+pichid+" img").addClass("full");
            
           
        }
        picno++;
        if (picno == fotos.length)
        picno = startpic;
         
        }*/

        $.fn.loadPicz = function (pikz, full) {

            var pichid = (picshow == 1) ? 2 : 1;
            $("#div" + pichid + " img").removeClass("imgLoaded");
            //$("#div"+pichid+"").hide().css({opacity:0});

            if (!pikz)
                pikz = fotos[picno].link;

            //$("#div"+pichid).hide();
            $("#div" + pichid + " img").attr("src", "");
            $("#div" + pichid + " img").attr("src", pikz);

            if (full) {
                $("#div" + pichid + " img").addClass("full");


            }
            picno++;
            if (picno == fotos.length)
                picno = startpic;

        }
        var dotransit = true;
        var piczLoaded = 0;
        $.fn.transitz = function (maybe) {
            //alert("transit" +maybe && !dotransit + " : " +ison);
            if (piczLoaded == 2) return;
            var pichid = (picshow == 1) ? 2 : 1;

            $("#div" + picshow).css({ opacity: 0 });
            $("#div" + picshow + " img").removeClass("full");
            $("#div" + pichid).show().css({ opacity: 1 });
            picshow = (picshow == 1) ? 2 : 1;


        }



        function adjustz() {
            //var height = $("body").height();
            //var width = $("body").width();   
            var height = self.height();
            var width = self.width();


            //alert("height "+height);

            /*$(".peKb_frame").height("100%");
            $(".peKb_frame").width("100%");
            $(".peKb_slides").height("100%").width("100%");
            $(".slideDiv").height("100%");*/

            var ratio = width / height;
            //$(".slideDiv img").each(function() {
            $(".imgLoaded").each(function () {
                var t = new Image();
                t.src = $(this).attr("src");
                $(this).css("display", "block");

                var picheight = t.height; //$(this).height();
                var picwidth = t.width; //$(this).width();

                t = null;

                var picratio = picwidth / picheight;


                if (ratio < picratio) {
                //$(this).height($("body").height());
                    //$(this).height($('html').height());
                    $(this).height("100%");
                    var xxa = parseInt(picratio * parseInt( $('html').height()));
                    //$(this).width( xxa);
                    //$(this).height($('html').height());
                    //$(this).height("100%");
                    //$('.copywrite').html( xxa  + "%" + "  --  " +$(this).css("width") + " - " + $(this).css("height"));
                } else {
                    var ratrat = ratio / picratio;
                    
                    if (ratrat) {
                        //var ha =  ($("body").height() * ratrat);
                        var ha = (100 * ratrat);
                        //alert(picwidth + " - " +picheight + "...... ratio:"+ratio + " .... picrat:"+picratio );
                        //alert(ha);
                        //$(this).height(ha + "%");
                        $(this).height(ha + "%");

                        $(this).width("100%");

                    } else {
                        $(this).height("100%");
                        //alert("Kona");
                    }
                    //$('.copywrite').html( "Big Rat" + (ratio * 100) + "%" + "  --  " +$(this).css("width") + " - " + $(this).css("height"));

                }

            });

        }

        //var pic1 = content.albuns[0].pasta_fotos + content.albuns[0].fotos[picno].file;
        var pic1 = fotos[picno].link;
        var forcex = false;
        var xox = fotos;

        var fotox = fotos;
        var delay = 7000;
        //var templ = $(".itemTemplate");
        /* for (var x= startpic; x < xox.length; x++) {

        var item =xox[x];
            
        var htm = '<div id="slix'+x+'" class="slideDiv peKb" data-delay="'+delay+'">'+
        '<img  class="slideImg" src="'+item.link+'" alt="Banner Image 1" />'+
        '</div>';
                      
        $(self).append(htm);
            
                        
        }*/


        var html = '<div id="div1" class="slideDiv peKb">' +
                          '<img class="slideImg"/>' +
                      '</div>' +
                    '<div id="div2" class="slideDiv peKb">' +
                          '<img class="slideImg"/>' +
                      '</div>';
        $(self).append(html);
        //$(this).width("100%").height("100%");
        $(".slideDiv").width("100%").height("100%");
        //$("#slix"+picno).css("opacity", "1").show();


        $("#div1 img").removeClass("imgLoaded").attr("src", fotos[picno].link);
        picno++;
        $("#div2 img").removeClass("imgLoaded").attr("src", fotos[picno].link);

        //$(".peKb").css("opacity", 0);    
        //$(".peKb_active").css("opacity", 1);
        adjustz();
        $(window).resize(adjustz);


        function pips() {
            dotransit = true;
            if (ison) //$(self).transitz();
                $(self).loadPicz();
            setTimeout(function () { pips(); }, delay);
        }

        var loaded = 0;
        $(".slideImg").load(function () {

            //loaded++;
            //if (loaded == (fotox.length - startpic)) 
            $(this).addClass("imgLoaded");
            adjustz();
            piczLoaded++;
            $(self).transitz(false);

            //setTimeout(function(){ pips(); }, delay);     
        });





        setTimeout(function () { pips(); }, delay);

        //if (!$.browser.safari) {





        //}
        //alert( navigator.userAgent);



        //$(".mainDiv").delay(delay).each(transit);

    }
}
)(jQuery);
