﻿//CENTERING POPUP FUNCTION
jQuery.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    jQuery.extend(opt, options);
   
    return this.each(function(i) {
        var el = jQuery(this);
        var jWin = jQuery(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.appendTo("body");
            else
                el.appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}

/***************************/
//@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;

function loadThankYouPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.8"
		});
        //jQuery("#backgroundPopup").show();
		//jQuery("#ThankYoupopup").show();
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#ThankYoupopup").fadeIn("slow");
		jQuery('#ThankYoupopup').fadeIn(function(){     
		    this.style.removeAttribute("filter"); 
		});
		popupStatus = 1;
	}
}

function loadUnsubscribeConfirmPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.8"
		});
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#UnsubConfirmpopup").fadeIn("slow");
		jQuery('#UnsubConfirmpopup').fadeIn(function(){     
		    this.style.removeAttribute("filter"); 
		});
		popupStatus = 1;
	}
}

//disabling popup
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
        //jQuery("#backgroundPopup").hide();
		//jQuery("#ThankYoupopup").hide();
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#ThankYoupopup").fadeOut("slow");
		jQuery("#UnsubConfirmpopup").fadeOut("slow");
		popupStatus = 0;
	}
}


//center popup
function centerPopup(){
    jQuery("#ThankYoupopup").centerInClient({ container: window, forceAbsolute: true });
    //only need force for IE6
	jQuery("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

function centerUnsubConfirmPopup(){
    jQuery("#UnsubConfirmpopup").centerInClient({ container: window, forceAbsolute: true });
    //only need force for IE6
	jQuery("#backgroundPopup").css({
		"height": "100%",
		"width": "100%"
	});
	
}

//CONTROLLING EVENTS IN jQuery

 //jQuery(window).load(function(){
    //setTimeout(function () { 
		//center popup
		//centerPopup();
		//load popup
		//loadThankYouPopup();
    //}, 0000); 
 //});


jQuery(document).ready(function(){

    //CLOSING POPUPS
	//Click the x event!
	jQuery(".CApopupClose").click(function(){
		disablePopup();
	});
	//Click out event!
	jQuery("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	jQuery(document).keyup(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	
    //Subscribe Tip Hover Start	
    	
    // select all desired input fields and attach tooltips to them
    jQuery("#Template_header_SubscribeBtn").tooltip({
     
	    position: "bottom left",
	    offset: [0, 65],
	    effect: "fade",
	    opacity: 0.9,
	    delay: 0
     
    });

    var prm = Sys.WebForms.PageRequestManager.getInstance(); 
     
    prm.add_endRequest(function() { 
        // re-bind your jquery events here 
        
        jQuery("#Template_header_SubscribeBtn").tooltip({
     
	    position: "bottom left",
	    offset: [0, 65],
	    effect: "fade",
	    opacity: 0.9,
	    delay: 0
     
    });

});

//Subscribe Tip Hover End	
	

});
