// Nicer margins for content images based on their position
$(".textpage img").each(function() {
    if ( $(this).css("float") == "left" ) {
        $(this).css("margin-left", "0px");
    } else if ( $(this).css("float") == "right" ) {
        $(this).css("margin-right", 0);
    }
});

// IE Safari detect ( for button fixes )
(function($) {
    var userAgent = navigator.userAgent.toString().toLowerCase();
    if ((userAgent.indexOf('safari') != -1) && !(userAgent.indexOf('chrome') != -1) && (navigator.platform=="Win32" || navigator.platform=="Win64")) {
        $("body").addClass("safari");
    }
})(jQuery);

// #nav hover
$("#nav a").bind({
    mouseenter: function() {
        $('.hover', this).animate({
            top: "0"
        }, 200);
    },
    mouseleave: function() {
        $('.hover', this).stop(true,false).animate({
            top: "-16px"
        }, 200);
    }
});

// Whats Hot slider
(function($) {
    var whatshot = $("#whatshot");
    var whatshotItems = $("div.item", whatshot);
    var whatshotItemsCount = whatshotItems.length;
    var animateSpeed = 500;
    var currentItem = $("div.item:first", whatshot);
    var previousButton = $(".previous", whatshot);
    var nextButton = $(".next", whatshot);
    var canAutonext = true;
    var timer;

    animate = function(fromItem, toItem) {
        fromItem.css("z-index", 1).animate({
            opacity: 0
        }, animateSpeed);
        toItem.css("z-index", 2).animate({
            opacity: 1
        }, animateSpeed);
        currentItem = toItem;
    }

    nextImage = function() {
        var nextItem = currentItem.next();
        if (nextItem.length===0) {
            nextItem = $("div.item:first", whatshot);
        }
        animate(currentItem, nextItem);
    }

    previousImage = function() {
        var prevItem = currentItem.prev();
        if (prevItem.length==0) {
            prevItem = $("div.item:last", whatshot);
        }
        animate(currentItem, prevItem);
    }

    previousButton.click(function(e) {
        previousImage();
        clearInterval(timer);
        timer = setInterval(autonext,5000);
    });

    nextButton.click(function(e) {
        nextImage();
        clearInterval(timer);
        timer = setInterval(autonext,5000);
    });

    /*
    // canAutonext check
    $("#threegroups, #whatshot").bind({
        "mouseenter": function() {
            canAutonext = false;
        },
        "mouseleave": function() {
            canAutonext = true;
        }
    })
    */

    function autonext() {
        if (canAutonext) {
            nextButton.trigger("click");
        }
    }
    timer = setInterval(autonext,5000);

    whatshot.disableTextSelect();

})(jQuery);

// 3groups slider
(function($) {
    var threegroups = $("#threegroups");
    var groups = $(".group", threegroups);
    var canHover = false;
    var animateSpeed1 = 100;
    var animateSpeed2 = 200;
    var timer, timer2;

    $(".inner", threegroups).hover(
        function() {
            timer = setTimeout(enableHover, 300);
        },
        function() {
            disableHover();
        }
        );

    function enableHover() {
        canHover = true;
    }

    function disableHover() {
        clearTimeout(timer);
        canHover = false;
    }

    groups.bind({
        mouseenter: function() {
            var el = this;
            if (!canHover) {
                timer2 = setInterval(intervalAction, 10);
            } else {
                intervalAction();
            }

            function intervalAction() {
                if (canHover) {
                    clearTimeout(timer2);
                    $(el).addClass("hover");
                    animateBg($(".hoverbg", el), $("div.img", el), "show");
                }
            }

        },
        mouseleave: function() {
            clearTimeout(timer2);
            animateImage($(".hoverbg", this), $("div.img", this), "hide");
        }
    });

    animateBg = function(bg, img, action) {
        if (action==="show") {
            bg.css("display", "block").animate({
                opacity: 1
            }, animateSpeed1, function() {
                animateImage(bg, img, "show");
            });
        } else if (action==="hide") {
            bg.stop(true,false).animate({
                opacity: 0
            },
            animateSpeed1,
            function() {
                 bg.css("display", "none");
            }
            );
        }
    }

    animateImage = function(bg, img, action) {
        if (action==="show") {
            img.animate({
                height: 124
            }, animateSpeed2);
        } else if (action==="hide") {
            bg.stop(true,false);
            img.stop(true,false).animate({
                height: 62
            }, animateSpeed2, function() {
                bg.parent().removeClass("hover");
                animateBg(bg, img, "hide");
            });
        }
    }

})(jQuery);

// showcase hover
(function($) {
    if ($.browser.msie && $.browser.version.substr(0,1)<9) {
        var speed = 0;
    } else {
        var speed = 350;
    }
    speed = 350;
    $(".boxWork").bind({
        mouseenter: function() {
            $(".hover", this).fadeIn(speed);
        },
        mouseleave: function() {
            $(".hover", this).stop(false,true).fadeOut(speed);
        }
    });
})(jQuery);


// About Us popup (hovering on people)
(function($) {
    var peopleBubble_html = ''+
    '<div id="peopleBubble" class="aller">'+
    '  <div class="bg"><div></div></div>'+
    '</div>';
    var peopleBubble = $(peopleBubble_html);

    $(".teamphoto .people .agent").bind({
        mouseenter: function() {
            var offsets = $(this).offset();
            $(".bg div", peopleBubble).html("");
            $("span", this ).each(function() {
                span = $(this).clone();
                $(".bg div", peopleBubble).append(span);
            });
            peopleBubble.css({
                "display": "none",
                "left": offsets.left+40+"px",
                "top": offsets.top-118+"px"
            })
            $("body").append(peopleBubble);
            peopleBubble.stop(false, true).fadeIn();
        },
        mouseleave: function() {
            peopleBubble.remove();
        }
    });
})(jQuery);

$("#showcaseDetail .nav a").hover(
    function () {
        $(this).addClass("mainHover");
        $(".hover", this).stop(0,1).fadeIn();
    },
    function () {
        $(this).removeClass("mainHover");
        $(".hover", this).stop(0,1).fadeOut();
    }
    );

$("#fourgroups a").hover(
    function () {
        $(this).addClass("hover");
        $(".bghover", this).stop(0,1).fadeIn();
    },
    function () {
        $(this).removeClass("hover");
        $(".bghover", this).stop(0,1).fadeOut();
    }
    );

$(".popup").each(function() {
    $(this).attr("target", "_blank");
});

$('#threegroups .group').biggerlink();
