function toggleLayer(i,t,path,img1,img2) {
  var i = document.getElementById(i)||i;
  var t = document.getElementById(t)||t;
  if(t.style.display!='none') {
    t.style.display = 'none';
    i.innerHTML = '<img src="'+path+'/'+img1+'" alt="SHOW" />';
  } else {
    t.style.display = 'block';
    i.innerHTML = '<img src="'+path+'/'+img2+'" alt="HIDE" />';
  }
  return false;
}
function init_brandList(){
  var brandinput = $('#bsearch input');
  var brandlist = $('#bsearch select');
  var brandname = brandlist.children(':selected').text();
  var t;
  $('#bsearch img[class=button]').click(function(){brandlist.slideDown();});
  brandinput.val(brandname?brandname:'BRAND LIST');
  brandinput.click(function(){
    brandinput.val('');
    brandlist.slideDown();
  }).keyup(function(event){
    var haystack = '';
    var needle = $(this).val().toLowerCase();
    var n = brandlist.children().length;
    for(i=0;i<n;i++){
      haystack = brandlist.children().eq(i).text().toLowerCase();
      pos = haystack.indexOf(needle);
      if(pos == 0){brandlist.children().eq(i).attr('selected','selected');break;}
    }
  }).submit(function(){
      brandinput.val(brandlist.children(':selected').text());
  });
  brandlist.click(function(){$('#bsearch').submit();});
  $('#bsearch')
    .mouseleave(function(){t=setTimeout(function(){brandlist.slideUp();},1000);})
    .mouseenter(function(){clearTimeout(t);});
}
function init_submenu() {
  var t;
  $('#mainMenu a')
    .click(function(){
      $('#subMenu:visible').stop().slideUp();
      $('#subMenu:hidden').slideDown().focus();
      return false;})
    .mouseenter(function(){t=setTimeout(function(){$('#subMenu:hidden').slideDown();},1000);})
    .mouseleave(function(){clearTimeout(t);})
    .focus(function(){$(this).blur();});
  $('#menuWrap .open').click(function(){$(this).blur();$('#subMenu').stop().slideDown();return false;});
  $('#menuWrap .close').click(function(){$(this).blur();$('#subMenu').slideUp();return false;});
  $('body').click(function(){clearTimeout(t);$('#subMenu').slideUp();});
}
function init_bookmark() {
  $('#bookmark').click(function(){CreateBookmark();});
}
function chasing(oid, topPos, topLimit, btmLimit, duration){
	var obj = $(oid);
	var btmLimit = $(document).height() - obj.height() - btmLimit;
	if(topPos > 0){pos = $(document).scrollTop() + topPos;} else {pos = $(document).scrollTop() + $(window).innerHeight() - obj.height() + topPos;} 
	if(pos > btmLimit){pos = btmLimit;}
	if(pos < topLimit){pos = topLimit;}
	var topOffset = $(document).scrollTop() - obj.height() - 100;
	var btmOffset = $(document).scrollTop() + $(window).height() + 100;
  if(obj.position().top < topOffset) obj.stop().css({top:topOffset});
  if(obj.position().top > btmOffset) obj.stop().css({top:btmOffset});
	obj.stop().animate({top:pos},duration,'easeinout');
}
function init_chasing() {
  $('#todayGoods').height($('#todayGoods li:first').height());
  $(window).scroll(function(){chasing('#chasingRight',100,140,100,800);});
  $(window).scroll(function(){chasing('#eventLeft',100,140,100,800);});
}
function init_todayGoods() {
  $('#todayGoods').cycle({ 
    next:     '#todayGoodsWrap a.next', 
    prev:     '#todayGoodsWrap a.prev',
    fx:       'scrollHorz', 
    speed:    1000,
    easing:   'easeinout',
    timeout:  6000,
    pause:    1,
    delay:    -1000
  });
}
$.fn.wait = function(time, type) {
  time = time || 1000;
  type = type || "fx";
  return this.queue(type, function() {
    var self = this;
    setTimeout(function() {
      $(self).dequeue();
    }, time);
  });
};
function carousel(o,w,d,s,r) {
  w = w || 234;
  s = s || '-';
  d = d || 2000;
  if(s == '-') {
    if(r){
      o.children().eq(0).clone().appendTo(o).parent()
      .wait()
      .animate({left:-w},d,'easeinout',function(){
        o.css({left:0}).children().eq(0).remove();
        carousel(o,w,d,s,r);
      });
    } else {
      o.animate({left:-w},d,'easeinout',function(){
        o.css({left:0}).children().eq(0).remove();
        carousel(o,w,d,s,true);
      });
    }
  } else {
    if(r){
      var n = o.children().length;
      o.children().eq(n-1).clone().prependTo(o).parent().css({left:-w})
      .wait()
      .animate({left:0},d,'easeinout',function(){
        o.children().eq(n).remove();
        carousel(o,w,d,s,r);
      });
    } else {
      var n = o.children().length;
      o.animate({left:0},d,'easeinout',function(){
        o.children().eq(n-1).remove();
        carousel(o,w,d,s,true);
      });
    }
  }
}
function init_rolling(id,vn) {
  if(!id) return false;
  vn = vn ? vn : 4;
  var carouselSign = '-';
  var d = 2000;
  var o = $(id);
  var n = parseInt(o.children().length);
  if(n<vn) return false;
//  o.css({display:'none'}).shuffle().css({display:'block'});
  var j = 0;
  for(i=n;i<vn;i++) {
    o.children().eq(j).clone().appendTo(o);
    j++;
    n++;
  }
  var w = parseInt(o.children().eq(0).width()) + 8;
  o.width(w * (n + 2)).hover(function(){
    o.stop(true,false);
  },function(){
    carousel(o,w,d,carouselSign,false);
  });
  o.parent().parent().children('a.prev').click(function(){
    if(carouselSign == '+') {
      o.stop(true,false);
      carouselSign = '-';
      carousel(o,w,d,carouselSign,false);
    }
    return false;
  });
  o.parent().parent().children('a.next').click(function(){
    if(carouselSign == '-') {
      o.stop(true,false);
      carouselSign = '+';
      carousel(o,w,d,carouselSign,false);
    }
    return false;
  });
  setTimeout(function(){carousel(o,w,d,carouselSign,true)},d);
}
$(document).ready(function(){
  $('.scrollto').scrollToHash();
  init_brandList();
  init_submenu();
  init_chasing();
  init_todayGoods();
  init_rolling('#rollingBanner');
});
