var i;
var scrollPosition;

function openModalWindow(index) {
	if (index != null && index != "") {
		i = index;
	}
	
	// PAUSE BANNER ON HOME PAGE
	$('#homeBanner').cycle('pause');
	
	//DISABLE PAGE SCROLLING
	 $('html').css({
		'overflow': 'hidden',
		'margin-top':-$(window).scrollTop()+'px'
	 }); 
	
	//SET OVERLAY TO FILL SCREEN
	scrollPosition = parseInt( $('html').css('margin-top').replace('-','') );

	$('#modalWindowOverlay').css({
		'top':$(document.body).scrollTop(),
		'width':$(window).width(),
		'height':$(window).height()
	})
	
	//SET MODAL DIMENSIONS
	$('.modalWindowWrapper').css({
		'height' : $('#modalWindowContent'+i).attr('data-modalHeight') + 'px',
		'width' : $('#modalWindowContent'+i).attr('data-modalWidth') + 'px',
		'margin-top' : '-' + ( $('#modalWindowContent'+i).attr('data-modalHeight') / 2 ) + 'px',
		'margin-left' : '-' + ( $('#modalWindowContent'+i).attr('data-modalWidth') / 2 ) + 'px',
		'top':$(document.body).scrollTop() + ($('#modalWindowOverlay').height() / 2 )
	});
	
	
	//SET MODAL iFRAME DIMENSIONS	
	$('.modalWindowContent, .modalWindowContent iframe').css({
		'height' : ( $('#modalWindowContent'+i).attr('data-modalHeight') - parseInt( $('.modalWindowContent').css('padding-top') )  - parseInt( $('.modalWindowContent').css('padding-bottom') ) - parseInt( $('.modalTitle').css('height') )) + 'px',
		'width' : ( $('#modalWindowContent'+i).attr('data-modalWidth') - ( parseInt( $('.modalWindowContent').css('padding-left') )  + parseInt( $('.modalWindowContent').css('padding-right') ) ) ) + 'px'
});
		
	//SHOW MODAL WINDOW
	$('#modalWindowOverlay').show();
	$('#modalWindowContent'+i).show();
}

$(document).ready(function() {

	//MODAL WINDOW
	
	$('.modalWindowBtn').click(function(event) {
		i = $(this).attr('id');
		i = i.replace('modalWindowBtn','');
		openModalWindow();
	});	
		
	//CLOSE MODAL WINDOW
	$('.modalClose, #modalWindowOverlay').click(function() {
		 $('html').css({
			'overflow': 'auto',
			'margin-top':'0px'
		 }); 
		$(window).unbind('scroll');
		$('#modalWindowOverlay').hide();
		$('#modalWindowContent'+i).hide();
		$(window).scrollTop(scrollPosition);
		
		$('#flashVideoContainer').html('<div id="flash"></div>');
		// RESUME BANNER ON HOME PAGE
		$('#homeBanner').cycle('resume');
	});
	
	
	$('.modalWindowWrapper').click(function(event){
		 event.stopPropagation();
	});

});

