/*--start of const--*/
var BANNER_IMAGES_BASE_URL = 'http://images.conduit-banners.com/';
var API_PATH = 'http://api.conduit.com/';
var LOG_SITE_URL = 'http://usage.conduit-banners.com/Logger/';
var LOG_ENABLED = true;
/*--end of const--*/
var bGlobalDontShowAgain = false;
function GetRealBodyWidth()
{
    // screen size
    var screenWidth = screen.width;
    var screenHeight = screen.height; 

    // body size
    var scrollWidth = document.body.scrollWidth;
    var scrollHeight = document.body.scrollHeight; 

    if(screenWidth < scrollWidth) 
    { 
    scrollWidth = screenWidth; 
    }

    if(screenHeight < scrollHeight) 
    { 
    scrollHeight = screenHeight; 
    }
    return scrollWidth;
}


function CommitTopSlide() {
   	if(EBStop)
	{
		var d = document.getElementById('EBTopBanner');		
	  //  d.style.width= parseInt(GetRealBodyWidth());
		d.style.left = 0+"px";
		if(d.style.display=="none")d.style.display="block";
	    if (d) {
	        if (__top < 0) {
	            __top += 3;
	            d.style.top = __top+"px";
				d.style.hieght=__top+"px";
	            setTimeout(CommitTopSlide, 25);
	        }
	    }
	}  
}


function EBPreVanishTopSlide(bDontShowAgain) 
{ 
    if (bGlobalDontShowAgain == true){bDontShowAgain=true}
    var iDays = bDontShowAgain ? DAYS_FOR_NEXT_APPEARANCE_DONT_SHOW_NOTIFICATION : DAYS_FOR_NEXT_APPEARANCE_CLOSE_BUTTON;    
    EBStop=true;
    BannersControlHelper.SetCookie("TopSliderExposed","1",iDays);   
    EBVanishTopSlide();
}


function EBDownloadToolbar()
{
    setTimeout(EBPreVanishTopSlide, 5000);
}

function DelayVanish()
{
     var objCheckTopScrollDontShowAgain = document.getElementById("checkTopScrollDontShowAgain");
     
     if(objCheckTopScrollDontShowAgain != null)
     {
        bGlobalDontShowAgain = objCheckTopScrollDontShowAgain.checked;
        if(bGlobalDontShowAgain)        
            setTimeout(EBPreVanishTopSlide, 5000)
     }
}

function EBVanishTopSlide() {
   	if(EBStop)
	{
		var d = document.getElementById('EBTopBanner');
		 d.style.left = 0+"px";
		if(d.style.display=="none")d.style.display="block";
	    if (d) {
	        if (__top > -77) {
	            __top -= 10;
	            d.style.top = __top+"px";
				d.style.hieght=__top+"px";
	            setTimeout(EBVanishTopSlide, 10);
	        }
	    }
	}    
}


function GetAlphaFilter()
{
    var ie = (document.all) ? 1 : 0;
    
    if(ie)
     return "filter:alpha(opacity=" + __opacity+")";
    else
     return "-moz-opacity: " + __opacity/ 100 ;
}



//definitions - internals
var __top                 = -66;
var EBStop                = true;
var DAYS_FOR_NEXT_APPEARANCE_CLOSE_BUTTON =7;
var DAYS_FOR_NEXT_APPEARANCE_DONT_SHOW_NOTIFICATION =365;



//Images
var __bgImage;
var __ImageArrow;
var __textImage;       
var __downloadBtn;        
var __downloadBtnOver;
var __downloadBtnDown;
var __closeBtn; 
var __closeBtnOver;
var __closeBtnDown;
var __dontShowCaption;
var __imagesPath;


