/**
 * JavaScript Library required for all pages of the public site.
 *
 * Avoid altering this file per project; please add custom site-wide code to
 * custom.js instead.
 */

$(document).ready(function()
{

    // TELL A FRIEND:
    $('a.taf').bind('click', function() {
        window.open('http://www.freetellafriend.com/tell/?option=manual&title='+encodeURIComponent(document.title)+'&url='+encodeURIComponent(document.location.href), 'freetellafriend', 'scrollbars=1,menubar=0,width=617,height=530,resizable=1,toolbar=0,location=0,status=0,screenX=210,screenY=100,left=210,top=100');
        return false;
    });
    
    // OPEN POLL LINKS IN NEW WINDOW:
    $(".wp-polls-ul a").attr('target','_blank');
    
    // TOGGLE POLLS / ARCHIVE
    
    $("body.slug-polls-archive .wp-polls-ans").hide();
    $("body.slug-polls-archive .wp-polls h3").bind('click', function() {
        $(this).next().slideToggle();
        return false;
    });
    
    // OPEN FIRST POLL:
    var url = document.location.href.toString();
    if (url.match("#")) {
        anchor = "#" + url.split("#")[1];
        $("body.slug-polls-archive " + anchor + " .wp-polls-ans").show();
    } else {
        $("body.slug-polls-archive .wp-polls-ans").eq(0).show();
    }

    // SWAP SHORT FILMS
    
    $('body.slug-short-films .entry-content a').bind('click', function() {
        
        tmpLink = $(this).attr('href');
        v = $(this).getUrlParam('v');
        $(this).attr('href', 'javascript:void(0)');

        // revert all others:
        $('#movie-container').remove();
        $('.entry-content a img').show();

        // swap current movie:
        $(this).find('img').hide();
        ytm = '<div id="movie-container">'
            + '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="460" height="284">'
            + '<param name="movie" value="http://www.youtube.com/v/' + v + '&hl=en&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999&autoplay=1" />'
            + '<!--[if !IE]>-->'
            + '<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/' + v + '&hl=en&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999&autoplay=1" width="460" height="284">'
            + '<!--<![endif]-->'
            + '<!--Alternative content-->'
            + '<!--[if !IE]>-->'
            + '</object>'
            + '<!--<![endif]-->'
            + '</object></div>';
        $(this).parent().append(ytm);

        return false;
    });
    
    // -------------

    var story_width = 220;
    var pos = 0;
    var num = $('#spotlight-stories .story').length;

    // HELPERS: ---------------------------------------------------------------

    var advanceSlider = function(pos) {
        $('#spotlight-container').animate({
            left: -pos * story_width * 3 + "px"
        }, 1000, 'easeInOutQuad', updateButtons());
    }

    var updateButtons = function() {
        $("#slide-left").animate({opacity: ((pos > 0) ? 1 : 0.25) }, 500);
        $("#slide-right").animate({opacity: ((pos < Math.floor(num / 3.1)) ? 1 : 0.25) }, 500);
    }

    // INITIALIZE: ------------------------------------------------------------

    // Position stories horizontally:
    $('#spotlight-stories .story').each(function (i) {
        $(this).css("left", i * story_width + "px");
    });

    // Position slider out of view:
    $('#spotlight-container').css('left', 2 * num * -story_width + 'px');
    $('#spotlight-container').animate({opacity:1}, 500, function(){
        $('#spotlight-container').animate({left: "0"}, 1250, 'easeOutExpo', updateButtons());
    });
    
    // Add Left / Right Buttons:
    $('body.home #spotlight-stories').append(
        '<a id="slide-left" href="#"><span>Prev</span></a>' +
        '<a id="slide-right" href="#"><span>Next</span></a>'
    );

    // BEHAVIOR: --------------------------------------------------------------

    // Click behavior for Prev / Left
    $('#slide-left').bind('click', function() {
        if (pos > 0) {
            advanceSlider(--pos);
        }
        return false;
    });

    // Click behavior for Next / Right
    $('#slide-right').bind('click', function() {
        if (pos < Math.floor(num / 3.1)) {
            advanceSlider(++pos);
        }
        return false;
    });

    // Auto-advance:
    /*
    setInterval(function() {
        if (pos < Math.floor(num / 3.1)) {
            advanceSlider(++pos);
        } else {
            pos = 0;
            advanceSlider(pos);
        }
    }, 20000);
    */

});
