var strSrc;
var strClosingCoords;
var strBannerCoords;
var w;
var h;

function __FlyOver()
{  
    //banner base code should be written first.    
    initBannerParams();
    WriteBannerHTML();    
    
    //ExecuteFlyOver only after page loads and banner
    //code is written to the document!!!
    BannersControlHelper.AttachEvent(window,'onload',ExecuteFlyOver);
}

function ExecuteFlyOver()
{
    if(ValidateShow())
    {
        ExecuteFloater();
    }
}


//Validates whether show the bunner or not
function ValidateShow()
{

    //preview mode always shows banner
    if (typeof __preview == "string")
        return true;

    //if the ctid var was defined, use the TPI to check if toolbar
    //is already installed in order to disable the banner in such case.  
    if (typeof __ctid =="string")
    { 
    
      var oToolbar = new TPI.Toolbar(__ctid);       
      var oReturn = oToolbar.IsVisible();            
      if (oReturn.returnValue   ==  true)//toolbar is installed and visible
        return false
    }
        
    //first validate period exposure
    if (BannersControlHelper.ReadCookie("FlyOverExposed") == "1")
        return false;
        
    //Validate cookies are enabled on the user's browser
    BannersControlHelper.SetCookie("FlyOverTester","1",1);    
   
    if(BannersControlHelper.ReadCookie("FlyOverTester") == "1")
    {
        //Cookies Enabled on user's browser
        if (BannersControlHelper.ReadCookie("FlyOverExposed") == "1")
        {
           return false;
        }            
        BannersControlHelper.SetCookie("FlyOverExposed","1",7);                    
        return true;             
    }
    else //Cookies disabled on user's browser
    {
        return false;
    }
}

//v1 calls to the js files need  the TPI be written from this script,
// therefore it will not be written from the ascx.

function get_ApiPath() {
    if (typeof __apiPath == 'undefined')
        return 'http://www.conduit.com/Api/ToolbarApi.js';
    return __apiPath + 'ToolbarApi.js';
}


function WriteToolbarAPIRequest() {
    document.write("<script src=\"" + get_ApiPath() + "\"></script>");
    //document.write("<script src=\"" + __baseURL + "/Api/ToolbarApi.js\"></script>");
}
  

function WriteBannerHTML()
{    
    WriteToolbarAPIRequest();
    document.write("<img id=\"__flyImg\"  style=\"display:none;cursor:pointer;cursor:hand;position:absolute\"  onmouseover =\"ToggleChip(this.id)\"  onmouseout =\"ToggleChip(this.id)\"/>");
    document.write("<MAP NAME=\"" + __bannerId + "\" z-index=\"10000\" >");
    document.write("<AREA SHAPE=CIRCLE COORDS=\"" + strClosingCoords + "\" onmouseover=\"StopChip()\" HREF=\"javascript:CloseFloatingBanner()\" ALT=\"Close\">");
    document.write("<AREA SHAPE=RECT COORDS=\"" + strBannerCoords + "\" onmouseover=\"StopChip()\" onclick=\"CloseFloatingBanner()\" HREF=\"" + __downloadUrl + "\" >");
    document.write("</MAP>");
}

function ExecuteFloater()
{
    //preloader
    img  = new Image();
    var strImage =     DetectBannerImageSource();
    img.setAttribute("src",strImage);

    
    var __flyImg    =   document.getElementById("__flyImg")

    //object data
    __flyImg.style.display='inline';
    __flyImg.setAttribute("src",strImage);
    __flyImg.useMap     =  "#" + __bannerId;
    __flyImg.border     =   0;
    __flyimage          =   new Chip("__flyImg",w,h);

    movechip("__flyimage");
}

function DetectBannerImageSource()
{
   
    if (typeof __bannerImageUrl == "string")
        return __bannerImageUrl;
    
    return __baseUrl + "/Banners/" + __bannerId;
}

function initBannerParams()
{
    switch (__bannerId)
    {
        case "floater1.gif","FlyOver1":
        {
           w    =   154;
           h    =   177;
           strClosingCoords = "124,12,12";
           strBannerCoords = "0,26,153,189";
           break;
        }
        case "floater2.gif","FlyOver2":
        {
           w    =   214;
           h    =   170;
           strClosingCoords = "197,13,13";
           strBannerCoords = "0,28,213,169"; 
           break;           
        }
        default :
        {
           var bannerId = "floater1.gif";
           w    =   154;
           h    =   177;
           strClosingCoords = "124,12,12";
           strBannerCoords = "0,26,153,189";        
            
        }
    }
    
}


var BannersControlHelper = 
{     	
	IsIE : function()
	{
		return (window.navigator.userAgent.indexOf("MSIE") > 0);
	},
	IsIE7 : function()
	{
		return (window.navigator.userAgent.indexOf("MSIE 7.0") > 0);
	},
	IsIE6 : function()
	{   
	    var regexIE6 = /MSIE ([0-6])/;
		return (regexIE6.test(window.navigator.userAgent));
	},
	AttachEvent : function(obj, eventName, delegate) 
	{
		if  (typeof( obj.addEventListener ) != 'undefined' ) 
		{
			obj.addEventListener(BannersControlHelper.GetEventName(eventName),delegate,false);
		} 
		else {
			obj.attachEvent(eventName,delegate);
		}
	},
	DetachEvent : function(obj,eventName,delegate)
	{
		if (obj.removeEventListener) {
			obj.removeEventListener(eventName,delegate, true);
		}
		else if (obj.detachEvent) {
			obj.detachEvent(eventName,delegate);
		}
	},
	GetEventName : function (name)
	{
		return (name.indexOf('on') == 0) ? name.substring(2,name.length) : name;
	},
	SetCookie:function (cookieName,cookieValue,nDays) 
	{
     var today = new Date();
     var expire = new Date();
     if (nDays==null || nDays==0) nDays=1;
     expire.setTime(today.getTime() + 3600000*24*nDays);
     document.cookie = cookieName+"="+escape(cookieValue)
                     + ";expires="+expire.toGMTString();
    },
    ReadCookie:function (cookieName)
    {
     var theCookie=""+document.cookie;
     var ind=theCookie.indexOf(cookieName);
     if (ind==-1 || cookieName=="") return ""; 
     var ind1=theCookie.indexOf(';',ind);
     if (ind1==-1) ind1=theCookie.length; 
     return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
    }	
}