﻿function Shop_Widget() {

    this.base_url = 'http://shop.vtc.vn';
    this.rand_limit = 6;

    this.Shuffle = function(v) {
        for (var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
        return v;
    }

    var url = window.location.href.toLowerCase();

    // Create Css
    this.CreateStyleSheet = function() {

        var css = '';

        var style = document.createElement('style');
        style.type = 'text/css';
        var head = document.getElementsByTagName('head')[0];
        head.appendChild(style);
        if (style.styleSheet)  // IE
            style.styleSheet.cssText = css;
        else  // other browserss
            style.appendChild(document.createTextNode(css));
    }

    this.InitScript = function() {
        var output = '';
        try {
            var url = window.location.href.toLowerCase();
            if (!document.getElementById('Shop_Content')) {
                document.write('<div id="Shop_Content" style="width: 190px;" align="left"></div><div><img src="http://shop.vtc.vn/adv/vtc_zooz/bottom_g.gif" width="190" height="13"/></div>');
            }
        }
        catch (e) {
            //Do nothing?
        }
		
        output += '<div><a href="http://shop.vtc.vn/" target="_blank"><img src="http://shop.vtc.vn/adv/vtc_zooz/vtc_shop_logo.gif" width="190" height="33" style="border:0"/></a></div>';
        output += '<div><img src="http://shop.vtc.vn/adv/vtc_zooz/top_g.gif" width="190" height="14" /></div>';
		output += '<div style=" width: 190px; background-image:url(http://shop.vtc.vn/adv/vtc_zooz/bg_main.gif);" align="center">';
        output += '<div align="center"><div id="Shop_Item" style="padding-top:1px; width: 190px;" align="center"></div></div>';

        try {
            if (document.getElementById('Shop_Content')) {
                document.getElementById('Shop_Content').innerHTML = output;
            }
        }
        catch (e) {
            //Do nothing?
            alert("exception");
        }

        this.CreateStyleSheet();
    }

    this.OnLoaded = function(data) {
        var json = eval(data); // Parse json data

        var output = '';
        var image_url = '';
        var item_link = '';

        json.Items = this.Shuffle(json.Items);
        json.Items.length = (this.rand_limit > json.Items.length) ? json.Items.length : this.rand_limit;

        for (var i = 0; i < json.Items.length; i++) {
            image_url = '';
            if (json.Items[i].Image != '') {
                image_url = json.Items[i].Image;
            }
            else {
                image_url = this.script_folder + 'data/upload/no-images-69x53.jpg';
            }
            var price = json.Items[i].Price;
            var numberText = '';
            var numberTextAll = '';

            item_link = json.Items[i].Link;

			output += '<div id="vtcshop_item_' + json.Items[i].id + '" style="margin: 2px 3px 0px;padding-top: 1px;"><div><a href="' + item_link + '" target="_blank" style="border: medium none ; margin: 0pt; padding: 0pt;"><img src="' + image_url + '" width="172" height="130" alt="Giá: ' + json.Items[i].Price + '" title="Giá: ' + json.Items[i].Price + '" style="border: medium none; margin: 0pt; padding: 0pt;" /></a></div><div style="padding-top:4px; margin-top:4px; background-image:url(http://shop.vtc.vn/adv/vtc_zooz/bg_title.gif); background-repeat: no-repeat;overflow:hidden; color:rgb(255, 255, 255); font-family:Arial,Helvetica,sans-serif; font-size: 11px;font-weight: bold; width:172px; height:23px" align="center"><div align="center" title="' + json.Items[i].Title + '" style="width:150px;height:13px;overflow:hidden;">' + json.Items[i].Title + '</div></div></div>';
        }

        try {
            if (document.getElementById('Shop_Item')) {
                document.getElementById('Shop_Item').innerHTML = output;
            }
        }
        catch (e) {
            //Do nothing?
        }
    }
}

/* Call ajax to load json file */
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

var xmlHttpObj = GetXmlHttpObject();
if (xmlHttpObj == null) {
    alert("Browser does not support HTTP Request");
}
var shop = new Shop_Widget();
shop.InitScript();
xmlHttpObj.onreadystatechange = function() {
    if (xmlHttpObj.readyState == 4) {
        shop.OnLoaded(({ "Items": [{ "id": "1133", "Title": "Samsung E1310", "Link": "http://shop.vtc.vn/XemChiTiet/108/1133/index.htm","Price": "858,500 VNĐ", "Image": "https://shop.vtc.vn/ImagesService/srvPhoto.ashx?name=47/1133/2351&w=160&h=160"},{ "id": "1132", "Title": "Samsung E1080", "Link": "http://shop.vtc.vn/XemChiTiet/108/1132/index.htm","Price": "434,300 VNĐ", "Image": "https://shop.vtc.vn/ImagesService/srvPhoto.ashx?name=47/1132/2350&w=160&h=160"},{ "id": "1124", "Title": "Samsung B200", "Link": "http://shop.vtc.vn/XemChiTiet/108/1124/index.htm","Price": "737,300 VNĐ", "Image": "https://shop.vtc.vn/ImagesService/srvPhoto.ashx?name=47/1124/2342&w=160&h=160"},{ "id": "1472", "Title": "Nokia 1208", "Link": "http://shop.vtc.vn/XemChiTiet/108/1472/index.htm","Price": "575,700 VNĐ", "Image": "https://shop.vtc.vn/ImagesService/srvPhoto.ashx?name=47/1472/2985&w=160&h=160"},{ "id": "959", "Title": "Nokia 2730", "Link": "http://shop.vtc.vn/XemChiTiet/108/959/index.htm","Price": "1,919,000 VNĐ", "Image": "https://shop.vtc.vn/ImagesService/srvPhoto.ashx?name=47/959/1942&w=160&h=160"},{ "id": "1493", "Title": "Nokia 1661", "Link": "http://shop.vtc.vn/XemChiTiet/108/1493/index.htm","Price": "727,200 VNĐ", "Image": "https://shop.vtc.vn/ImagesService/srvPhoto.ashx?name=47/1493/3024&w=160&h=160"} ] }));
    }
}
xmlHttpObj.open("GET", "Items", true);
xmlHttpObj.send(null);