function InitImagesDefinitions() {
    var flag = '0';
    
    if (typeof __baseURL == 'undefiend')
        var __baseURL;    
    
     if (!__baseURL || __baseURL == 'http://www.conduit.com/') {
        __baseURL = BANNER_IMAGES_BASE_URL;
        flag = '1';
    }
    else {
        flag = '2&baseUrl=' + __baseURL;
    }
    if (__baseURL.charAt[__baseURL.length] != '/')
        __baseURL += '/';
    __imagesPath        = __baseURL +  'TopSlider/'; //CHANGED BY SURIEL
    __bgImage           = __imagesPath + "strip_" + __selectedColor + ".gif";
    __ImageArrow        = __ImageArrow || __imagesPath + "arrow_" + __selectedColor + ".gif" + '?flag=' + flag;
    __textImage         = __textImage           || __imagesPath + "text_" + __selectedColor + ".gif";
    __downloadBtn       = __downloadBtn         || __imagesPath + "dnld_" + __selectedColor + ".gif"; 
    __downloadBtnOver   = __downloadBtnOver     ||  __imagesPath + "dnld_" + __selectedColor + "_over.gif";
    __downloadBtnDown   = __downloadBtnDown     ||  __imagesPath + "dnld_" + __selectedColor + "_dn.gif";
    __closeBtn          = __closeBtn            ||  __imagesPath + "close_" + __selectedColor + ".gif"; 
    __closeBtnOver      = __closeBtnOver        ||  __imagesPath + "close_" + __selectedColor + "_over.gif";
    __closeBtnDown      = __closeBtnDown        ||  __imagesPath + "close_" + __selectedColor + "_dn.gif";
    __dontShowCaption   = __dontShowCaption     || __imagesPath + "dont_show.gif";   
}



function __TopSlide()
{

    WriteToolbarAPIRequest();
    InitImagesDefinitions();
    WriteBannerHTML();
    
    //ExecuteFlyOver only after page loads and banner
    //code is written to the document!!!

    BannersControlHelper.AttachEvent(window, 'onload', ExecuteTopSlide);
    //log the banner
    if (LOG_ENABLED)
        var req = new LogScriptRequest(LOG_SITE_URL + logUsage());
}

function setTopSlideWidth(hide) {
    var d = document.getElementById('EBTopBanner');
    var f = function(){d.style.width = parseInt(GetRealBodyWidth());};

    if (hide) {
        d.style.width = 10;
        window.setTimeout(f, 1);
    }
    else f();
    
    
}

function ExecuteTopSlide()
{
    if(ValidateShow())
    {
        setTopSlideWidth(false);

        BannersControlHelper.AttachEvent(window, 'onresize',
        function() { setTopSlideWidth(true) });
        setTimeout(CommitTopSlide, 400);
    } 
}



function ValidateShow()
{
    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("TopSliderExposed") == "1")
        return false;
        
    //Validate cookies are enabled on the user's browser
    BannersControlHelper.SetCookie("TopSliderTester","1",1);    
   
    if(BannersControlHelper.ReadCookie("TopSliderTester") == "1")
    {
        //Cookies Enabled on user's browser
        if (BannersControlHelper.ReadCookie("TopSliderExposed") == "1")
        {
           return false;
        }            
                          
        return true;             
    }
    else //Cookies disabled on user's browser
    {
        return false;
    }
}

function get_ApiPath()
{
    if (typeof __apiPath == 'undefined')
        __apiPath = API_PATH;
    return __apiPath +'ToolbarApi.js';
}


function WriteToolbarAPIRequest() {
    document.write("<script src=\"" + get_ApiPath() +"\"></script>");
    //document.write("<script src=\"" + __baseURL + "/Api/ToolbarApi.js\"></script>");
}

function GetDownloadPath() {
    if (__downloadUrl.match("/exe") && typeof __newDownloadPath != 'undefined')
        return __newDownloadPath;
    return __downloadUrl;
}
//support for old banners
function getLocaleDirection() {
    //get the direction from accounts
    if (typeof __BannerDirection != "undefined")
        return __BannerDirection;
    //get the direction from the server - fallback
    if (typeof __LocaleDirection != "undefined")
        return __LocaleDirection;
    return "ltr";
    
}
  
