﻿//************************************************************************************* 
// File     :   tsob_view_adsample.js
// Version  :   1.0
// Requires :   mf_domLibrary_0.1.js, prototype.js
// Author   :   Kyle Weems (ksw)
// Origin   :   mindfly.com
// Created  :   December 17, 2007
// Modified :   December 17, 2007 (ksw)
// Purpose  :   
//*************************************************************************************

// Version History (only for changes of at least 0.0.1 in version number)
// ===============
// Version 1.0   (12/17/2007) - Basic functionality.


//=============================================================================================
// showAdSample()
//=============================================================================================
// Show the popup for the ad #id. 
function showAdSample(id)
{
    hideAllSamples();
    var popup = 'AdExamplePopup_' + id;
    $(popup).removeClassName('displayNone');
    $(popup).addClassName('displayBlock');
    placeAdSample(id);
} // end of showAdSample()


//=============================================================================================
// hideAdSample()
//=============================================================================================
// Hide the popup for the ad #id.
function hideAdSample(id)
{
    var popup = 'AdExamplePopup_' + id;
    $(popup).removeClassName('displayBlock');
    $(popup).addClassName('displayNone');
} // end of hideAdSample()


//=============================================================================================
// hideAllSamples()
//=============================================================================================
// Hide all the sample popups.
function hideAllSamples()
{

    // create a variable 'popup'
    var popup = "";
    // Enter a loop to select each popup
    for(i=1;i<10;i++)
    {
        // set popup to equal the id of the popup with the #i.
        popup = "AdExamplePopup_" + i;
        // set the popup's classes to displayNone.
        $(popup).removeClassName("displayBlock");
        $(popup).addClassName("displayNone");
    } // end for loop

} // end of hideAllSamples()

//=============================================================================================
// placeAdSample()
//=============================================================================================
// Sets the position of a popup in relation to the anchor that created it.
function placeAdSample(id)
{
    // set up object names for the popup element and the link element that it relates to.
    popup = "AdExamplePopup_" + id;
    linksource = "adDetails_" + id;
    // IE isn't working properly with Position.cumulativeOffset, so if IE, calculate differently.
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        // The Y value from getBoundingClient needs to be adjusted by the scroll value, client offset, and the margin of the body element.
        var yOffset = document.documentElement.scrollTop - (parseInt(document.documentElement.clientTop));
        var winPos  = $(linksource).getBoundingClientRect();
        var y       = winPos.top + yOffset ;
    }
    else
    {
        // Other broswers don't suffer the same issue. Treat them normally.
        var object  = linksource;
        var winPos  = Position.cumulativeOffset($(object));
        var y       = winPos[1]; //getElementTop(object);
    }
    // 
    if(id < 4)            { y -= 400; }
    if(id == 4)           { y -= 300; }
    if(id > 4 && id < 7)  { y -= 400; }
    if(id > 6 && id < 10) { y -= 450; }
    // Assign the position values to the popup.    
    $(popup).style.position = "absolute";
    $(popup).style.top      = y + "px";
    $(popup).style.zIndex   = "1000";
    // Determine if the placement puts part of the popup off the bottom of the screen. If so, correct it.
    var windowSize  = Position.getWindowSize();
    var windowY     = windowSize[1];
    var popupPage   = Position.page($(popup))
    var popupY      = popupPage[1] + $(popup).offsetHeight;
    if(popupY > windowY)
    {
        y -= $(popup).offsetHeight - 2;
        $(popup).style.top = y + "px";
    }
    
    $(popup).style.left = "0px";
    //$(popup).style.left = Math.floor(($('content_main_body').offsetWidth - $(popup).offsetWidth) / 2) + 'px';
    
    // Determine if the placement puts part of the popup off the right of the screen. If so, hide it.
    var windowX = windowSize[0];
    var popupX  = popupPage[0]  + $(popup).offsetWidth;
    if(popupX > windowX) { hidePopup(elem); }

} // end of placeAdSample()


//=============================================================================================
// Position.getWindowSize()
//=============================================================================================
// extension of Protype's Position class. Returns the size of the browser window as an array of [width, height].
Position.getWindowSize = function(w) 
{
        w = w ? w : window;
        var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        return [width, height]
} // end of Position.getWindowSize()
