var aim_interstitial_popup_status = 0;
var targettext= 'initial'

// Show the popup
function aim_interstitial_show_popup() {
    if(aim_interstitial_popup_status == 0) {

            jq("#aim_interstitial_background").css({
                "opacity": "0.65"

            });
            jq("#aim_interstitial_background").fadeIn("slow");
            jq("#archetypes-fieldname-requested-url").css({"display":"none"});
            jq("#aim_interstitial_container").slideDown("slow");

           // Set the focus
           jq("input.firstToFocus:first").focus()
           jq("#requested-url").val(targettext);

           aim_interstitial_popup_status = 1;
    }
}

// Hide the popup
function aim_interstitial_hide_popup() {
    // Hide the popup if it is currently displayed
    if(aim_interstitial_popup_status == 1){
        jq("#aim_interstitial_background").fadeOut("slow");
        jq("#aim_interstitial_container").slideUp("slow");
        aim_interstitial_popup_status = 0;
    }
}

// Position the popup
function aim_interstitial_position_popup() {
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = jq("#aim_interstitial_container").height();
    var popupWidth = jq("#aim_interstitial_container").width();
    // Set the CSS to centerit
    var scrolltop = document.documentElement.scrollTop;
    var top = windowHeight*0.3-popupHeight/2+scrolltop;
    var left = windowWidth/2-popupWidth/2;
    if (top < 10) top = 10; // Don't go above the top of the window
    jq("#aim_interstitial_container").css({
        "position": "absolute",
        "top": top,
        "left": left,
        "z-index": "1001",
        "display": "none",
        "background": "white",
        "padding": "10px"
    });

    //only need force for IE6
    jq("#aim_interstitial_background").css({
        "height": windowHeight,
        "width": windowWidth,
        "position":"absolute",
        "top": scrolltop,
        "left": "0",
        "z-index": "1000",
        "background": "black",
        "display": "none"
    });
}

function aim_interstitial_do_popup() {
    aim_interstitial_position_popup();
    aim_interstitial_show_popup();
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_name;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


// Set up jQuery actions
jq(document).ready(function() {

    jq("a.popup").each(function(i){
        if (Get_Cookie('__ac')==null){
        jq(this).click(function(event) {        
            event.preventDefault();
            targettext = jq(this).attr('href');
            aim_interstitial_do_popup();
        });}
    });

    // Ways to close the popup
    // Click the X
    jq("#aim_interstitial_close").click(function(){
        aim_interstitial_hide_popup();
    });
    // Click the background
    jq("#aim_interstitial_background").click(function(){
        aim_interstitial_hide_popup();
    });
    // Press escape
    jq(document).keypress(function(e){
        if(e.keyCode==27)
            aim_interstitial_hide_popup();
    });
});



