$(document).ready(function () {
    
    $('a.poplight[href^=#]').click(function () {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).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]; //Gets the first query string value
        dotherealopo(popID, popURL, query, dim, popWidth);
        return false;
    });


    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function () { //When clicking on the close or fade layer...
        dotherealclose();
        return false;
    });

});

function dotherealclose() {
	$('#fade , .popup_block').fadeOut(function() {
		$('#fade, a.close').remove();  
		$("video").trigger("pause");
	}); //fade them both out
}

function dotherealopo(popID, popURL, query, dim, popWidth){
	$("video").trigger("play");
	$("video").bind("ended", function(){
		dotherealclose();
	});		
	//alert("popID:"+popID+" popURL:"+query+" dim:"+dim+" popWidth:"+popWidth);
	$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="assets/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" style="border:0px;" /></a>');
	var popMargTop = ($('#' + popID).height() + 80) / 2;
	var popMargLeft = ($('#' + popID).width() + 80) / 2;
	$('#' + popID).css({ 
		'margin-top' : -popMargTop,
		'margin-left' : -popMargLeft
	});
	$('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 
}

