window.onresize = function() {
    fnCheckScreenWidth();
};

window.onload = function() {
    fnCheckScreenWidth();
};

function fnCheckScreenWidth() {
    var flashViewer = document.getElementById('flashViewer');
    if (flashViewer) {
        if (fnGetWindowWidth() < 1240) {
            flashViewer.style.marginBottom = '60px';
        } else {
            flashViewer.style.marginBottom = '300px';
        }
    }

    var banners = new Array();
    banners.push(document.getElementById('bannerSide1'));
    banners.push(document.getElementById('bannerSide2'));
    banners.push(document.getElementById('bannerSide3'));
    banners.push(document.getElementById('bannerSide4'));

    for (var i = 0; i < banners.length; i++) {
        if (fnGetWindowWidth() < 1240) {
            if (banners[i]) {
                banners[i].className = 'banner bannerBottom';
            }
        } else {
            if (banners[i]) {
                banners[i].className = 'banner';
            }
        }
    }

    var bannerLast = document.getElementById('bannerSide4');
    if (bannerLast) {
        bannerLast.className = 'banner bannerLast';
    }

    $('search').getElementsBySelector('.txtSearch')[0].observe('focus', function(event) {
        $('search').getElementsBySelector('.txtSearch')[0].addClassName('txtSearchOn');
    });
}

var ffrInit = false;

function toggleFitForRide()
{
    var ffr = document.getElementById('fit-for-ride');
    var ffrs = document.getElementById('fit-for-ride-shade');
    var ffrf = document.getElementById('fit-for-ride-iframe');
    if (ffr.style.display != 'block')
    {
        hideAllFlash();
        if (!ffrInit)
        {
            ffrf.src = '/cat/flash/fitForRide_' + $get('__GLOBALRESOURCE_CURRENTLANGUAGE').value + '.html';
            //ffrf.src = '/cat/flash/fitForRide.html';
        
            vpWidth = fnGetWindowWidth() + fnGetWindowScrollLeft();
            vpHeight = fnGetWindowHeight() + fnGetWindowScrollTop();
            
            ffrs.style.width = vpWidth + 'px';
            ffrs.style.height = vpHeight + 'px';
            setOpacity(ffrs, 8);

            ffrInit = true;
        }
        fnShowFFRDialog(ffr);
        ffrs.style.left = fnGetWindowScrollLeft() + 'px';
        ffrs.style.top = fnGetWindowScrollTop() + 'px';
        ffr.style.display = 'block';
        ffrs.style.display = 'block';
    }
    else
    {
        showAllFlash();
        ffr.style.display = 'none';
        ffrs.style.display = 'none';
    }
}

