var core = {
    callback: function(callback) {
        if (typeof callback === 'function') {
            callback(arguments);
        }
    },

    log: function(message) {
        if (typeof console === 'object') {
            console.log(message);
        }
    }
};

// ++++ Layout

var Layout = {
    fixes: function() {
        $("a").each(function(){
            $(this).addClass("internal");
        });

        $("a").filter(function() {
            return this.hostname && this.hostname !== location.hostname;
        }).removeClass("internal").attr('target', '_blank');

        $("a").filter(function() {
            var href = $(this).attr('href');
            if (href) {
                return $(this).attr('href').match(/^mailto/) ? true : false;
            }
            return false;
        }).removeClass("internal");

        if (document.getElementById('map')) {
            setTimeout(function() {
                var myOptions = {
                    zoom: 15,
                    scrollwheel: false,
                    center: new google.maps.LatLng(52.239564,20.988589),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map"), myOptions);
                var image = 'http://www.huncwot.com/www/img/google_maps_icon.png';
                var studio = new google.maps.Marker({position: new google.maps.LatLng(52.238564,20.988589), map:map, icon: image});
                google.maps.event.addListener(studio, 'click', infoShow);
                var infowindow;
                function infoShow(event) {
                    if (infowindow) {
                        infowindow.close();
                    }
                    infowindow = new google.maps.InfoWindow();
                    infowindow.setContent('Huncwot<br>ul. Ogrodowa 31/35<br>00-893 Warszawa');
                    infowindow.setPosition(new google.maps.LatLng(52.238564,20.988589));
                    infowindow.open(map);
                }
                infoShow(null);
                google.maps.event.addListener(studio, 'click', infoShow);
            }, 1000);
        }
    },

    operations: function(conteinerWrap) {

        var wraplinksNumber = $(".wrap").not(".wrap.picture, .wrap.text").length;


        var startNumber = 0;

        conteinerWrap.find(".wrap").not(".wrap.picture, .wrap.text").each(function(){
            $(this).find(".number").text(startNumber+1+".");
            startNumber = startNumber+1;
        });

        randPadOld = 1;
        randPad1Old = 1;

        if (conteinerWrap.find(".wrap:first").size() > 0) {
            conteinerWrap.find(".wrap:first").width(conteinerWrap.find(".wrap:first").find("img").width()).css({"margin-left": 0, "margin-top": 0 });
            andPad=Math.round(Math.random()*1+2);
            curH=conteinerWrap.find(".wrap:first").outerHeight();
            newH=Math.ceil(curH/42)*42;
            margBot = newH-curH;

            randPad1=Math.round(Math.random()*1+1);

            // if(randPad1==1) {
            //     newPad1=0;
            // }
            // if(randPad1==2) {
            //     newPad1=42;
            // }
            // if(randPad1==3) {
            //     newPad1=84;
            // }
            // if(randPad1==4) {
            //     newPad1=126;
            // }
            // if(randPad1==5) {
            //     newPad1=168;
            // }

            newPad1 = (randPad1 - 1 >= 0 ? randPad1 - 1 : 0) * 42;

            conteinerWrap.find(".wrap:first").css("margin-top",newPad1);
            randPad1Old = randPad1;

            conteinerWrap.find(".wrap").not(".wrap:first").each(function() {

                wrapThis = $(this);

                setRandomMarginLeft = function() {
                    randPad=Math.round(Math.random()*3+1);
                    if (randPadOld == randPad) {
                        setRandomMarginLeft();
                    } else {
                        if (randPad==1) {
                            newPad=0;
                        }
                        if (randPad==2) {
                            newPad=42;
                        }
                        if (randPad==3) {
                            newPad=84;
                        }
                        if (randPad==4) {
                            newPad=126;
                        }
                        if (randPad==5) {
                            newPad=168;
                        }

                        wrapThis.css("margin-left", newPad);
                        randPadOld = randPad;
                    }
                };

                setRandomMarginTop = function() {
                    randPad1 = Math.round(Math.random()*3+1);
                    if (randPad1Old == randPad1) {
                        setRandomMarginTop();
                    } else {
                        if (randPad1==1) {
                            newPad1=0;
                        }
                        if (randPad1==2) {
                            newPad1=42;
                        }
                        if (randPad1==3) {
                            newPad1=84;
                        }
                        if (randPad1==4) {
                            newPad1=126;
                        }
                        if (randPad1==5) {
                            newPad1=168;
                        }
                        wrapThis.css("margin-top",newPad1);
                        randPad1Old = randPad1;
                    }
                };

                setRandomMarginLeft();
                setRandomMarginTop();
                if ($(this).hasClass("picture")) {
                    $(this).css("margin-bottom", 21);
                    $(this).width($(this).children(":first").width());
                } else {
                    $(this).width($(this).find("img").width());
                    // ******
                    curH=$(this).outerHeight();
                    newH=Math.ceil(curH/42)*42;
                    margBot = newH-curH;
                    $(this).css("margin-bottom",margBot);
                }
            });
        }
    },

    init: function() {
        conteinerWrap = $("#conteiner");
        Loader.play();

        $(window).load(function(){
            $(document).waitForImages(function(){
                Layout.operations(conteinerWrap);
                $("#conteiner").masonry({
                    itemSelector : '.wrap',
                    columnWidth : 21
                });
                $("#hide_everything ").fadeOut();
                Loader.stop();
            });
        });
    }
};

// ++++ Grid

var Grid = {
    init: function() {
        var gCode = "g";
        var MykeyCode = gCode.charCodeAt();

        $(document).keypress(function(e){
            if (e.which == MykeyCode) {
                if ($("#conteiner").hasClass("show_grid")) {
                    $("#conteiner").removeClass("show_grid");
                } else {
                    $("#conteiner").addClass("show_grid");
                }
            }
        });
    }
};

// ++++ Menu



var Menu = {
    calculations: function() {

        var $oldMenu = $("#menu");
        var $newMenu = $("#new_menu");

        $oldMenu.find("a").each(function() {

            var thisElement = $(this);
            var thisText = thisElement.text();
            $newMenu.find("a").each(function() {
                var newText = $(this).text();
                if (newText == thisText) {
                    thisElement.addClass("stay");
                    $(this).addClass("old");
                } else {
                    thisElement.addClass("goaway");
                    $(this).addClass("new");
                }
            });
        });
        $oldMenu.find("a.stay").removeClass("goaway");
        $newMenu.find("a.old").removeClass("new");
        freeze = false;
    },

    animations: function() {

        var $oldMenu = $("#menu");
        var $newMenu = $("#new_menu");

        $newMenu.find("a.new").each(function() {
            if ($(this).parent().hasClass("refresh")) {
                $oldMenu.prepend("<li class='newli refresh'>"+$(this).parent().html()+"</li>");
                currentIndex = currentIndex + 1;
            } else {
                $oldMenu.find("li:eq("+currentIndex+")").after("<li class='newli'>"+$(this).parent().html()+"</li>");
                currentIndex = currentIndex + 1;
            }
        });

        $oldMenu.find("a.goaway").parent().animate({"width": 0}, 500, function() {
            $(this).remove();
        });
        $oldMenu.find(".newli").each(function() {
            $(this).animate({"width": $(this).find("a").outerWidth()+1}, 500, function() {
                $(this).removeClass("newli").width("");
            });
        });
        $newMenu.html("");
        $oldMenu.find("a").each(function() {
            $(this).removeClass("new").removeClass("stay");
        });
        $('#menu li:first a').attr('href', '/' + language);

        $('#menu li a').each(function(i, e) {
            var href = $(this).attr('href');
            if (!/\/set\-lang/.test(href))
            {
                try
                {
                $(this).attr('href', href.replace(/\/(pl|en)/, '/' + language));
                }
                catch(err)
                {
                }
            }
        });
    }
};

// ++++ Loader

var loaderFrame = 0;
var loaderTimeout = null;

var Loader = {
    animate: function() {
        if (loaderFrame < 12) {
            var leftPosition = loaderFrame * 37;
            ++loaderFrame;
            $("#png_loader").find("img").css("left", -leftPosition);
        }
        if (loaderFrame == 12) {
            loaderFrame = 0;
            $("#png_loader").find("img").css("left", 0);
            ++loaderFrame;
        }

        loaderTimeout = setTimeout("Loader.animate(1)", 83);
    },

    stop: function() {
        $("#png_loader").stop().fadeOut(function() {
            clearTimeout(loaderTimeout);
            $("#png_loader").find("img").css("left", 0);
        });
    },

    play: function() {
        clearTimeout(loaderTimeout);
        $("#png_loader").fadeIn();
        Loader.animate();
    }
};

// ++++ Load
var iframeTimeout = null;
var Load = {
    more: function() {
    },

    performance: function() {
        $("#hide_everything").fadeIn(500, function() {
            conteinerWrap = $("#loadconteiner");
            Layout.operations(conteinerWrap);
            $(document).scrollTop(0);
            $("#conteiner").html(conteinerWrap.html());
            conteinerWrap.html("");
            $("#conteiner").masonry( 'destroy');
            $("#conteiner").masonry({
                itemSelector : '.wrap',
                columnWidth : 21
            });
            $("#hide_everything").fadeOut(500, function() {
                freeze = false;
            });
            Loader.stop();
            Load.iframes();
        });
    },

    iframes: function() {
        clearTimeout(iframeTimeout);
        iframeTimeout = setTimeout(function() {
            $('iframe').each(function() {
                var $that = $(this);
                if ($that.data('src')) {
                    $that.attr('src', $that.data('src'));
                }
            });
        }, 10);
    }
};

