if (! window.Maihao ) {
        window.Maihao = new Object();
}
Maihao.Groupon = {
    init: function()
    {
       // Maihao.Groupon.enableAjaxCart();
		//Maihao.Groupon.enableAjaxCart_2();
        Maihao.Groupon.enableGoodsCounter();
        Maihao.Groupon.enableOtherGoods2();
		Maihao.Groupon.enableOtherGoods3();
        Maihao.Groupon.enableDealIntro();
        Maihao.Groupon.enablePicLazyload();
        Maihao.Groupon.enableGoodsAttrSelect();
        Maihao.Groupon.enableSideSearchTips();
        Maihao.Groupon.enableSideCheckSearchForm();
        Maihao.Groupon.enableActivityClothingGoods();
        Maihao.Groupon.enableShopSuperiority();
    },
    enableAjaxCart: function()
    {
        /* 一日多团添加购物车按钮,主推产品添加购物车按钮,今日精选,右侧栏即刻抢购按钮,今日团购添加购物车按钮  */
        var ajaxCartObj = new Array();
        ajaxCartObj.push($('.other-goods2 .deal-intro .deal-img .add-cart'));
        ajaxCartObj.push($('#deal-stuff .deal_tabs li.buy span a'));
        ajaxCartObj.push($('.other-goods .deal-intro .deal-img .add-cart'));
        ajaxCartObj.push($('.today-other-teams .deals-list .buy span a'));
        ajaxCartObj.push($('#deal-intro .deal-price a'));
        ajaxCartObj.push($('#deal-intro .tips .add-cart'));
        ajaxCartObj.push($('#sidebar .side-goods .sbox-content li .bottom .buy'));
        ajaxCartObj.push($('#recent-deals .detail li .add-cart a'));
        ajaxCartObj.push($('.activity-item-addToCart'));
        for ( var i=0; i < ajaxCartObj.length; i++ ) {
            if ( ajaxCartObj[i].length ) {
                ajaxCartObj[i].each(ajaxCart);
            }
        }
        //$('.other-goods2 .deal-intro .deal-img .add-cart,#deal-stuff .deal_tabs li.buy span a,.other-goods .deal-intro .deal-img .add-cart,.today-other-teams .deals-list .buy span a,#deal-intro .deal-price a,#deal-intro .tips .add-cart,#sidebar .side-goods .sbox-content li .bottom .buy,#recent-deals .detail li .add-cart a').each(ajaxCart);
    },
	enableAjaxCart_2: function()
    {
        /* 一日多团添加购物车按钮,主推产品添加购物车按钮,今日精选,右侧栏即刻抢购按钮,今日团购添加购物车按钮  */
        var ajaxCartObj = new Array();
        ajaxCartObj.push($('.other-goods2 .deal-intro .deal-img .add-cart'));
		ajaxCartObj.push($('.other-goods3 .deal_info .common_img .add-cart'));
        for ( var i=0; i < ajaxCartObj.length; i++ ) {
            if ( ajaxCartObj[i].length ) {
                ajaxCartObj[i].each(ajaxCart);
            }
        }
    },
    enableGoodsCounter: function()
    {
        var $this = $('#counter,.counter');
        if ( $this.length > 0 ) {
            var _grouponCountTime = function(diffTime, $obj) {
                var ld,ls,lh,lm;
                diffTime = parseInt(diffTime * 1000);
                ls = diffTime;
                if ( ls > 0 ) {
                    // 取天数
                    ld = parseInt(ls / 86400000).toString();
                    ls = ls % 86400000
                    // 取小时数
                    lh = parseInt(ls / 3600000).toString();
                    lh = lh.length > 1 ? lh: '0' + lh;
                    ls = ls % 3600000;
                    // 取分钟数
                    lm = parseInt(ls / 60000).toString();
                    lm = lm.length > 1 ? lm: '0' + lm;
                    // 取秒数
                    ls = parseInt((ls % 60000) / 1000);
                    ls = ls.toString().length > 1 ? ls: '0' + ls;
                    if ( parseInt(ld) > 0 )  {
						ld = ld.length > 1 ? ld: '0' + ld;
                        timeHtml  = '<span>' + ld + '</span>天<span>' + lh + '</span>时';
                        timeHtml += '<span>' + lm + '</span>分<span>' + ls + '</span>秒';
                    } else {
                        timeHtml  = '<font>' + lh + '</font>时<font>' + lm + '</font>分';
                        timeHtml += '<font>' + ls + '</font>秒';
                    }
                    $obj.html(timeHtml)
                } else {
                    if ( window.MH_MODULE  == 'Goods' && window.MH_ACTION  == 'show' ) {
                        $endTime = new Date($obj.attr("endTime"));
                        var Y = $endTime.getFullYear();
                        var m = $endTime.getMonth() + 1;
                        var d = $endTime.getDate();
                        var H = $endTime.getHours();
                        var i = $endTime.getMinutes();
                        var s = $endTime.getSeconds();
                        $obj.parent().parent().html("<div class=\"goods-endTime-desc\">本团购结束于<br />" + Y + "年" + m + "月" +  d + "日" +  H + "时" +  i + "分</div>");
                    } else {
                        $obj.html("已结束");
                    }
                }
            }
            var updEndNowTime = MH_SERVER_TIME;
            var $objs = $this;
            var objs  = [];
            var diffs = [];
            for ( var i = 0; i < $objs.length; i++ ) {
                objs.push($($objs[i]))
            }
            if (objs.length > 0) {
                nowTime = (new Date()).getTime();
                for ( var i = 0; i < objs.length; i++ ) {
                    var diff = ((new Date(objs[i].attr("endTime"))).getTime() - updEndNowTime) / 1000;
                    diffs.push(diff);
                    _grouponCountTime(diffs[i], objs[i])
                }
                var $window = $(window);
                setInterval(function() {
                    for ( var i = 0; i < objs.length; i++ ) {
                        diffs[i] = diffs[i] - 1
                    }
                    var winScrlTop = $window.scrollTop();
                    var winScrlTopHeight = $window.scrollTop() + $window.height();
                    for ( var i = 0; i < objs.length; i++) {
                        var pos = objs[i].offset();
                        if ( pos.top + 30 > winScrlTop && pos.top < winScrlTopHeight ) {
                            _grouponCountTime(diffs[i],objs[i]);
                        }
                    }
                }, 1000);
            }
        }
    },
	enableAjaxCounter: function()
    {
        var $this = $('#new_container .counter');
        if ( $this.length > 0 ) {
            var _grouponCountTime = function(diffTime, $obj) {
                var ld,ls,lh,lm;
                diffTime = parseInt(diffTime * 1000);
                ls = diffTime;
                if ( ls > 0 ) {
                    // 取天数
                    ld = parseInt(ls / 86400000).toString();
                    ls = ls % 86400000
                    // 取小时数
                    lh = parseInt(ls / 3600000).toString();
                    lh = lh.length > 1 ? lh: '0' + lh;
                    ls = ls % 3600000;
                    // 取分钟数
                    lm = parseInt(ls / 60000).toString();
                    lm = lm.length > 1 ? lm: '0' + lm;
                    // 取秒数
                    ls = parseInt((ls % 60000) / 1000);
                    ls = ls.toString().length > 1 ? ls: '0' + ls;
                    if ( parseInt(ld) > 0 )  {
						ld = ld.length > 1 ? ld: '0' + ld;
                        timeHtml  = '<span>' + ld + '</span>天<span>' + lh + '</span>时';
                        timeHtml += '<span>' + lm + '</span>分<span>' + ls + '</span>秒';
                    } else {
                        timeHtml  = '<font>' + lh + '</font>时<font>' + lm + '</font>分';
                        timeHtml += '<font>' + ls + '</font>秒';
                    }
                    $obj.html(timeHtml)
                } else {
                    if ( MH_MODULE  == 'Goods' && MH_ACTION  == 'show' ) {
                        $endTime = new Date($obj.attr("endTime"));
                        var Y = $endTime.getFullYear();
                        var m = $endTime.getMonth() + 1;
                        var d = $endTime.getDate();
                        var H = $endTime.getHours();
                        var i = $endTime.getMinutes();
                        var s = $endTime.getSeconds();
                        $obj.parent().parent().html("<div class=\"goods-endTime-desc\">本团购结束于<br />" + Y + "年" + m + "月" +  d + "日" +  H + "时" +  i + "分</div>");
                    } else {
                        $obj.html("已结束");
                    }
                }
            }
            var updEndNowTime = MH_SERVER_TIME;
            var $objs = $this;
            var objs  = [];
            var diffs = [];
            for ( var i = 0; i < $objs.length; i++ ) {
                objs.push($($objs[i]))
            }
            if (objs.length > 0) {
                nowTime = (new Date()).getTime();
                for ( var i = 0; i < objs.length; i++ ) {
                    var diff = ((new Date(objs[i].attr("endTime"))).getTime() - updEndNowTime) / 1000;
                    diffs.push(diff);
                    _grouponCountTime(diffs[i], objs[i])
                }
                var $window = $(window);
                setInterval(function() {
                    for ( var i = 0; i < objs.length; i++ ) {
                        diffs[i] = diffs[i] - 1
                    }
                    var winScrlTop = $window.scrollTop();
                    var winScrlTopHeight = $window.scrollTop() + $window.height();
                    for ( var i = 0; i < objs.length; i++) {
                        var pos = objs[i].offset();
                        if ( pos.top + 30 > winScrlTop && pos.top < winScrlTopHeight ) {
                            _grouponCountTime(diffs[i],objs[i]);
                        }
                    }
                }, 1000);
            }
        }
    },
    enableOtherGoods2: function()
    {
        $this = $('.other-goods2');
        if ( $this ) {
            $this.hover(
                function()
                {
                    $(this).addClass('hover');
                    if ($(this).find('.sellout').length) {
                        $(this).find('.deal-intro .deal-img .add-cart').css('opacity', 0);
                    } else {
                        $('.other-goods2 .deal-intro .deal-img .add-cart').css('opacity', 1);
                    }
                },
                function()
                {
                    $(this).removeClass('hover');
                }
            );
            $this.find('.sellout').each(function(){
                $(this).parent().find('.deal-shopping').css('filter', 'gray');
                $(this).parent().find('.deal-shopping').css('backgroundImage','url(/app/Tpl/17mh/Public/images/deal-shopping-bg2.jpg)');
                $(this).parent().find('.deal-shopping .go-look img').attr('src','/app/Tpl/17mh/Public/images/bg-deal-soldout-text.gif');
                $(this).parent().find('.limitdate-icon').html('已结束');
            });
        }
    },
	enableOtherGoods3: function()
    {
        $this = $('.other-goods3');
        if ( $this ) {
            $this.hover(
                function()
                {
                    $(this).addClass('goods3_hover');
                    if ($(this).find('.sellout').length) {
                        $(this).find('.deal_info .common_img .add-cart').css('opacity', 0);
                    } else {
                        $('.other-goods3 .deal_info .common_img .add-cart').css('opacity', 1);
                    }
                },
                function()
                {
                    $(this).removeClass('goods3_hover');
                }
            );
        }
    },
    enableDealIntro: function()
    {
        $this = $('#deal-intro');
        $this.hover(
            function() {
                $(this).addClass('hover');
            },
            function() {
                $(this).removeClass('hover');
            }
        );
    },
    enablePicLazyload: function()
    {
        $this = new Array();
        $this.push($("#deal-stuff .detail_info .goods-detail img"));
        $this.push($("#deal-default #content .other-goods2 .deal-img img"));
        $this.push($("#sidebar .side-goods .sbox-content li img"));
        for ( var i=0; i < $this.length; i++ ) {
            if ( $this[i].length ) {
                $this[i].lazyload({
                    placeholder: MH_CND_URL + "/" + MH_TMPL_PATH + "Public/images/transparent.gif",
                    failurelimit: 10,
                    threshold: 200,
                    effect: "fadeIn"
                });
            }
        }
    },
    enableGoodsAttrSelect: function()
    {
        $this = $('#deal-intro .goods-attr-content-item');
        if ( $this.length ) {
            var goods_attr_value_str = $('#deal-intro .goods-attr-value-str').html();
            // 重新统计及显示已选择的属性
            var recountSelectedAttr = function($obj){
                var $this= $obj;
                var selectedStr = '';
                var selectedVal = '';
                $this.find('.goods-attr-content-item-list .selected').each(function(i){
                    selectedStr += "[" + $(this).text() + "]&nbsp;";
                    selectedVal += $(this).text() + ",";
                });
                $this.find('.goods-attr-selected-list').html(selectedStr);
                $this.find('.selected-goods-attr-value').html(selectedVal);
                $this.find('.selected-goods-attr-id').html('');
                var json_goods_attr_value_list = $this.find('.json-goods-attr-value-list').html();
                var goods_attr_value_list = eval('(' + json_goods_attr_value_list + ')');
                for ( var i in goods_attr_value_list ) {
                    if ( selectedVal == goods_attr_value_list[i] ) {
                        $this.find('.selected-goods-attr-id').html(i);
                        break;
                    }
                }
            }
            // 选择属性时触发的动作
            var select_attr_click = function(){
                var thisText = $(this).text();
                $(this).addClass('selected').siblings().removeClass('selected');
                var $currentAttr = $(this).parent().parent();
                var $nextItem = $currentAttr.next('.goods-attr-content-item');
                if ( $nextItem.length ) {
                    var goods_attr_value_str = $currentAttr.parent().find('.goods-attr-value-str').html();
                    var $prevAllItem = $currentAttr.prevAll('.goods-attr-content-item');
                    var selectedText = '';
                    $prevAllItem.each(function(){
                        var text = $prevAllItem.find('.selected').text();
                        if ( text != '' ) {
                            selectedText += text + ',';
                        }
                    });
                    selectedText += thisText + ',';
                    $nextItem.find('.goods-attr-content-item-list a').each(function(){
                        var partText = selectedText + $(this).text() + ',';
                        var cPos = goods_attr_value_str.indexOf(partText);
						/*
                        if ( cPos >= 0 && (cPos == 0 || goods_attr_value_str.substr(cPos-1, 1) == '|') )  {
                            $(this).removeClass('disabled');
                            $(this).bind('click', select_attr_click);
                        } else {
                            $(this).addClass('disabled').removeClass('selected');
                            $(this).unbind('click');
                            $(this).attr('onclick', '');
                        }*/
                        if ( cPos == 0  ) {
                            return;
                        } else {
                            var partText2 =  '|' + partText;
                            var cPos2 = goods_attr_value_str.indexOf(partText2);
                            if ( cPos2 > 0 ) {
                                return;
                            } else{
                                $(this).addClass('disabled').removeClass('selected');
                                $(this).unbind('click');
                                $(this).attr('onclick', '');
                            }
                        }
                    });
                }
                recountSelectedAttr($currentAttr.parent());
                return false;
            }
            // 选择后，重新操作赋值
            // recountSelectedAttr();
            // 绑定选择属性事件
            $this.find('.goods-attr-content-item-list a').bind('click', select_attr_click);
            // 初始化
            $this.eq(0).find('a').each(function(){
                var partText = $(this).text() + ',';
                var cPos = goods_attr_value_str.indexOf(partText);
				/*
                if ( cPos >= 0 && (cPos == 0 || goods_attr_value_str.substr(cPos-1, 1) == '|') )  {
                    return;
                } else {
                    $(this).addClass('disabled').removeClass('selected');
                    $(this).unbind('click');
                    $(this).attr('onclick', '');
                }
                */
                if ( cPos == 0  ) {
                    return;
                } else {
                    var partText2 =  '|' + partText;
                    var cPos2 = goods_attr_value_str.indexOf(partText2);
                    if ( cPos2 > 0 ) {
                        return;
                    } else{
                        $(this).addClass('disabled').removeClass('selected');
                        $(this).unbind('click');
                        $(this).attr('onclick', '');
                    }
                }
            });
        }
    },
    enableSideSearchTips: function()
    {
        $this = $('#side-search-nav-form');
        if ( $this.length ) {
            $.ajax({
                type: 'GET',
                cache: false,
                url: MH_DOMAIN_URL + '/shop/index-getSearchData.html?isAjaxRequest=1',
                dataType: 'json',
                success: function(data)
                {
                    if ( data.data.length ) {
                        // ["我c++", "cjava", "cphp", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"]
                        $('.side-search-box-keyword').autocomplete(data.data, {
                            resultsClass: "side-search-ac_results",
                            width: 185,
                            minChars: 1,
                            max: 20,
                            matchContains: true,
                            scroll: true,
                            scrollHeight: 300,
                            selectFirst: false
                        });
                    }
                }
            });
        }
    },
    enableSideCheckSearchForm: function()
    {
        $this = $('#side-search-nav-form');
        if ( $this.length ) {
            $(this).submit(function(){
                var keyword = $('.side-search-box-keyword').val();
                if ( keyword == '' ) {
                    alert('请输入搜索词！');
                    return false;
                } else {
                    return true;
                }
            });
        }
    },
    enableActivityClothingGoods: function()
    {
        $this = $('#activity-clothing .activity-item');
        if ( $this ) {
            $this.hover(
                function()
                {
                    $(this).addClass('hover');
                },
                function()
                {
                    $(this).removeClass('hover');
                }
            );
            $this.find('.activity-item-sellout').each(function(){
                $(this).parent().find('.activity-item-shopping').css({filter:'gray',backgroundImage:'url(/'+MH_TMPL_PATH+'/activity/clothing/images/shopping-price-sellout-bg.jpg)'});
                $(this).parent().find('.activity-item-boximg').css({border:'1px solid #5C5C5C'});
                $(this).parent().find('.activity-item-limitdate-icon').html('已结束');
                $(this).parent().unbind();
            });
        }
    },
    getTodayOtherList: function(page, goodsId, perRecords)
    {
        page = parseInt(page) > 0 ? parseInt(page) : 1;
        goodsId = parseInt(goodsId) > 0 ? parseInt(goodsId) : 0;
        perRecords = parseInt(perRecords) > 0 ? parseInt(perRecords) : 0;
        $.ajax({
            type: 'GET',
            cache: false,
            url: '/services/ajax.php?run=getTodayOtherList&page=' + page + '&goodsId=' + goodsId + '&perRecords=' + perRecords,
            dataType: 'json',
            success: function(data)
            {
                if ( data.status == 1 ) {
                    $('#sidebar .side-goods').replaceWith(data.msg);
                }
                return false;
            }
        });
        return false;
    },
    enableShopSuperiority: function()
    {
        $this = $('.shop-superiority-selector span');
        if ( $this.length ) {
            $('.shop-superiority-selector span').each(function(i){
                $(this).hover(
                    function(){
                        $(".shop-superiority-list div").eq(i).fadeIn(100).siblings().hide();
                    },
                    function()
                    {;}
                );
            });
        }
    }
};

