
//初始化mywebphp
var xwjs = jQuery.noConflict();

//创建js对象三部曲
var sunweihua = function(){};
sunweihua.prototype.$= xwjs;
sunweihua.fn = sunweihua.prototype;
var mywebphp = new sunweihua();




sunweihua.prototype.search_form_blur = function(){

    var _$ = this.$;

    _$('#keyword').bind('focus',function(){

        _$('#keyword').val('');
    });
}

sunweihua.prototype.slide_init = function(){

    var _$ = this.$;

    var that = this;

    var mw_auto = setInterval(this.slide_img_auto,4000);

    _$('#slide_num span').bind('mouseenter',function(){



       window.clearInterval(mw_auto);
       _$(document).stop();
       _$(document).clearQueue();

       _$(this).children('p').addClass('curr').end().siblings('span').children('p').removeClass('curr');


    //计算当前li在整个li队列中的编号
    var index_curr = _$('#slide_num span').index(this);
    var left_pos = 0;
    left_pos = left_pos - (index_curr*950);
    left_pos = left_pos + 'px';

       //设置动画效果
       _$('ul#slide_img').animate({'left':left_pos},'slow');

    });

    _$('#slide_num span').bind('mouseleave',function(){

        mw_auto = setInterval(that.slide_img_auto,4000);

    });

}

sunweihua.prototype.slide_img_auto = function(){

    var  _$ = xwjs;
    _$('ul#slide_img li').each(function(i,item){
        var left_pos =  - (i*950);
        left_pos = left_pos + 'px';

       _$(document).delay(4000).queue(function(){

           _$('#slide_num span').eq(i).children('p').addClass('curr').end().siblings().children('p').removeClass('curr');
           _$('ul#slide_img').animate({'left':left_pos},'slow');
           _$(this).dequeue();
       });
    });
}



var obj2str = function(c) {

	var b = [];
	if (typeof c == "string") {
		return '"' + c.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + '"'
	}
	if (typeof c == "undefined") {
		return "undefined"
	}
	if (typeof c == "object") {
		if (c === null) {
			return "null"
		} else {
			if (!c.sort) {
				for (var a in c) {
					if (a != "toJSONString") {
						b.push('"' + a + '":' + obj2str(c[a]))
					}
				}
				b = "{" + b.join() + "}"
			} else {
				for (var a = 0; a < c.length; a++) {
					b.push(obj2str(c[a]))
				}
				b = "[" + b.join() + "]"
			}
		}
		return b
	}
	return c.toString()
}

sunweihua.prototype.attr_init = function(){

    var _$ = this.$;
    _$('#mywebphp_goods_attr span.attr_span').bind('click',function(){

        _$(this).addClass('attr_span_curr').siblings('span').removeClass('attr_span_curr');
        var this_rel = _$(this).attr('rel');

        _$('#spec_value_' + this_rel).attr('checked', true);

        changePrice();
    });


    _$.each(_$('#mywebphp_goods_attr .attr_div') , function(i,item){

        _$(item).children('.attr_span').eq(0).addClass('attr_span_curr');

    });

    //属性块背景效果
    _$('#mywebphp_goods_attr').bind('mouseover',function(){

        _$(this).addClass('div_curr');
    }).bind('mouseout',function(){

        _$(this).removeClass('div_curr');

    });

    //加号和减号点击效果

    _$('span.span_sub').bind('click',function(){

        var this_number = _$('#number').val();

        this_number = parseInt(this_number);

        if(this_number == 1){

            alert('购买数量最小为1');
        }

        if(this_number >1){

            _$('#number').val(this_number - 1 );
            changePrice();
        }

    })

    _$('span.span_sub').bind('mouseover',function(){

        _$(this).addClass('span_sub_curr');

    }).bind('mouseout',function(){

        _$(this).removeClass('span_sub_curr');

    });

    _$('span.span_add').bind('click',function(){

        var this_number = _$('#number').val();

        this_number = parseInt(this_number);

         _$('#number').val(this_number  + 1 );


         changePrice();
    })

    _$('span.span_add').bind('mouseover',function(){

        _$(this).addClass('span_add_curr');

    }).bind('mouseout',function(){

        _$(this).removeClass('span_add_curr');

    });
}

sunweihua.prototype.add_to_cart = function(goods_id){

  var goods        = new Object();
  var spec_arr     = new Array();
  var fittings_arr = new Array();
  var number       = 1;
  var formBuy      = document.forms['ECS_FORMBUY'];
  var quick        = 0;

  // 检查是否有商品规格
  if (formBuy)
  {
    spec_arr = getSelectedAttributes(formBuy);

    if (formBuy.elements['number'])
    {
      number = formBuy.elements['number'].value;
    }

    quick = 1;
  }

  goods.quick    = quick;
  goods.spec     = spec_arr;
  goods.goods_id = goodsId;
  goods.number   = number;
  goods.parent   = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);

  Ajax.call('flow.php?step=add_to_cart', 'goods=' + obj2str(goods), mwaddToCartResponse, 'POST', 'JSON');

}

/* *
 * 处理添加商品到购物车的反馈信息
 */
function mwaddToCartResponse(result)
{
  if (result.error > 0)
  {
    // 如果需要缺货登记，跳转
    if (result.error == 2)
    {
      if (confirm(result.message))
      {
        location.href = 'user.php?act=add_booking&id=' + result.goods_id + '&spec=' + result.product_spec;
      }
    }
    // 没选规格，弹出属性选择框
    else if (result.error == 6)
    {
      openSpeDiv(result.message, result.goods_id, result.parent);
    }
    else
    {
      alert(result.message);
    }
  }
  else
  {

    //如果成功添加 则弹出购物车信息

    var _$ = xwjs;

    result.content = '<div class="title"><p>商品已经成功添加到购物车!</p></div><p>' + result.content + '</p>';

    _$('#mw_cart_info_bb').html(result.content);

    _$('#mw_cart_info').fadeIn('slow').delay(5000).fadeOut();

    _$('#mw_cart_info_bb').css('opacity',0.8);

  }
}


//商品切换选项卡
sunweihua.prototype.goods_init = function(){

    var _$ = this.$;
    var _that = this;

    _$('ul#mw_ul_1 li').bind('click',function(){

        var curr_index = _$('ul#mw_ul_1 li').index(_$(this));

        _$(this).addClass('curr').siblings('li').removeClass('curr');

        _$('.goods_xq_item').eq(curr_index).fadeIn().siblings('div').hide();

    });
}
















sunweihua.prototype.add_bookmarket = function(){

           var _$= xwjs;

            _$("#mw-addbookmarket").click(function () {　　　　//$里面是链接的id

                var ctrl = (navigator.userAgent.toLowerCase()).indexOf('mac') != -1 ? 'Command/Cmd' : 'CTRL';

                var href_url = _$(this).attr('title');
                var site_name = _$(this).attr('site_name');

               if (document.all) {

                    window.external.addFavorite(href_url, site_name)

                } else if (window.sidebar) {

                    window.sidebar.addPanel(site_name , href_url , "")

                } else {　　　　//添加收藏的快捷键

                    alert('添加失败\n您可以尝试通过快捷键' + ctrl + ' + D 加入到收藏夹~')

                }

            });
}




