/**************************************************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!

//@altered for dynamic addition of the popup to the body by AppStract
/**************************************************************/
jQuery.noConflict();

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	//LOADING POPUP
	initPopup();
});

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var popupBoxId = "popup-content";
var popupBackgroundId = "popup-background";
var popupCloseId = "popup-content-close";
var popupCloseImage = "/media/ClassicGolf/Images/Close.png";

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		jQuery("#" + popupBackgroundId).css({
			"opacity": "0.7"
		});
		jQuery("#" + popupBackgroundId).fadeIn("slow");
		jQuery("#" + popupBoxId).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery("#" + popupBackgroundId).fadeOut("slow", function(){
            jQuery("body #" + popupBackgroundId).remove();
		});
		jQuery("#" + popupBoxId).fadeOut("slow", function(){
            jQuery("body #" + popupBoxId).remove();
        });
        popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight;

	if(jQuery.browser.msie)
		windowHeight = document.documentElement.clientHeight;
	else
		windowHeight = window.innerHeight;

	var popupHeight = jQuery("#" + popupBoxId).height();
	var popupWidth = jQuery("#" + popupBoxId).width();
    	var contentHeight = jQuery("body").height(); 
	var backgroundHeight;

        //centering

	jQuery("#" + popupBoxId).css({
		"position": "absolute",
		"top": (windowHeight/2)-(popupHeight/2),
		"left": (windowWidth/2)-(popupWidth/2)
	});

	//only need force for IE6
	
    if(contentHeight < windowHeight)
        backgroundHeight = windowHeight;
    else
        backgroundHeight = contentHeight;
    
	jQuery("#" + popupBackgroundId).css({
		"height": backgroundHeight
	});
}

//Initializing the popup events
function initPopup(){
    //Click the button event!
	jQuery("#hotel-facts .preview-videos a").click(function(){
            var videoId = jQuery(this).attr("rel");
        	//Insert your popup HTML in the following variable, which will be added to the popup's content area
        	var popupContent;
                popupContent = "<object width='640' height='480'><param name='allowfullscreen' value='true' /><param name='allowscriptaccess' value='always' /><param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id=" + videoId + "&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1' /><embed src='http://vimeo.com/moogaloop.swf?clip_id=" + videoId + "&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1' type='application/x-shockwave-flash' allowfullscreen='true' allowscriptaccess='always' width='640' height='480'></embed></object>";
		displayPopup(popupContent);
        });
}

function displayPopup(popupContent){
	jQuery("body").append("<div id='" + popupBoxId + "'><div id='" + popupCloseId + "'><a href='#'><img src='" + popupCloseImage + "' alt='x'/></a></div><div class='content'>" + popupContent + "</div></div><div id='" + popupBackgroundId + "'></div>");
    		
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
        	
        //CLOSING POPUP
        //Click the x event!
        jQuery("#" + popupCloseId + " a").click(function(){
            //alert('close');
            disablePopup();
        });
        
        //Click out event!
        jQuery("#" + popupBackgroundId).click(function(){
            disablePopup();
        });
    
        //Press Escape event!
        jQuery(document).keypress(function(e){
            if(e.keyCode==27 && popupStatus==1){
                disablePopup();
            }
        });
}
