﻿// Make sure ajax is included.
var endorse_img_divid = 'endorse-box';
var still_over = false;

function Endorse(productid, userid, imageid, DoneFunction)
{
    var vars = new Array();
    vars[0] = 'product-id=' + productid.toString();
    vars[1] = 'image-id=' + imageid.toString();
    
    var a = new AjaxCall('product.endorse', vars);
    
    a.Send(function(result)
    {
        var mkey = new String('new-endorser-' + userid.toString());
        var e = document.getElementById(mkey);
        var p = document.getElementById('invisible-endorse');
        var n = document.getElementById('no-box');
        
        if(p != null) p.style.display = 'block';

        p = document.getElementById('endorse-div');
        
        n.removeChild(e);
        
        e.id = 'endorser-' + userid.toString();
        p.appendChild(e);

        e = document.getElementById('endorsing-action');
        e.className = 'endorse';
        e.innerHTML = 'You are endorsing this (<a href="javascript:void(0);" onclick="UnEndorse(' + productid.toString() + ', \'' + userid.toString() + '\');">Cancel</a>)';
        
        try
        {
            DoneFunction();
        }
        catch(ex)
        {
        
        }
    });
}

function UnEndorse(productid, userid)
{
    var vars = new Array();
    vars[0] = 'product-id=' + productid.toString();
    
    var a = new AjaxCall('product.unendorse', vars);
    
    a.Send(function(result)
    {
        var e = document.getElementById('endorser-' + userid.toString());
        var p = document.getElementById('no-box');
        
        e.parentNode.removeChild(e);
        
        e.id = 'new-endorser-' + userid.toString();
        p.appendChild(e);
        
        e = document.getElementById('endorsing-action');
        e.className = 'un-endorse';
        e.innerHTML = 'You are not endorsing this (<a href="javascript:void(0);" onclick="Endorse(' + productid.toString() + ', \'' + userid.toString() + '\', 0);">Endorse Now</a>)';
    });
}

function ShowImages(obj, productid, userid)
{
    var x = document.getElementById(endorse_img_divid);
    
    if(x == null)
    {
        x = document.createElement('div');
        x.id = endorse_img_divid;
        
        x.onmouseover = function(ev)
        {
            still_over = true;
        }
        
        x.onmouseout = function(ev)
        {
            still_over = false;
            HideImages();
        }
        
        document.body.appendChild(x);

        x.style.left = (GetX(obj) - 8).toString() + 'px';
        x.style.top = (GetY(obj) - 5).toString() + 'px';
        

        var div = document.createElement('div');
        div.innerHTML = '<span>Endorse Now</span>';
        div.className = 'tab-left';
        x.appendChild(div);

        div = document.createElement('div');
        div.innerHTML = '&nbsp;';
        div.className = 'tab-right';
        x.appendChild(div);
        
        var x_body = document.createElement('div');
        var strong = document.createElement('strong');
        
        strong.innerHTML = 'Choose an Image to Use';

        x_body.className = 'x-body';        
        x_body.appendChild(strong);
        
        div = document.createElement('div');
        div.className = 'inner';
        div.innerHTML = '<br/><br/><br/><img src="/user.assets/6/6/6/loading.gif" />'
        x_body.appendChild(div);
        
        x.appendChild(x_body);
        x.style.display = 'block';
        
        var vars = new Array();
        vars[0] = 'productid=' + productid;
        vars[1] = 'width=75';
        vars[2] = 'height=75';
        
        var a = new AjaxCall('get.product.images', vars);
        a.sessionid = 'xx-xx';
        a.resulttype = 'xml';
        
        a.Send(function(result)
        {
            var xml = LoadXMLDoc(result);
            var items = xml.getElementsByTagName('item');
            var str = new String('');
            
            div.innerHTML = '';
            
            for(var i = 0; i < items.length; i++)
            {
                var click = document.createElement('div');
                click.className = 'img';
                click.innerHTML = ('<img src="' + items[i].childNodes[2].firstChild.nodeValue + '" />');
                click.imageid = items[i].childNodes[0].firstChild.nodeValue;
                click.url = items[i].childNodes[2].firstChild.nodeValue;
                
                click.onclick = function(ev)
                {
                    Endorse(productid, userid, this.imageid, function()
                    {
                        still_over = false;
                        ActualHide();
                    });
                }
                
                div.appendChild(click);
            }

        });
        
    }
    else
    {
        x.style.display = 'block';
    }
}

function HideImages()
{
    setTimeout("ActualHide()", 300);
}

function ActualHide()
{
    if(still_over) return;
    var x = document.getElementById(endorse_img_divid);
    if(x != null) x.style.display = 'none';
}