function setOpacity(obj, value) {
	obj.style.opacity = value/10;
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function hideAllFlash()
{
    var objFlash = document.getElementsByTagName('OBJECT');
    var embFlash = document.getElementsByTagName('EMBED');
    var i = 0;
    for (i = 0; i < objFlash.length; i++)
    {
        objFlash[i].style.visibility = 'hidden';
    }
    for (i = 0; i < embFlash.length; i++)
    {
        embFlash[i].style.visibility = 'hidden';
    }
}

function showAllFlash()
{
    var objFlash = document.getElementsByTagName('OBJECT');
    var embFlash = document.getElementsByTagName('EMBED');
    var i = 0;
    for (i = 0; i < objFlash.length; i++)
    {
        objFlash[i].style.visibility = 'visible';
    }
    for (i = 0; i < embFlash.length; i++)
    {
        embFlash[i].style.visibility = 'visible';
    }
}

function fnShowFFRDialog(x)
{
    var vpWidth, vpHeight, dialogWidth, dialogHeight, dialogTop, dialogLeft;

    vpWidth = fnGetWindowWidth();
    vpHeight = fnGetWindowHeight();

    dialogWidth = 960;
    dialogHeight = 560;

    dialogTop = ((vpHeight/2) - (dialogHeight/2)) + fnGetWindowScrollTop();
    dialogLeft = ((vpWidth/2) - (dialogWidth/2)) + fnGetWindowScrollLeft();

    x.style.top = dialogTop + "px";
    x.style.left = dialogLeft + "px";
    x.style.display = "block";
}

function fnGetWindowHeight()
{
    var r;
    if (window.innerWidth)
    {
        r = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
        r = document.documentElement.clientHeight;
    }
    else
    {
        if (document.getElementsByTagName('body')[0])
        {
            r = document.getElementsByTagName('body')[0].clientHeight;
        }
    }
    return r;
}

function fnGetWindowWidth()
{
    var r;
    if (window.innerWidth)
    {
        r = window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
        r = document.documentElement.clientWidth;
    }
    else
    {
        if (document.getElementsByTagName('body')[0])
        {
            r = document.getElementsByTagName('body')[0].clientWidth;
        }
    }
    return r;
}

function fnGetWindowScrollTop()
{
    var y;
    if (self.pageYOffset)
    {
        y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        y = document.documentElement.scrollTop;
    }
    else if (document.body)
    {
        y = document.body.scrollTop;
    }
    return y;
}

function fnGetWindowScrollLeft()
{
    var x;
    if (self.pageYOffset)
    {
        x = self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        x = document.documentElement.scrollLeft;
    }
    else if (document.body)
    {
        x = document.body.scrollLeft;
    }
    return x;
}

function ClearField(field) 
{
	if (field.defaultValue == field.value) 
	{
		field.value = "";
	} 
}

function ResetField(field) 
{
	if (field.value == "") 
	{
		field.value = field.defaultValue;	
	}
}

function fnShowPreviousChunkSet(id)
{
    var current = parseInt($get(id + '_currentChunk').value);
    current = current - 6;
    $get(id + '_currentChunk').value = current;
    fnShowNextChunkSet(id);
}

function fnShowNextChunkSet(id)
{
    var iChunks = 3;
    var catResult = $get(id + '_catChunks').value.split(']]|[[');
    var prodResult = $get(id + '_prodChunks').value.split(']]|[[');
    var current = parseInt($get(id + '_currentChunk').value);
    var sHTML = '';
    var sCatHTML = '';
    var sProdHTML = '';
    var sPagination = '<div class="as-pagination">';
    var benchmark = 0;
    var benchIsProd = false;
    var oldCurrent = 0; 
    
    if (prodResult.length < catResult.length)
    {
        benchmark = catResult.length;
    }
    else
    {
        benchmark = prodResult.length;
        benchIsProd = true;
    }
    
    if (current > 0)
    {
        sPagination += '<div class="as-previous" onclick="fnShowPreviousChunkSet(\'' + id + '\');"><a href="javascript:fnShowPreviousChunkSet(\'' + id + '\');">&lt;</a></div>';
    }
    else
    {
        sPagination += '&nbsp;';
    }
    
    var shown = 0;
    
    if (current > benchmark || current < 0)
    {
        current = 0;
    }
    
    var previousCurrent = current;

    for (var i = 0; i < catResult.length; i++)
    {
        if (i >= current && catResult[i].length > 0)
        {
            sCatHTML += catResult[i];
            current++;
            shown++;
        }
        if (shown == iChunks)
        {
            break;
        }
    }
    
    if (!benchIsProd)
    {
        oldCurrent = current;
    }
    else
    {
        current = previousCurrent;
    }
    shown = 0;
    
    for (var i = 0; i < prodResult.length; i++)
    {
        if (i >= current && prodResult[i].length > 0)
        {
            sProdHTML += prodResult[i];
            current++;
            shown++;
        }
        if (shown == iChunks)
        {
            break;
        }
    }

    if (!benchIsProd)
    {
        current = oldCurrent;
    }

    $get(id + '_currentChunk').value = current;

    sPagination += $get(id + '_resourcePage').value + ' ' + Math.ceil(current / iChunks) + ' ' + $get(id + '_resourceOf').value + ' ' + Math.ceil(benchmark / iChunks);

    var catBlank = false;
    var prodBlank = false;

    if ($get(id).value != '')
    {
        if (sCatHTML == '')
        {
            sCatHTML = '';
            catBlank = true;
        }
        
        if (sProdHTML == '')
        {
            sProdHTML = '';
            prodBlank = true;
        }
        
        if (!catBlank || !prodBlank)
        {
            if (current < benchmark)
            {
                sPagination += '<div class="as-next" onclick="fnShowNextChunkSet(\'' + id + '\');"><a href="javascript:fnShowNextChunkSet(\'' + id + '\');">&gt;</a></div>';
            }
        }
        
        if (catBlank && prodBlank)
        {
            sPagination = '';
        }
    }

    if ($get(id).value == '')
    {
        sPagination = '';
    }

    if (sCatHTML != '')
    {
        sCatHTML = '<div class="as-heading">Categories</div>' + sCatHTML;
    }
    if (sProdHTML != '')
    {
        sProdHTML = '<div class="as-heading">Products</div>' + sProdHTML;
    }
    
    if ($get(id).value != '')
    {
        if (sCatHTML == '' && sProdHTML == '')
        {
            sCatHTML = '<div class="as-pagination">' + $get(id + '_resourceNoResults').value + '</div>';
        }
        else
        {
            sPagination += '<br /><a class="as-close" href="javascript:void(0);" onclick="$get(\'' + id + '_results\').innerHTML=\'\';">Close</a>';
        }
    }
    
    if ($get(id).value != '')
    {
        sPagination += '</div>';
    }

    Element.hide(id + '_results');
    $get(id + '_results').innerHTML = sPagination + sCatHTML + sProdHTML + sPagination;
    if ($get(id).value != '')
    {
        Effect.SlideDown(id + '_results');
    }
    window.setTimeout('$(\'' + id + '_results\').style.height=screen.height-350 + \'px\';', 2000);
}

function fnSetBasketText(btn, id)
{
    var quantity = parseInt($get(id + '_q').value);
    quantity++;
    btn.value = $get('__GLOBALRESOURCE_ADDTOBASKET').value + ' (' + $get('__GLOBALRESOURCE_INBASKET').value + ')';
    $get(id + '_q').value = quantity;
}

function noEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;
     else
          key = e.which;

     if(key == 13)
          return false;
     else
          return true;
}

