/**
 * @Author: Antoine E. Hall
 */


/** The section of functions below are for the certified site modal window */
﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
//
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#background_popup").css({
			"opacity": "0.7"
		});
        $("#background_popup").fadeIn("slow");
		$("#popup_certified_site").fadeIn("slow");
        //$("#popup_certified_site").load("/search/certified_site.php").fadeIn("slow");

		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#background_popup").fadeOut("slow");
		$("#popup_certified_site").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupHeight = $("#popup_certified_site").height();
	var popupWidth = $("#popup_certified_site").width();

    //centering
    $("#popup_certified_site").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#background_popup").css({
		"height": windowHeight
	});

}
// End modal window functions


// prepare the form when the DOM is ready
$(function() {

    // bind building form using ajaxForm
    $('#building_search_form').ajaxForm({
        // target identifies the element(s) to update with the server response
        target: '#search_results',

        // success identifies the function to invoke when the server response
        // has been received; here we apply a fade-in effect to the new content
        success: function() {
            $('#search_results').fadeIn('slow');
            $('a.back_to_search').fadeIn('slow');
            $('#building_search').hide();
            $('#site_search').hide();
        }
    });

    // bind site form using ajaxForm
    $('#site_search_form').ajaxForm({
        // target identifies the element(s) to update with the server response
        target: '#search_results',

        // success identifies the function to invoke when the server response
        // has been received; here we apply a fade-in effect to the new content
        success: function() {
            $('#search_results').fadeIn('slow');
            $('a.back_to_search').fadeIn('slow');
            $('#building_search').hide();
            $('#site_search').hide();
        }
    });

    // bind building map search using ajaxForm
    $('#view_buildings_map').click(function() {

        $('#map_iframe').attr('src', '/wp-content/themes/mcpip/search/view_map.php?q=buildings');
        $('#map_iframe').fadeIn('slow');
        $('a.back_to_search').fadeIn('slow');
        $('#building_search').hide();
        $('#site_search').hide();
    });


    // bind site map search using ajaxForm
    $('#view_sites_map').click(function() {

        $('#map_iframe').attr('src', '/wp-content/themes/mcpip/search/view_map.php?q=sites');
        $('#map_iframe').fadeIn('slow');
        $('a.back_to_search').fadeIn('slow');
        $('#building_search').hide();
        $('#site_search').hide();
    });


    // Goes back to search
    $('a.back_to_search').click(function() {
         $('#search_results').hide();
         $('#view_maps').hide();
         $('a.back_to_search').hide();
         $('#building_search').show();
         $('#site_search').show()

         $('#map_iframe').hide();
         $('#map_iframe').attr('src', '');
    });
    

    // The section of functions below are for the certified site modal window

    //LOADING POPUP
	//Click the link event!
	$("#certified_site_link").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});

	//CLOSING POPUP
	//Click the x event!
	$("#popup_certified_site_close").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#background_popup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
    // The section of functions below are for the certified site modal window
});


