// Functions to modify on the fly the height and width of the DIV that encapsulates the flash
// banner. These functions are called from within the banner itself to tell the encapsulating
// DIV to resize appropriately to fit the dimensions of the current flash banner being shown.
function setFlashWidth(divid, newW)
{
    document.getElementById(divid).style.width = newW+"px";
}

function setFlashHeight(divid, newH)
{
    document.getElementById(divid).style.height = newH+"px";
}

function setFlashSize(divid, newW, newH)
{
    setFlashWidth(divid, newW);
    setFlashHeight(divid, newH);
}

function MU_AC_Generateobj(objAttrs, params, embedAttrs)
{
    var str = '<object ';
    for (var i in objAttrs)
        if( i != 'name' )
        {
            str += i + '="' + objAttrs[i] + '" ';
        }
    str += '>';
    for (var i in params)
        str += '<param name="' + i + '" value="' + params[i] + '" /> ';
    str += '<embed ';
    for (var i in embedAttrs)
        if( i != 'id' )
        {
            str += i + '="' + embedAttrs[i] + '" ';
        }
    str += ' ></embed></object>';

    document.write(str);
}

function MU_AC_FL_RunContent(){
  var ret =
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  MU_AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function get_flash_movie(movie_name)
{
    if( window.document[movie_name] )
    {
        return window.document[movie_name];
    }
    if( navigator.appName.indexOf("Microsoft Internet") == -1 )
    {
        if (document.embeds && document.embeds[movie_name])
        {
            return document.embeds[movie_name];
        }
    }
    else
    {
        return document.getElementById(movie_name);
    }
}

var expandable_ad_element = null;
function expandable_advert( $width, $height, $alt_link, $alt_img, 
        $banner_name, $banner_name_expanded, $banner_width_expanded, $banner_height_expanded,
        $banner_directory, $div_width, $div_height )
{
    var hasRightVersion = false;
    hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

    var banner_src_noext = $banner_directory + $banner_name;
    var banner_src_noext_expanded = $banner_directory + $banner_name_expanded;

    document.write('    <div id="ad_layer_expanded" style="width: ' + 
        $banner_width_expanded + 'px; height: ' + $banner_height_expanded + 
        'px; padding: 0px; margin: 0px; z-index: 10; overflow: hidden; display: none; position: absolute; right: 0px; clear: none; visibility: hidden; left: auto; top: auto;" ' +
        'onmouseover="show_expandable(\'' + $banner_name_expanded + '\');" ' +
        'onmouseout="start_closing_expandable(\'' + $banner_name_expanded + '\');">');

    if(hasRightVersion)
    {  // if we've detected an acceptable version
        if (AC_FL_RunContent == 0)
        {
            // Show the client side error
            document.write('Failed to load Shockwave ActiveX content');
        }
        else
        {
            MU_AC_FL_RunContent('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
                            'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
                            'src', banner_src_noext_expanded,
                            'movie', banner_src_noext_expanded,
                            'id', $banner_name_expanded,
                            'name', $banner_name_expanded,
                            'width', $banner_width_expanded,
                            'height', $banner_height_expanded,
                            'bgcolor', '#FFFFFF',
                            'wmode', 'transparent',
                            'allowScriptAccess','always',
                            'menu', 'false',
                            'swliveconnect', 'true'
            ); //end AC code
        }
    }
    document.write('    </div>');

    document.write('    <div id="ad_layer" style="width: ' + $div_width + 'px; height: ' +
        $div_height + 'px; padding: 0px; margin: 0px; z-index: 4; overflow: hidden; position: relative; right: 0px;"' +
        'onmouseover="show_expandable(\'' + $banner_name_expanded + '\');" ' +
        'onmouseout="start_closing_expandable(\'' + $banner_name_expanded + '\');">');

    if(hasRightVersion)
    {  // if we've detected an acceptable version
        if (AC_FL_RunContent == 0)
        {
            // Show the client side error
            document.write('Failed to load Shockwave ActiveX content');
        }
        else
        {
            AC_FL_RunContent('codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
                            'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
                            'src', banner_src_noext,
                            'movie', banner_src_noext,
                            'name', $banner_name,
                            'width', $div_width,
                            'height', $div_height,
                            'bgcolor', '#FFFFFF',
                            'wmode', 'transparent',
                            'allowScriptAccess','always',
                            'menu', 'false',
                            'swliveconnect', 'true'
            ); //end AC code
        }
    }
    else
    {  // flash is too old or we can't detect the plugin
        var alternateContent = '<a href="' + $alt_link + 
            '" target="_blank" shape="rect"><img src="' + $alt_img + '" border="0" width="' + 
            $width + '" height="' + $height + '" /></a>';
        document.write(alternateContent);  // insert non-flash content
    }

    // Output the container DIVs closing tags
    document.write('    </div>');
}

var timeoutid = null;
function show_expandable( $banner_name_expanded )
{
    normal = document.getElementById('ad_layer');
    expandable = document.getElementById('ad_layer_expanded');

    expandable.style["display"] = "inline";

    // Recalculate where the position should be.
    panel_top = calculateOffsetTop(normal);
    panel_left = calculateOffsetLeft(normal) - parseInt(expandable.style['width']) +
        parseInt(normal.style['width']);

    expandable.style.top = panel_top + "px";
    expandable.style.left = panel_left + "px";
    expandable.style.right = "auto";

    expandable.style["position"] = "absolute";
    expandable.style["visibility"] = "visible";

    if( timeoutid != null )
    {
        window.clearTimeout(timeoutid);
    }
    banner = get_flash_movie($banner_name_expanded);
    try
    {
        banner.Play();
    }
    catch(e)
    {
        // pass
    }
}

function start_closing_expandable( $banner_name_expanded )
{
    timeoutid = window.setTimeout("close_expandable( '" + $banner_name_expanded + "')", 500);
}

function close_expandable( $banner_name_expanded )
{
    // Find the div
    expandable = document.getElementById('ad_layer_expanded');
    expandable.style['display'] = 'none';
    expandable.style['visibility']="hidden";
    expandable_ad = window.document[$banner_name_expanded];
    timeoutid = null;

    banner = get_flash_movie($banner_name_expanded);
    banner.Rewind();
}

// Called as:
// calculateOffsetLeft(_inputField)
// was ob
function calculateOffsetLeft(r){
  return Ya(r,"offsetLeft")
}

// Called as:
// calculateOffsetTop(_inputField)
// Was Qb...
function calculateOffsetTop(r){
  return Ya(r,"offsetTop")
}

function Ya(r,attr){
  var kb=0;
  while(r){
    kb+=r[attr];
    r=r.offsetParent
  }
  return kb
}


