// JavaScript Document
cms.components.h_popup = {}; //create namespace for this component

//move the popup layers to the top of the page
$(document).ready( function(){
	$('#cms_components_h_popup_window').parent().css({'font-size':'0px'});//ie7 fix, empty container gets height automatically added even if div is empty
	$('body').prepend($('#cms_components_h_popup_background'));
	$('body').prepend($('#cms_components_h_popup_window'));							
});

//function to close the popup window
cms.components.h_popup.close = function() {
	$('#cms_components_h_popup_background').fadeOut('fast');
	$('#cms_components_h_popup_window').fadeOut('fast');
	$('#cms_components_h_popup_content').empty();
	return false;
}

cms.addDomLoad(function(){							
	
	$('#cms_components_h_popup_background').click(function(){	
			cms.components.h_popup.close();												
	});
	
	
	$('.cms_components_h_popup_link',cms.loadObject).each(function(){
		
		var popupLink = $(this);
		var path = popupLink.attr('href');
				
		popupLink.click(function(){		

			//don't load external scripts
			if(path.indexOf('http://',0)!=0){

				$.ajax({
					type: "GET",
					url: path,
					success: function(msg){
											
						$('#cms_components_h_popup_content').html(msg);
						
						//size of the viewport 
						var w = $(window).width();
						var h = $(window).height();
						
						//find the size of the layer #cms_popup_window
						var cw = $('#cms_components_h_popup_window').width();
						var ch = $('#cms_components_h_popup_window').height();
						//console.log(cw);
					
						//centre the window
						$('#cms_components_h_popup_window').css('left',(w/2)-(cw/2)+'px'); //(w/2)-(cw/2);
						$('#cms_components_h_popup_window').css('top',(h/2)-(ch/2)+'px'); //(h/2)-(ch/2);
						
						$('#cms_components_h_popup_background').fadeIn('fast');
						$('#cms_components_h_popup_window').fadeIn('fast');
					}
				});
				return false;
			}
			
/*			else{
				var url ='www.lancsngfl.ac.uk';
				$('#cms_components_h_popup_content').html('<iframe frameBorder="0" style="display:none;" width="100%" height="100%" id="popupiframe" marginHeight="0" marginWidth="0" name="cms_popupiframe" scrolling="yes" src="'+url+'"></iframe>');
				$('#cms_components_h_popup_background').fadeIn('fast');
				$('#cms_components_h_popup_window').fadeIn('fast');
			}*/
			
		});

	});

});
