var theCity = getCookie('city');
if (!theCity || theCity == 'null' || /\W/.test(theCity)) theCity = '';
var theCate = '';
var thePage = 0;
var pageCount = 0;
var pageSize = 18;
var list;
var lazy;
var crossdomain = false;
var busy = false;
var timeout = 10000;
var ua = navigator.userAgent;
var ie = ua.indexOf('MSIE') != -1;
var ff = ua.indexOf('Firefox') != -1;
var text, getText, setText;
if (ff) {
    getText = function(el) {
        return el.textContent;
    };
    setText = function(el, s) {
        el.textContent = s;
    };
} else {
    getText = function(el) {
        return el.innerText;
    };
    setText = function(el, s) {
        el.innerText = s;
    };
}
text = function(el, s) {
    if (arguments.length > 1) setText(el, s);
    else return getText(el);
};
var tg2 = {
    lazyLoad: function() {}
};
function hzCity(s) {
    for (var i = 0; cityList[i]; i++) if (cityList[i][2] == s) return cityList[i][0];
    return null;
}
function pyCity(s) {
    for (var i = 0; cityList[i]; i++) if (cityList[i][0] == s) return cityList[i][2];
    return null;
}
function $id(s) {
    return document.getElementById(s);
}
function htmlEncode(s) {
    return (s + '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\x22/g, '&quot;');
}
function contains(a, b) {
    return a.contains ? a != b && a.contains(b) : a.compareDocumentPosition(b) & 16;
}
function toggleCity(c) {
    var d = document.getElementById('d2');
    if (d.style.display == 'none') {
        d.style.display = '';
    } else {
        d.style.display = 'none';
    }
    return false;
}
function getCookie(name) {
    var re = new RegExp('(?:^| )' + name + '=([^;]*)');
    var a = document.cookie.match(re);
    if (!a) return '';
    return unescape(a[1]);
}
function setCookie(name, value, days) {
    var d = new Date;
    d.setDate(d.getDate() + days);
    document.cookie = name + '=' + escape(value) + '; expires=' + d.toGMTString();
}
function setCity(s) {
    setCookie('city', theCity = s, 30);
    text($id('c'), hzCity(s));
    $id('ct' + s).className = 'cur';
    var o = $id('ct0' + s);
    if (o) o.className = 'cur';
}
function setCate(s) {
    $id('cate' + theCate).className = '';
    theCate = s;
    $id('cate' + s).className = 'cur';
}
function setPage(page, count) {
    var p = $id('page'); 
	if (count == 0) {
        p.parentNode.style.display = 'none';
        return;
    }
    p.parentNode.style.display = '';
    thePage = page;
    pageCount = Math.ceil(count / pageSize);
    text(p, page + '/' + pageCount);
    var sel = $id('go_page');
    sel.options.length = 0;
    for (var i = 1; i <= pageCount; i++) sel.options.add(new Option('第' + i + '页', i));
    sel.selectedIndex = page - 1;
}
function cbGetGoods(r) {
    r = eval('0,' + r);
    var bTop = r.city == theCity && r.cate == theCate && thePage != r.page;
    setCity(r.city);
    setCate(r.cate);
    setPage(r.page, r.count);
    if (bTop) toTop();
    lazy = !crossdomain && self != parent && parent == top;
    var a = list = r.list;
    var s = '';
    for (var i = 0; a[i]; i++) s += '\
<div class=item id=item' + i + '>\
 <div class=m1><a href="' + htmlEncode(a[i].VC2SOURCEURL) + '"><img src="' + (lazy && i > 5 ? 'about:blank': ('img2/pic_1.jpg', htmlEncode(a[i].VC2THUMB))) + '"></a></div>\
 <div class=m2><a href="' + htmlEncode(a[i].VC2COMURL) + '"><b>' + htmlEncode(a[i].VC2COMNAME) + '</b></a> <a href="' + htmlEncode(a[i].VC2SOURCEURL) + '">' + htmlEncode(a[i].VC2TITLE) + '</a></div>\
 <div class=m3>\
  <table width="100%">\
  <tr>\
  <td nowrap>团购价: <font color=red size="+1"><b>￥ ' + htmlEncode(a[i].NUMDISPRICE) + '</b></font></td>\
  <td align=right><span class=blue>' + htmlEncode(a[i].NUMORDERS) + '</span>人已买</td></tr></table>\
  <table width="100%"><tr>\
  <td>折&#12288;扣: <font color=red>' + htmlEncode(a[i].NUMDISPER) + '折</font><br>\
   原&#12288;价: ' + htmlEncode(a[i].NUMPRICE) + '元</td>\
  <td align=right><a href="' + htmlEncode(a[i].VC2SOURCEURL) + '" class=btn>立刻团购</a></td>\
  </tr>\
  </table>\
 </div>\
</div>\
';
    $id('main').innerHTML = s ? s + '<div style="clear:both"></div>': '<div style="margin-top:10px" align=center>没有找到，请选择其他城市或分类</div>';
    updateHeight();
}
function getGoods(s) {
    ajaxGet(G_dirUrl+'images/tuangou/Ajax_value.asp?v=city&s='+escape('http://apps.1616.net/tuangou/_tuangou.php?' + s)+'&tk='+new Date(), cbGetGoods);
}
function cityClick(a) {
    var o = $id('ct0' + theCity);
    if (o) o.className = '';
    $id('ct' + theCity).className = '';
    getGoods('city=' + pyCity(text(a)));
    toggleCity();
    return false;
}
function cateClick() {
    getGoods('city=' + theCity + '&cate=' + this.id.substr(4));
    return false;
}
function pageTo(n) {
    if (n == thePage || n < 1 || n > pageCount) return;
    getGoods('city=' + theCity + '&cate=' + theCate + '&page=' + n);
}
var xhr = window.XMLHttpRequest ?
function() {
    return new XMLHttpRequest();
}: function() {
    return new ActiveXObject("Microsoft.XMLHTTP");
};
function ajaxGet(url, callback) {
    if (busy) return;
    busy = true;
    var x = xhr();
    x.open('GET', url + (url.indexOf('?') == -1 ? '?': '&') + 'r=' + (Math.random() + '').substr(2), true);
    x.send(null);
    x.onreadystatechange = function() {
        if (x.readyState == 4) {
            busy = false;
            if (x.status == 200) callback(x.responseText);
        }
    };
}
void
function(a) {
    var d = $id('d1');
    a = a.slice(0, 20);
    var s = '';
    for (var i = 0; i < a.length; i++) s += '<div>' + a[i][0].link(a[i][1]) + '</div>';
    s += '<div><a href="http://tuangou.xungou.com/all.html" class=more>更多&gt;&gt;</a></div>';
    d.innerHTML = s + '<p style="clear:both"></p>';
} (linkList);
void
function(a) {
    var d = $id('d2');
    d.innerHTML = '';
    var D = function(s) {
        var d = document.createElement('div');
        d.id = s;
        return d;
    };
    var d1 = d.appendChild(D('ctop'));
    var d2 = d.appendChild(D('clist'));
    var c = [[]];
    for (var i = 0; i < cityList.length; i++) {
        var j = cityList[i][1].charCodeAt(0) - 96;
        if (!c[j]) c[j] = [];
        switch (cityList[i][1]) {
        case 'quanguo':
        case 'beijing':
        case 'shanghai':
        case 'guangzhou':
        case 'shenzhen':
            c[0].push(cityList[i]);
            c[j].unshift(cityList[i]);
            break;
        default:
            c[j].push(cityList[i]);
        }
    }
    var s = '<table cellpadding=0 cellspacing=4 width=592 id=t>';
    for (var i = 1; i < 27; i++) {
        if (c[i]) {
            s += '<tr><td width=30 valign=top align=center><span>' + String.fromCharCode(i + 64) + '</span></td><td>';
            for (j = 0; c[i][j]; j++) {
                s += '<div><a href=# onclick="return cityClick(this)" id=ct' + c[i][j][2] + '>' + c[i][j][0] + '</a></div>';
            }
            s += '</td></tr>';
        }
    }
    s += '</table>';
    d2.innerHTML = s;
    s = '';
    for (i = 0; c[0][i]; i++) s += '<div><a href=# onclick="return cityClick(this)" id=ct0' + c[0][i][1] + '>' + c[0][i][0] + '</a></div>';
    d1.innerHTML = s + '<span onclick="toggleCity()"></span>';
} (cityList);
$id('go_index').onclick = function() {
    pageTo(1);
    return false;
};
$id('go_prev').onclick = function() {
    pageTo(thePage - 1);
    return false;
};
$id('go_next').onclick = function() {
    pageTo(thePage + 1);
    return false;
};
$id('go_end').onclick = function() {
    pageTo(pageCount);
    return false;
};
$id('go_page').onchange = function() {
    pageTo( + this.value);
    this.blur();
};
void
function(a) {
    for (var i = 0; a[i]; i++) a[i].onclick = cateClick;
} ($id('cs').getElementsByTagName('a'));
void
function(f, s) {
    var k = f.key;
    k.onfocus = function() {
        if (this.value == s) {
            this.value = '';
            this.style.color = '#000';
        }
    };
    k.onblur = function() {
        if (!this.value) {
            this.value = s;
            this.style.color = '#999';
        }
    };
    k.value = s;
    k.style.color = '#999';
    function setCharset(s) {
        f.setAttribute('accept-charset', s);
        if (ie) document.charset = s;
    }
    function back() {
        setCharset('UTF-8');
    }
    f.onsubmit = function() {
        if (!k.value || k.value == s) return false;
        setCharset('GB2312');
        setTimeout(back, 1);
    };
} (document.forms[0], '请输入团购信息');
function toTop() {
    var n = 125;
    if (self == parent) window.scrollTo(0, n);
    else {
        try {
            parent.scrollTo(0, parent.$(frameElement).offset().top + n);
        } catch(e) {}
    }
}
function updateHeight() {
    if (self == parent) return;
    try {
        getIns('tuangou').__updateHeight(true);
    } catch(e) {
        try {
            tg2.__updateHeight();
        } catch(e) {}
    }
}
if (self != parent) {
    try {
        parent.J.add('tuangou', tg2, window);
    } catch(e) {
        crossdomain = true;
    }
}
function WData_callback() {
    getGoods('city=' + WData[2]);
}
if (theCity) getGoods('city=' + theCity);
else void
function() {
    var h = document.getElementsByTagName('head')[0];
    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.src = G_dirUrl+'images/tuangou/js/city.s.js';
    h.appendChild(s);
} ();
onerror = function(msg, url, line) {
    if (/iptolocal/.test(url)) {
        getGoods('city=');
        return true;
    }
};
var _scroll;
if (self != parent) {
    _scroll = function() {
        if (!lazy) return;
        lazy = false;
        for (var i = 6; i < list.length; i++) $id('item' + i).getElementsByTagName('img')[0].src = list[i].VC2THUMB;
    };
    try {
        if (window.attachEvent) {
            parent.attachEvent('onscroll', _scroll);
        } else {
            parent.$(parent).bind('scroll', _scroll);
        }
    } catch(e) {}
}
var euc = encodeURIComponent;
function stat(s, f) {
    $id(s).onclick = function(e) {
        if (!e) e = window.event;
        var t = e.target || e.srcElement;
        if (t.tagName == 'A')(new Image).src = 'http://st.1616.net/s.php?act=17&' + f(t);
    };
}
stat('d1',
function(t) {
    return 'r=0&p=' + euc(text(t));
});
stat('main',
function(t) {
    do t = t.parentNode;
    while (t.className != 'item');
    return 'r=1&p=' + ( + t.id.substr(4) + 1 + (thePage - 1) * pageSize) + '&c=' + euc(text($id('c'))) + '&t=' + euc(text($id('cate' + theCate)));
});