/**
 * 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.
 */

jQuery(document).ready(function()
{

    // TELL A FRIEND:
    jQuery('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:
    jQuery(".wp-polls-ul a").attr('target','_blank');
    
    // TOGGLE POLLS / ARCHIVE
    
    jQuery("body.slug-polls-archive .wp-polls-ans").hide();
    jQuery("body.slug-polls-archive .wp-polls h3").bind('click', function() {
        jQuery(this).next().slideToggle();
        return false;
    });
    
    // OPEN FIRST POLL:
    var url = document.location.href.toString();
    if (url.match("#")) {
        anchor = url.split("#")[1];
        if (anchor != '') {
            jQuery("body.slug-polls-archive #" + anchor + " .wp-polls-ans").show();
        }
    } else {
        jQuery("body.slug-polls-archive .wp-polls-ans").eq(0).show();
    }

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

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

        // swap current movie:
        jQuery(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>';
        jQuery(this).parent().append(ytm);

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

    var story_width = 220;
    var pos = 0;
    var num = jQuery('#spotlight-stories .story').length;
    
    
    // SUBSCRIBE MODAL:

	if (jQuery("#modal-window").length) {
		jQuery.jqm.params.modal = true;
		jQuery.jqm.params.toTop = true;
		jQuery.jqm.params.overlay = 80;

		jQuery("#modal-window").jqm({
			trigger: jQuery(".jqmOpen"),
			target: '#jqmContent',
			onShow:  loadInIframeModal
		});
	}

	jQuery(document).keydown(function(e) {
		if(e.which == 27) {  // escape, close box
			jQuery(".jqmWindow").length ? jQuery(".jqmWindow").jqmHide() : parent.jQuery(".jqmWindow").jqmHide(); // determine where keydown occured
		}
	});

	function loadInIframeModal(hash) {
		var $trigger = jQuery(hash.t);
		var $modal = jQuery(hash.w);
		var $modalContent = jQuery("iframe", $modal);

		$modalContent.html('').attr('src', '/wp-content/themes/facesoflawsuitabuse/_subscribe.php');
		hash.w.show();
	}

	jQuery(".jqmClose").click(function(e) {
		parent.jQuery("#modal-window").jqmHide();
		e.preventDefault();
	});

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

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

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

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

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

    // Position slider out of view:
    jQuery('#spotlight-container').css('left', 2 * num * -story_width + 'px');
    jQuery('#spotlight-container').animate({opacity:1}, 500, function(){
        jQuery('#spotlight-container').animate({left: "0"}, 1250, 'easeOutExpo', updateButtons());
    });
    
    // Add Left / Right Buttons:
    jQuery('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
    jQuery('#slide-left').bind('click', function() {
        if (pos > 0) {
            advanceSlider(--pos);
        }
        return false;
    });

    // Click behavior for Next / Right
    jQuery('#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);
    */

});

