// jQuery mod functions
jQuery.fn.center = function(){
	var ltRtPadding = parseInt((this.css('padding-left').replace(/px/g, ""))) + parseInt((this.css('padding-right').replace(/px/g, "")));
    if (isNaN(ltRtPadding)) { ltRtPadding = 0; }

    var topBotPadding = parseInt((this.css('padding-top').replace(/px/g, ""))) + parseInt((this.css('padding-bottom').replace(/px/g, "")));
    if (isNaN(topBotPadding)) { topBotPadding = 0; }

	var ltRtBorder = parseInt((this.css('border-left-width').replace(/px/g, ""))) + parseInt((this.css('border-right-width').replace(/px/g, "")));
    if (isNaN(ltRtBorder)) { ltRtBorder = 0; }

    var topBotBorder = parseInt((this.css('border-top-width').replace(/px/g, ""))) + parseInt((this.css('border-bottom-width').replace(/px/g, "")));
    if (isNaN(topBotBorder)) { topBotBorder = 0; }

	this.css("position", "absolute");
    //this.css("top", ($(window).height() - (this.height() + topBotPadding + topBotBorder)) / 2 + $(window).scrollTop() + "px");
	this.css("top", $(window).scrollTop() + 146 + "px");
	this.css("left", ($(window).width() - (this.width() + ltRtPadding + ltRtBorder)) / 2 + $(window).scrollLeft() + "px");

    return this;
}

function getImgSize(imgSrc){
    var newImg = new Image();
    newImg.src = imgSrc;
    var height = newImg.height;
    var width = newImg.width;
    return width;
}

//Modal Window functions
function launchWindow(id){

    //Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    
    //Set height and width to mask to fill up the whole screen
    $('#mask').css({
        'width': maskWidth,
        'height': maskHeight
    });
    
    //transition effect
    $('#mask').fadeTo("slow", 0.6);
    $('#mask').fadeIn(500);
    
    //Set the popup window to center
    $(id).center();
    
    //transition effect
    $(id).fadeIn(2000);
}

function launchPopWindow(obj){
    $('#popup_content').html('');
    var popID = $(obj).attr('rel'); //Get Popup Name
    var popURL = $(obj).attr('href'); //Get Popup href to define size
    
	//Pull Query & Variables from href URL
    var query = popURL.split('?');
    var dim = query[1].split('&');
    var popWidth = dim[0].split('=')[1];
    var popHeight = dim[1].split('=')[1];
    var popPage = dim[2].split('=')[1];
    var popElement = dim[3].split('=')[1];

    if (popElement == '') {
    	$('#popup_content').load('/' + popPage);
    }
    else {
        $('#popup_content').load('/' + popPage + ' ' + popElement);
    }
    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({
        'width': Number(popWidth),
        'height': Number(popHeight)
    }).prepend('<a href="#" class="closePop"><img src="/img/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
    
    $('#' + popID).center();

    //Fade in Background
    //$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    //$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
    
    return false;
}

function launchSWFWindow(src){
	
	loadPlayer(src);
    
	//Get the screen height and width
    var maskHeight = $(document).height();
    var maskWidth = $(window).width();
    
    //Set heigth and width to mask to fill up the whole screen
    $('#mask').css({
        'width': maskWidth,
        'height': maskHeight
    });
    
    //transition effect
    $('#mask').fadeTo("slow", 0.6);
    $('#mask').fadeIn(500);
    
    //Set the popup window to center
    $('#modalPlayerWin').center();
    
    //transition effect
    $('#modalPlayerWin').fadeIn(2000);
}

function loadPlayer(moviepath){	
	
	var html5type = "mp4";
	if ($.browser.webkit) {
		html5type = "mp4";
	}
	if ($.browser.mozilla) {
		html5type = "ogv";
	}
	
	var hasFlash = swfobject.getFlashPlayerVersion();
	if (hasFlash.major > 0) {
		
		flowplayer(
			"player", 
			"/swf/flowplayer-3.2.7.swf", 
			moviepath
		);
	    
	} else {
		
		if ($.browser.mozilla) {
			moviepath = moviepath.replace(/mp4/i, html5type);
		}
		$('#modalSWF').html('<div id="non-flash-html5"><video id="non-flash-video" controls="controls" width="640" height="480"  preload="auto"><source id="webkit-movie" src="'+moviepath+'" /></video></div>');
   		document.getElementById('non-flash-video').play();
		$('#modalPlayerWin a.close').bind('click', function() {
			document.getElementById('non-flash-video').pause();
		});	  	
	
	}
   
  }

// Deprecated
/*
function loadPlayer(moviepath){	
	
	var html5type = "mp4";
	if ($.browser.webkit) {
		html5type = "mp4";
	}
	if ($.browser.mozilla) {
		html5type = "ogv";
	}
	
	var hasFlash = swfobject.getFlashPlayerVersion();
	if (hasFlash.major > 0) {
		
		// Set the swfobject data
	    params = {};
	    params.quality = 'high';
	    params.wmode = 'opaque';
	    params.salign = 't';
	    params.allowFullScreen = true;
		params.bgcolor = '#003258';
	    vars = {};
	    vars.moviepath = moviepath;
	    //vars.transpath = transpath;
	    swfobject.embedSWF("/swf/Regeneron-PlayerFlash-R1.swf", "modalSWF", "640", "510", "8", "/js/expressInstall.swf", vars, params);
		
	    
	} else {
		
		if ($.browser.mozilla) {
			moviepath = moviepath.replace(/mp4/i, html5type);
		}
		$('#modalSWF').html('<div id="non-flash-html5"><video id="non-flash-video" controls="controls" width="640" height="480"  preload="auto"><source id="webkit-movie" src="'+moviepath+'" /></video></div>');
   		document.getElementById('non-flash-video').play();
		$('#modalPlayerWin a.close').bind('click', function() {
			document.getElementById('non-flash-video').pause();
		});	  	
	
	}
   
  }*/
  
function lightsOut(){
	if($.mask.isLoaded() == 'full') {
		$.mask.close();
	} else {
		$("#video-container").expose({
	        color: '#000',
	        onBeforeLoad: function(){
	            this.getMask().appendTo('body');			
	        }
	    });
	}    
}

function launchIMGWindow(id, src, link){

    $(id + ' #modalIMG').html('<a href="' + link + '"><img class="modal-image" src="" /></a>');
    
    $(id + ' #modalIMG img.modal-image').attr('src', src).load(function(){
    
        imgWidth = getImgSize(src);
        
        $(id + ' #modalContent').css("width", imgWidth);
        $(id + ' #modalIMG').css("width", imgWidth);
        
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({
            'width': maskWidth,
            'height': maskHeight
        });
        
        //transition effect
        $('#mask').fadeTo("slow", 0.6);
        $('#mask').fadeIn(500);
        
        $(id).center();
        
        //transition effect
        $(id).fadeIn(2000);
        
    });
}

