/*

Author: NetConstruct

*/

// Remove the .no-js class so that a backup plan can be introduced in the stylesheet
$('html').removeClass('no-js');


$('#logo span').stop(true).mouseover(function () {

    var elem = $(this);
    var frames = 83;
    var frameCount = 0;
    var slideWidth = 218;
    var currentSlideWidth = slideWidth;
    var intervalID = null;

    function animate() {
        if (frameCount < frames) {
            elem.css('left', '-' + currentSlideWidth + 'px');
            frameCount++;
            currentSlideWidth += slideWidth;
        }
        else {
            window.clearInterval(intervalID);
        }
    }

    var intervalID = window.setInterval(animate, 35);

    return false;
});

$(document).ready(function () {
    $("a.fancyVideo").fancybox({
        'type': 'iframe',
        'scrolling': 'no',
        'centerOnScroll': true,
        'width': 560,
        'height': 320
    });

    $(".christmasTab").click(function () {
        $.fancybox({
            'padding': 0,
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': this.title,
            'width': 516,
            'height': 320,
            'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'autoDimensions': false,
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true',
                'allowScriptAccess': "always"
            },
            'onComplete': function () {
                
            }
        });

        return false;
    });
});

function onYouTubePlayerReady(playerid) {
    if (playerid == "xmasplayer") {
        var ytplayer = $("#fancybox-content embed")[0];
        ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    }
}

function onytplayerStateChange (newState) {
    if (newState == 0) {
        $.fancybox.close();
    }
}

// A simple animation that moves the position of the anchor to produce a nice effect
$("nav a").mouseover(function () {
    $(this).stop().animate({ "top": "-16px" }, { duration: 200 });
}).mouseout(function () {
    $(this).stop().animate({ "top": "0" }, { duration: 200 });
});


// A simple animation that animates an indent on hover
$('footer .csLinks li a, #csNav a').mouseover(function () {
    $(this).stop().animate({ "padding-left": "5px" }, { duration: 150 });
}).mouseout(function () {
    $(this).stop().animate({ "padding-left": "0" }, { duration: 150 });
});


// Creates a nice fading efect on the promo boxes
$(".promo .block h2").mouseenter(function () {
    clearTimeout($(this).data('timeoutId')); // on the event of a rollover on child , the timer is cancelled and the mouseleave event isn't fired
    $(this).prev().stop().css('opacity', '1').animate({ opacity: 0 }, 350);
}).mouseleave(function () {
    var element = $(this);

    // because of a focus issue, the event was being fired again when a child of the target node was rolled over, 
    var timeoutId = setTimeout(function () { $(element).prev().stop().animate({ opacity: 1 }, 350); }, 10);
    $(element).data('timeoutId', timeoutId); //set the timeoutId, allowing us to clear this trigger if the mouse comes back over
});

function fnClear(sender) { if (sender.value == defaultText) sender.value = ""; else if (sender.value == "") sender.value = defaultText; }

var defaultText = "Your email address";

$(document).ready(function () {
    /* The following code is executed once the DOM is loaded */

    $(".newsletterInput input[type=\"text\"]").val(defaultText);
    $(".newsletterInput input[type=\"text\"]").focus(function () { this.select(); })

    $('.socials').mobilyblocks();

    $('#latestReleases .latestReleaseScroller').cycle({
        fx: 'fade',
        slideExpr: '.latestRelease',
        speed: 4000
    });

    $('a[rel="lightbox"]').lightBox();

    var latestReleasesHeight = 0;
    $('#latestReleases .latestReleaseScroller .latestRelease').each(function () {
        var height = $(this).height();
        if (latestReleasesHeight < height) {
            latestReleasesHeight = height;
        }
    });
    $('#latestReleases .latestReleaseScroller').height(latestReleasesHeight);

    $('.flipBlock .imgWrap').bind("click", function () {

        // $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):

        var elem = $(this);

        // data('flipped') is a flag we set when we flip the element:

        if (elem.data('flipped')) {
            // If the element has already been flipped, use the revertFlip method
            // defined by the plug-in to revert to the default state automatically:

            elem.revertFlip();

            // Unsetting the flag:
            elem.data('flipped', false)
        }
        else {
            // Using the flip method defined by the plugin:

            elem.flip({
                direction: 'lr',
                color: '#00cda4',
                speed: 350,
                onBefore: function () {
                    // Insert the contents of the .sponsorData div (hidden
                    // from view with display:none) into the clicked
                    // .sponsorFlip div before the flipping animation starts:

                    elem.html(elem.siblings('.textWrap').html());
                }
            });

            // Setting the flag:
            elem.data('flipped', true);
        }
    });




});

/*  CarouFredSel: an infinite, circular jQuery carousel.
Configuration created by the "Configuration Robot"
at caroufredsel.frebsite.nl
*/

$("#slider1").carouFredSel({
    direction: "down",
    width: 301,
    height: 400,
    items: {
        width: 301,
        height: 400
    },
    scroll: {
        easing: "easeInOutQuad",
        pauseOnHover: true,
        duration: 4000
    },
    auto: {
        pauseDuration: 4000,
        delay: 4000
    }
});


$("#slider2").carouFredSel({
    direction: "down",
    width: 485,
    height: 400,
    items: {
        width: 485,
        height: 400
    },
    scroll: {
        easing: "easeInOutQuad",
        pauseOnHover: true,
        duration: 4000
    },
    auto: {
        pauseDuration: 4000,
        delay: 4000
    }
});


$("#slider3").carouFredSel({
    direction: "down",
    width: 155,
    height: 400,
    items: {
        width: 155,
        height: 400
    },
    scroll: {
        easing: "easeInOutQuad",
        pauseOnHover: true,
        duration: 4000
    },
    auto: {
        pauseDuration: 4000,
        delay: 4000
    }
});


$('#home .vid').click(function () {
    var 
    link = $(this),
    target = link.attr('id'),
    video = $('video[rel|="' + target + '"]');

    video.fadeIn('slow').get(0).play();
    $("#slider1, #slider2, #slider3").trigger("pause");
    return false;
});


$('#home video').bind('ended', function () {
    $(this).fadeOut('slow');
    if (!$("#slider1, #slider2, #slider3").hasClass('hover')) {
        $("#slider1, #slider2, #slider3").trigger("play");
    }
});


$(function () {
    var timer;

    $("#slider1, #slider2, #slider3").mouseover(function () {
        $("#slider1, #slider2, #slider3").addClass('hover');

        if (timer) {
            clearTimeout(timer);
            timer = null;
        }
        timer = setTimeout(function () {
            $("#slider1, #slider2, #slider3").trigger("pause");
        }, 1000);
    }).mouseout(function () {
        $("#slider1, #slider2, #slider3").removeClass('hover').trigger("play");
    });

});


$("#ExpandingTiles .expand").click(function () {
    $("#ExpandingTiles .text").fadeOut("fast");
    $(this).siblings(".text").fadeIn("fast");
    $("#ExpandingTiles .expandingTile").css("z-index", 0);
    $(this).parent().css("z-index", 1);
});

$("#ExpandingTiles .close").click(function () {
    $("#ExpandingTiles .text").fadeOut("fast");
});

$("#ClientTiles .shuffle").click(function () {
    $("#ClientTiles .tileItem").shuffle();
});





