
// vertical positioning in the viewport

(function($){
  $.fn.vCenter = function(options) {
    var pos = {
      sTop : function() {
        return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
      },
      wHeight : function() {
        return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
      }
    };
    return this.each(function(index) {
      if (index == 0) {
        var $this = $(this);
        var elHeight = $this.height();
		    var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
        $this.css({
          position: 'absolute',
          marginTop: '0',
          top: elTop
        });


      }
    });
  };

})(jQuery);

$(document).ready(function(){
// add a click event
	$('a[@href$=flv]').click(function(){
		movieLink = $(this).attr("href");
		if (!movieLink.match('wp-content'))
		{
			movieLink = "/wp-content/themes/cufoundation/images" + movieLink
		}

		window.startOverlay();
		return false;
	});
});

function startOverlay(callback) {
//add the elements to the dom
	$("body")
		.append('<iframe id="overlay-iframe" frameborder="0" src="javascript:false;"></iframe><div class="overlay"></div><div class="container"><div id="test" class="light-wrap"></div></div>')
	//	.css({"overflow-y":"hidden"});

//animate the semitransparant layer
	$('.overlay').css({opacity : "0.6"});

//add the lightbox image to the DOM
    if($("#checkout").html()){
        $(".light-wrap").html('<div id="order" style="text-align: center;">Please wait for your order to finish processing.<br />Please do not click on anything untill your order is processed</div>');
        $('.container').vCenter();
    }
    else
        $(".light-wrap").html('<div id="closebutton"><p>CLOSE</p></div><div id="movie"></div>');
//position it correctly after downloading
//	$(".container img").load(function() {
	//	var imgWidth = $(".container img").width();
	//	var imgHeight = $(".container img").height();
		$(".container")
			.animate({"opacity":"1"}, 400, "linear", function() {
                if(!$("#checkout").html()){
                    $("#closebutton").slideDown(function() {
                         movie();
                    });
                }else
                    callback();
			});

// you need to initiate the removeoverlay function here, otherwise it will not execute.
		window.removeOverlay();
		
	//});
}

function movie() {
	var flashvars = {
		media: movieLink
	};	
	swfobject.embedSWF("/wp-content/themes/cufoundation/images/global/videoPlayer.swf", "movie", "407", "254", "7", "/wp-content/themes/cufoundation/scripts/swfobject/expressInstall.swf", flashvars);
}

function removeOverlay() {
// allow users to be able to close the lightbox
	$(".overlay, #closebutton").click(function(){
        if(!$("#checkout").html()){
			$(".light-wrap").css({"background":"#fff"});
			$("#movie").remove();
			$("#closebutton").slideUp(function() {
				$(".container, .overlay").animate({"opacity":"0"}, 200, "linear", function(){
					$(".container, .overlay, #overlay-iframe").remove();
					$("body").css({"overflow-y":"visible"});			
				});
			});
        }
	});
}
/* fin. */