function fnAddProductToBasket_Complete(result, context, methodName)
{
    $('basketStatusUpdate').innerHTML = result;
    var contextSplit = context.split('|');
    var productID = contextSplit[0];
    var btnID = contextSplit[1];
    if ($('q_' + productID) != null)
    {
        var quantity = parseInt($('q_' + productID).value);
        quantity++;
        $('q_' + productID).value = quantity;
    }
    if ($(btnID) != null)
    {
        //$(btnID).value = $('__GLOBALRESOURCE_ADDTOBASKET').value + ' (' + quantity + ' ' + $('__GLOBALRESOURCE_INBASKET').value + ')';
        $(btnID).value = $('__GLOBALRESOURCE_ADDEDTOBASKET').value
        $(btnID).disabled = true;
    }
}

function fnAddProductToBasket(productID, btnID)
{
    Sys.Net.WebServiceProxy.invoke('/HarleyAJAX.asmx', 'AddProductToBasket', true, {"productID":productID}, fnAddProductToBasket_Complete, function(e,r,c){}, productID + '|' + btnID, 50000);
}

function fnShowAlert(id, spanContent)
{
    $(id + '_span').innerHTML = spanContent;
    $(id).style.display = 'block';
    
    // Code from http://www.quirksmode.org/viewport/compatibility.html
    var y;
    if (self.pageYOffset) // all except Explorer
    {
	    y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
	    // Explorer 6 Strict
    {
	    y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
	    y = document.body.scrollTop;
    }
    
    $(id).style.top = parseInt(y + (self.screen.height - 295)) + 'px';
    window.setTimeout('Effect.Fade(\'' + id + '\')', 2500);
}

function openWindow(target)
{
    the_window = window.open(target, 'the_window', 'toolbars=0, scrollbars=1, width=600, height=500');
    the_window.focus();
}