﻿
/*********************************************************
** Browser - to contain all required browser information
*********************************************************/

function BrowserBase()
{
    try
    {
        this.AgentName=navigator.userAgent.toLowerCase();
    }
    catch(e)
    {
        this.AgentName = '';
    }
    this.MajorVersionNumber =parseInt(navigator.appVersion);
    this.IsIE = ( this.AgentName.indexOf('msie') != -1 );
    this.IsChrome = ( this.AgentName.indexOf('chrome') != -1 );
    if ( this.IsIE )
    {
        this.Version = parseFloat(this.AgentName.substring(this.AgentName.indexOf('msie') + 5, this.AgentName.length));
    }
}

var Browser = new BrowserBase();

/*********************************************************
** iePage - IE specific page related functions
*********************************************************/

iePageBase = function()
{
    this.Mask;
}
iePageBase.prototype = {
    init : function()
    {
        if ( Browser.IsIE && Browser.Version < 8 )
        {
            // Run the updates        
            this.fixMenu();
            if ( Browser.Version < 7 )
            {
                this.fixPNGs();
            }
        }
    },
    fixPNGs : function()
    {
        for ( var i = 0; i < document.images.length; i ++ )
        {
            var img = document.images[i]
            var imgName = img.src.toUpperCase()
            if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
            {
                var imgID = (img.id) ? "id='" + img.id + "' " : ""
                var imgClass = (img.className) ? "class='" + img.className + " PngFix' " : "class='PngFix' "
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
	            var imgStyle = "display:inline-block;" + img.style.cssText
                if (img.align == "left") imgStyle = "float:left;" + imgStyle
                if (img.align == "right") imgStyle = "float:right;" + imgStyle
                if (img.parentElement && img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                var strNewHTML = "<span " + imgID + imgClass + imgTitle
                 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                 + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>"
                 img.outerHTML = strNewHTML
                 i = i-1;
            }
        }
    },
    fixMenu : function()
    {
        var UL = document.getElementsByTagName('ul');
        if ( UL.length > 0 )
        {
            for ( var i = 0; i < UL.length; i ++ )
            {
                if ( UL[i].className.indexOf('AspNet-Menu') != -1 )
                {
                    var LI = UL[i].getElementsByTagName('li');
                    for ( var j = 0; j < LI.length; j ++ )
                    {
                        LI[j].onmouseover = function()
                        {
                            this.className += ' AspNet-Menu-Hover';
                        }
                        LI[j].onmouseout = function()
                        {
                             this.className = this.className.split(' AspNet-Menu-Hover').join('');
                        }
            
                        // Mask for IE6
                        if ( LI[j].firstChild && LI[j].firstChild.tagName
                            && ( LI[j].className.indexOf('AspNet-Menu-WithChildren') != -1 )
                            && ( Browser.Version < 7 ) )
                        {
                            // Get the LI
                            var button = LI[j];
                            
                            // Set the Interaction
                            button.onmouseover = function()
                            {
                                this.className += ' AspNet-Menu-Hover';
                                iePage.showMask(this);
                            }
                            button.onmouseout = function()
                            {
                                iePage.checkMouseOut(this);
                            }

                            var DropElement = this.getDropElement( LI[j] );
                            if ( DropElement != null )
                            {
                                // Set the zindex on the ul/div to always be above the iframe
                                DropElement.style.zIndex = 12;
                            }
                        }
                    }
                }
            }
        }
    },
    getDropElement : function(o)
    {
        var DropElement = null;
        if ( o.getElementsByTagName('div').length > 0 )
        {
            DropElement = o.getElementsByTagName('div')[0];
        }
        else if ( o.getElementsByTagName('ul').length > 0 )
        {
            DropElement = o.getElementsByTagName('ul')[0];
        }
        
        return DropElement;
    },
    createMask : function()
    {
        // Create the Masking Iframe
        var mask = document.createElement('iframe');
        mask.scrolling= 'no';
        mask.src = 'javascript:false;';
        mask.id = 'DropMask';
        mask.frameBorder = 0;
        mask.style.position = 'absolute';
        mask.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
        mask.style.zIndex = 10;
        mask.style.display = 'none';
        document.getElementsByTagName('body')[0].appendChild(mask);
        
        // Create a ref to the IFrame
        this.Mask = document.getElementById('DropMask');
    },
    showMask : function(o)
    {
        // If the mask hasn't been created, then create it
        if ( typeof( this.Mask ) != 'object' )
        {
            this.createMask();
        }
        
        var DropElement = this.getDropElement(o);
        if ( DropElement != null )
        {
            var pos = this.getAbsolutePosition(DropElement);
            
            // Reposition and show the mask
            this.Mask.style.width = DropElement.offsetWidth + 'px';
            this.Mask.style.height = DropElement.offsetHeight + 'px';
            this.Mask.style.top = pos.y + 'px';
            this.Mask.style.left = pos.x + 'px';
            this.Mask.style.display = 'block';
        }
    },
    getAbsolutePosition: function(element)
    {
        var r = { x: element.offsetLeft, y: element.offsetTop };
        if (element.offsetParent)
        {
            var tmp = this.getAbsolutePosition(element.offsetParent);
            r.x += tmp.x;
            r.y += tmp.y;
        }
        return r;
    },
    checkMouseOut : function(o)
    {
        var DropElement = this.getDropElement(o);
        if ( DropElement != null )
        {
            // Get the mouse positions
            var mouse = 
            {
                x: window.event.x + document.body.scrollLeft, 
                y: window.event.y + document.body.scrollTop 
            };
            
            // Get the Drop Element positions - one pixel inside
            var element = 
            {
                l: DropElement.offsetLeft + 1,
                r: DropElement.offsetLeft + DropElement.offsetWidth - 1,
                t: DropElement.offsetTop + 1,
                b: DropElement.offsetTop + DropElement.offsetHeight - 1
            };
            
            // Mouse inside Element?
            var inside = ( ( mouse.x > element.l && mouse.x < element.r ) && ( mouse.y > element.t && mouse.y < element.b ) );

            if ( !inside )
            {
                o.className = o.className.split(' AspNet-Menu-Hover').join('');
                this.Mask.style.display = 'none';
            }
        }
    }
}
var iePage = new iePageBase();

//*************************************
// Page Object
//*************************************

PageBase = function(){}
PageBase.prototype =
{
	init: function()
    {
        this.attachEvents();
        this.SelectContentTabs();
        this.SelectMenuItems();
        this.convertObjectToIFrame();
    },
    SelectContentTabs: function()
    {
        if ( document.getElementById('Content') )
        {
            var ul = document.getElementById('Content').getElementsByTagName('ul');
            if ( ul.length > 0 )
            {
                for ( var i = 0; i < ul.length; i ++ )
                {
                    if ( ul[i].className.indexOf('tabnav') != -1 )
                    {
                        this.SelectContentTab(ul[i]);
                    }
                }
            }
        }
    },
    SelectContentTab: function( ul )
    {
        var li = ul.getElementsByTagName('li');
        if ( li.length > 0 )
        {
            for ( var i = 0; i < li.length; i ++ )
            {
                var a = li[i].getElementsByTagName('a')[0];
                if ( a && ( a.className.indexOf('sel') != -1 ))
                {
                    li[i].className += ' Selected';
                }
            }
        }
    },
    SelectMenuItems: function()
    {
        if ( document.getElementById('BCASelectableMenuControl') )
        {
            var li = document.getElementById('BCASelectableMenuControl').getElementsByTagName('li');
            if ( li.length > 0 )
            {
                for ( var i = 0; i < li.length; i ++ )
                {
                    var a = li[i].getElementsByTagName('a')[0];
                    if ( a && ( a.href == window.location ))
                    {
                        li[i].className += ' Selected';
                    }
                }
            }
        }
    },
    attachEvents: function()
    {
        // FTS
        if ( document.getElementById('fts_input') && document.getElementById('fts_submit') )
        {
            var i = document.getElementById('fts_input');
            var s = document.getElementById('fts_submit');
            i.onfocus = function(){ if ( this.value == 'Quick search' ) this.value = ''; }
            i.onblur  = function(){ if ( this.value == '' ) this.value = 'Quick search'; }
            s.onclick = function()
            {
                if ( document.getElementById('fts_input').value != 'Quick search' )
                {
                    var f = document.getElementById('aspnetForm');
                    var v = document.getElementById('fts_input').value;
                    v = v.replace(/^(\s+)/, ''); // Remove any preceding spaces
                    v = v.replace(/(\s+)$/, ''); // Remove any trailing spaces
                    f.action = 'http://auctionview.british-car-auctions.co.uk/Results/FreeTextSearch/' + escape(v);
                }
            }
            i.onkeydown = function(e)
            {
            	var evt = e || window.event;
                if ( evt.keyCode == 13 )
                {
                    var f = document.getElementById('aspnetForm');
                    var v = this.value;
                    v = v.replace(/^(\s+)/, ''); // Remove any preceding spaces
                    v = v.replace(/(\s+)$/, ''); // Remove any trailing spaces
                    f.action = 'http://auctionview.british-car-auctions.co.uk/Results/FreeTextSearch/' + escape(v);
                    f.submit();
                }
            }
        }
        
        // Country Select
        if ( document.getElementById('country_select_close') && document.getElementById('country_select_toggle') )
        {
            var c = document.getElementById('country_select_close');
            var t = document.getElementById('country_select_toggle');
            c.onclick = function(){ document.getElementById('SiteSelection').style.display = 'none'; }
            t.onclick = function()
            {
                var ss = document.getElementById('SiteSelection')
                ss.style.display = ( ss.style.display == 'block' )? 'none' : 'block';
                this.blur();
            }
        }
        
        // Check for any image rollovers
        if ( document.getElementById('Content') )
        {
            var img = document.getElementById('Content').getElementsByTagName('img');
            for ( var i = 0; i < img.length; i ++ )
            {
                if ( ( img[i].className == 'Rollover' ) && ( img[i].src.indexOf('_n.') != -1 ) )
                {
                    // Preload the hover image
                    var temp = new Image();
                    temp.src = img[i].src.split('_n.').join('_h.');
                    
                    // Attach the rollover events
                    img[i].onmouseover = function()
                    {
                        this.src = this.src.split('_n.').join('_h.');
                    }
                    img[i].onmouseout = function()
                    {
                        this.src = this.src.split('_h.').join('_n.');
                    }
                }
            }
        }
    },
    convertObjectToIFrame: function()
    {
        var o = document.getElementsByTagName('object');
        for (var i = 0; i < o.length; i++)
        {
            if (o[i].type && (o[i].type == 'text/html') && o[i].data)
            {
                var frame = '<iframe id="ObjectIFrame' + i + '" scrolling="auto" frameborder="0" marginwidth="0" marginheight="0" border="0"';
                if ( o[i].style['cssText'] ){ frame += ' style="' + o[i].style['cssText'] + '"'; }
                frame += ' src="' + o[i].data + '">Please update your browser.</iframe>';
                var d = document.createElement('div');
                d.innerHTML = frame;
                o[i].parentNode.insertBefore(d, o[i]);
                o[i].style.display = 'none';
            }
        }
    }
}

var Page = new PageBase();

//*************************************
// Popup Load
//*************************************

PopupBase = function(){}
PopupBase.prototype =
{
	open: function( href, w, h )
    {
        if ( window.pop ){ window.pop.close(); } // close an open one
        if ( Popup.open.arguments.length < 2 ){ w = 540; };
        if ( Popup.open.arguments.length < 3 ){ h = 440; };
        var args = 'toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,copyhistory=no,locationbar=no,width=';
        args += w + ',height=' + h + ',screenX=0,screenY=0,top=0,left=0';
        pop = window.open( href, 'BCAPOPUP', args );
    }
}

var Popup = new PopupBase();

//*************************************
// Analytics
//*************************************

var Analytics =
{
    GetPageURL: function()
    {
        var url = '';
        if ( document.getElementById('SiteNav').innerHTML )
        {
            var div = document.getElementById('SiteNav').getElementsByTagName('div');
            for ( var i = 0; i < div.length; i ++ )
            {
                if ( div[i].className == 'Location' )
                {
                    var li = div[i].getElementsByTagName('li');
                    for ( var j = 1; j < li.length; j ++ )
                    {
                        url += '/' + li[j].firstChild.innerHTML;
                    }
                }
            }
        }
        if ( url == '' )
        {
            url = '/Home';
        }
        return url;
    },
    TrackEvent: function(category, action, opt_label, opt_value)
    {
        //alert('Track Event: ' + category.toString() + ',' + action.toString() + ',' + opt_label.toString() ); //+ ',' + opt_value.toString());
        try
        {
            var pageTrackerResponse = pageTracker._trackEvent(category, action, opt_label, opt_value);
        }
        catch (err) { }
    },
    SetCustomVariable: function(index, name, value, scope)
    {
        //alert('Set Custom Variable: ' + index.toString() + ',' + name.toString() + ',' + value.toString() + ',' + scope.toString());
        try
        {
            pageTracker._setCustomVar(index, name, value, scope);
        }
        catch (err) { }
    },
    TrackPageView: function()
    {
        //alert('Track Page: ' + this.GetPageURL() );
        try
        {
            pageTracker._trackPageview( this.GetPageURL() );
        }
        catch (err) { }
    }
}

//*************************************
// DOM Load
//*************************************

function domFunction(f, a)
{
	var n = 0;
	var t = setInterval(function()
	{
		var c = true;
		n++;
		if (typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
			c = false;
			if (typeof a == 'object') {
				for (var i in a) {
					if ((a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)) {
						c = true;
						break;
					}
				}
			}
			if (!c) { f(); clearInterval(t); }
		}
		if (n >= 60) { clearInterval(t); }
	}, 250);
}

// Detect when the DOM has loaded
var dom_load = new domFunction( function()
    {
        if ( typeof(EditorMode) == 'undefined' )
        {
            iePage.init();
            Page.init();
        }
    },
    { 'dom_loaded' : 'id' }); // Once the last div has been detected