function WriteBannerHTML()
{
    document.write('<div class="" id="EBTopBanner" style="'+GetAlphaFilter()+';z-index:1000;padding:0px 0px 0px 0px;display:none;width:100%;position:absolute;top:0px;left:0px;height:66px;background-image:url(' + __bgImage + ');background-repeat: repeat;background-repeat: repeat-x;">');
    document.write('<table class="" dir="' + getLocaleDirection() + '" height="100%" width="100%" cellpadding="0" cellspacing="0">');
    document.write('<tr>');
    document.write('	<td valign="top" style="padding-left:5px">');
    document.write('	<table cellpadding="0" cellspacing="0">');
    document.write('	<tr>');
    document.write('	<td align="left" style="padding-top:2px;padding-right:7px">');
    document.write('	<img border="0" style="cursor:pointer;cursor:hand" src="' + __closeBtn  + '"   onmouseover = "this.src=\'' + __closeBtnOver  +'\'"  onmouseout = "this.src=\'' + __closeBtn   +'\'" onmousedown = "this.src=\''+ __closeBtnDown  +'\'"  onmousedown = "this.src=\''+ __downloadBtnDown  +'\'" onclick="EBPreVanishTopSlide(false)" />');
    document.write('	</td>');     
    document.write('	<td style="padding-top:1px;padding-right:4px;text-align:right" class="">');
    document.write('	<input type="checkbox" class="" id="checkTopScrollDontShowAgain" onClick="DelayVanish()" />');
    document.write('	</td>');
    document.write('	<td>');
    document.write('	<img border="0" style="margin-top:1px" src="' + __dontShowCaption  + '" >');
    document.write('	</td>');
    document.write('	</tr>');
    document.write('	</table>');
    document.write('	</td>');
    document.write('	<td>');
    document.write('	<table cellpadding="0" cellspacing="0">');
    document.write('	<tr>');
    document.write('	<td><img src="' + __ImageArrow + '" />');
    document.write('	</td>');
    document.write('	<td width="18px">&nbsp;');
    document.write('	</td>');
    document.write('	<td align="top" style="vertical-align:top" ><img src="' + __textImage + '" align=top  />');
    document.write('	</td>');
    document.write('	<td style="width:31px">&nbsp;&nbsp;');
    document.write('	</td>');
    document.write('	</tr>');
    document.write('	</table>');
    document.write('	</td>');
    document.write('	<td style="vertical-align:top">&nbsp;');
    document.write('	</td>');
    document.write('	<td style="vertical-align:top;text-align:right">');
    document.write('	<table cellpadding="0" cellspacing="0">');
    document.write('	<tr>');
    document.write('	<td  colspan="4" style="padding-top:23px;padding-right:4px;vertical-align:top;text-align:right"><a href="' + GetDownloadPath() + '" ><img border="0" src="' + __downloadBtn + '"   onmouseover = "this.src=\'' + __downloadBtnOver + '\'"  onmouseout = "this.src=\'' + __downloadBtn + '\'" onmousedown = "this.src=\'' + __downloadBtnDown + '\'"  onmousedown = "this.src=\'' + __downloadBtnDown + '\'" onclick="EBDownloadToolbar()"  target="_blank" /></a>');
    document.write('	</td>');
    document.write('	</tr>');
    document.write('	</table>');
    document.write('	</td>');
    document.write('</tr>');
    document.write('</table>');
    document.write('</div>');
}

//------Log section------------//
String.format = function(text) {

    if (arguments.length <= 1) return text;
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++)
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                    arguments[token + 1]);
    return text;
};
function LogScriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl;
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Create the script tag
    this.scriptObj = document.createElement("script");

    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl);
    this.scriptObj.setAttribute("id", this.scriptId);
    //add the Script tag
    this.headLoc.appendChild(this.scriptObj);
}

function GetCtid() {
    if (typeof __ctid != 'undefined')
        return __ctid
    return "";
}

function GetCulture() {
    if (typeof __BannerCulture != 'undefined')
        return __BannerCulture;
    return "";
}

function logUsage() {
    try {
        var req = String.format('/?logType=impression&ctid={0}&bannertypeid={1}&bannerName={2}&bannerculture={3}', GetCtid(), 8, "Top_Slider", GetCulture());
        return req;
    }
    catch (e) { }
};
//------Log section------------//

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));
    }
}