
var mmlist = new Array();
var imageUrl = 'http://i.expansys.com/i/s/s';
var productUrl = '';

// MiniMall class

function miniMall(id, classname, basesite, region) {
    this.id = id;
    this.classname = classname;
    this.basesite = basesite;
    this.region = region;
    this.products = new Array();
    this.add = addProduct;
    this.render = renderMiniMall;
    this.switchProduct = switchProduct;
    this.ordinal = mmlist.length;
    this.locked = false;
    mmlist.push(this);
}

function addProduct(id, title, price, description, partner, slugizedFqn) {
    var p = new miniMallProduct(id, title, price, description, partner, slugizedFqn);
    this.products.push(p);
}

function renderMiniMall() {
    document.write('<div id="'+ this.id +'" class="'+ this.classname +'">');
    if (this.region == 3)
    {
        document.write('<ul class="productListFR">');
    }
    else
    {
        document.write('<ul class="productList">');
    }
    if (this.classname == 'w728') document.write('<h3><a href="' + this.basesite + '/smartphone?top20=1&partner=mobig">Most Popular Devices</a></h3>');
    for(var x in this.products) 
    {
        // set productUrl
        productUrl = "/" + this.products[x].slugizedFqn + "-" + this.products[x].id + "/";
        
        if (this.classname == 'w728')
        {
            productUrl += '?partner=mobig';
        }
        else if (this.products[x].partner == 'tracyandmatt')
        {
            productUrl += '?utm_source=tracyandmatt&utm_medium=iframe&utm_campaign=tracyandmatttop5&partner=tracyandmatt';
        }
        else if (this.products[x].partner == 'modaco')
        {
            productUrl += '?utm_source=modaco&utm_medium=iframe&utm_campaign=modacotop3&partner=modaco';
        }
        else if (this.products[x].partner == 'register')
        {
            productUrl += '?partner=register&utm_source=register&utm_medium=iframe&utm_campaign=registertop5';
        }
        else if (this.products[x].partner == 'reghardware')
        {
            productUrl += '?partner=reghardware&utm_source=reghardware&utm_medium=iframe&utm_campaign=reghardwaretop5';
        }
        else if (this.products[x].partner == 'eurodroid')
        {
            productUrl += '?partner=eurodroid&utm_source=eurodroid&utm_medium=iframe&utm_campaign=eurodroidtop3';
        }
        else
        {
            productUrl += '?partner=' + this.products[x].partner;
        }
          
        // write the product <li>      
        if (this.classname == 'w728')
        {
            document.write('<li><a id="'+ this.id +'prdlink'+ x +'" href="' + productUrl + '">' + this.products[x].title + '</a></li>');
        }
        else if (this.classname == 'w270')
        {
            document.write('<li><a id="'+ this.id +'prdlink'+ x +'" href="' + productUrl + '" target="_blank"">' + this.products[x].title + '</a></li>');
        }
        else
        {
            document.write('<li><a id="'+ this.id +'prdlink'+ x +'" href="' + productUrl + '" target="_blank" onmouseover="mmlist['+ this.ordinal +'].switchProduct('+ x +');"">' + this.products[x].title + '</a></li>');
        }
    }
    document.write('</ul>');
    
    if (this.classname != 'w728' && this.classname != 'w270') {
        for(var x in this.products) this.products[x].render(x, this.id, this.classname, this.region);
    }
    
    document.write('</div>');
}

function switchProduct(product) {
    if (!this.locked) {
        for(var x in this.products) document.getElementById(this.id + 'prd' + x).style.display = 'none';
        for(var x in this.products) document.getElementById(this.id + 'prd' + product).style.display = 'block';
    }
}


// MiniMallProduct class

function miniMallProduct(id, title, price, description, partner, slugizedFqn) {
    this.id = id;
    this.partner = partner;
    this.title = title;
    this.price = price;
    this.description = description;
    this.image = imageUrl + this.id + '.jpg';
    this.slugizedFqn = slugizedFqn;
    this.render = renderMiniMallProduct;
}

function renderMiniMallProduct(ord, parentid, classname, region) {
    if (classname == 'w468')
    {
        document.write('<div class="product" id="'+ parentid +'prd'+ ord +'"');
        if (ord != 0) document.write(' style="display:none;"');
        document.write('>');
        document.write('<a href="'+ productUrl + '" target="_blank"><img class="productImage" src="' + this.image + '" alt="' + this.title + '"/></a>');
        document.write('<h3><a href="'+ productUrl + '" target="_blank">'+ this.title +' - ' + this.price + '</a></h3>');
        document.write('<div class="button"><a href="'+ productUrl + '" target="_blank"><img src="/j/6c/images/minimall/buy-now.png"></a></div>');
        document.write('</div>');
    }
    else
    {
        var buyNowButton = "buy-now.png";
        if (region == 3)
        {
            buyNowButton = "buy-now-fr.png";
        }
        
        document.write('<div class="product" id="'+ parentid +'prd'+ ord +'"');
        if (ord != 0) document.write(' style="display:none;"');
        document.write('>');
        document.write('<a href="'+ productUrl + '" target="_blank"><img class="productImage" src="' + this.image + '" alt="' + this.title + '"/></a>');
        document.write('<h3><a href="'+ productUrl + '" target="_blank">'+ this.title +'</a></h3>');
        
        document.write('<p>'+ this.description +'</p>');
        document.write('<div class="button"><a href="'+ productUrl + '" target="_blank"><img src="/j/6c/images/minimall/' + buyNowButton + '"></a></div>');
        document.write('<h4');
        if (region == 3)
        {
            document.write(' class="fr"');
        }
        document.write('>'+ this.price +'</h4>');
        document.write('</div>');
    }
}
