/*
plusminuszero.js
update: 2011-01-22
*/

/* jquery tooltip plugin */
(function($){
    $.fn.easyToolTip = function(options){
        var toolTip = $('<div id="toolTip"></div>').hide();
        $('body').append(toolTip);
        
        $(this).filter(function(){
            return this.title || this.alt;
        }).each(function(){
        
            var self = $(this);
            var target = this.title ? 'title' : 'alt';
            var tipText = self.attr(target);
            
            self.hover(function(e){
                self.attr(target,'');
                toolTip
                    .stop(true,true)
                    .fadeIn('slow')
                    .text(tipText)
                    .css({
                        position: 'absolute',
                        top: e.pageY - 20,
                        left: e.pageX + 20
                    })
            },function(){
                self.attr(target,tipText);
                toolTip.fadeOut('fast');
            }).mousemove(function(e){
                toolTip.css({
                    top: e.pageY - 20,
                    left: e.pageX + 20
                });
            });
        });
    }
})(jQuery);

/* jquery shuffle plugin */
(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }

  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);


/* -- for search -- */
jQuery(document).bind("ready", function(){
  $('#search').focus(function(){
    $(this).val("");
  });
});

/* -- scroll to Top -- */
jQuery(document).bind("ready", function(){
  $('.pagetop a').click(function(){
    var pageTop = $("#wrapper").scrollTop();
    $($.browser.opera ? document.compatMode == 'BackCompat' ? 'body' : 'html' :'html,body').animate({scrollTop:pageTop},500);return false;
  });
});

/* -- for top -- */
var arr = [1,2,3,4,5,6,7,8,9];
arr = $.shuffle(arr);

jQuery(document).bind("ready", function(){
  $('.home ul.images').css('height','365px');
  $('#.home ul.images li').hide();
  $(arr).each(function(k,n){
    var num = k * 500;
    $(".home ul.images li:nth-child(" + n + ")").delay(num).fadeIn(800);
  });
  $('ul.images img').easyToolTip();
});

/* -- for category top -- */
var arr2 = [1,2,3,4,5,6,7,8,9];
arr2 = $.shuffle(arr2);

jQuery(document).bind("ready", function(){
  $('ul.images li').hide();
  $(arr2).each(function(k2,n2){
    var num2 = k2 * 500;
    $("ul.images li:nth-child(" + n2 + ")").delay(num2).fadeIn(800);
  });
});

/* -- for product gallery -- */
jQuery(document).bind("ready", function(){
  var l = $('.products_gallery .image_box img').length;
  $('.products_gallery .image_box img').hide();
  for(var i=1;i<=l;i++){
    $('<li id="btn'+i+'"></li>').appendTo('.products_gallery .liststyleA ul');
  }
  $('<img alt=" " width="12" height="12" />').attr('src','../../../common/images/ring_brn.gif').attr('style','cursor: pointer;').appendTo('.products_gallery .liststyleA ul li');
  $('.products_gallery .image_box img#img1').fadeIn(1000);
  $('.products_gallery .liststyleA ul li#btn1 img').attr('src','../../../common/images/ring_brn_on.gif');
  var current = 1;
  var next;
  $('.products_gallery .liststyleA ul li img').mouseover(function(){
    $(this).attr('src','../../../common/images/ring_brn_on.gif');
  });
  $('.products_gallery .liststyleA ul li img').mouseout(function(){
    var thisLi = $(this).parent().attr('id').slice(-1);
    if(current != thisLi){
      $(this).attr('src','../../../common/images/ring_brn.gif');
    }
  });
  
  $('.products_gallery .image_box img').click(function(){
    current = $(this).attr('id').slice(-1);
    if(current < l){
      next = Number(current)+1;
    }else if(current == l){
      next = 1;
    }
    $('.products_gallery .liststyleA ul li#btn'+current+' img').attr('src','../../../common/images/ring_brn.gif');
    $(this).fadeOut("fast",function(){
      $('.products_gallery .image_box img#img'+next).fadeIn(1000);
      $('.products_gallery .liststyleA ul li#btn'+next+' img').attr('src','../../../common/images/ring_brn_on.gif');
      current = next;
    });
  });
  $('.products_gallery .liststyleA ul li img').click(function(){
    $('.products_gallery .liststyleA ul li#btn'+current+' img').attr('src','../../../common/images/ring_brn.gif');
    next = $(this).parent().attr('id').slice(-1);
    $('.products_gallery .image_box img#img'+current).fadeOut("fast",function(){
      $('.products_gallery .image_box img#img'+next).fadeIn(1000);
      $('.products_gallery .liststyleA ul li#btn'+next+' img').attr('src','../../../common/images/ring_brn_on.gif');
      current = next;
    });
  });
});

/* -- for product detail -- */
jQuery(document).bind("ready", function(){
  //color
  var l2 = $('.products_detail .top_image img').length;
  $('.products_detail .top_image img').hide();
  $('.products_detail .top_image img#img1').fadeIn(1000);
  var current2 = 1;
  var next2;
  if(l2 > 1){
    $('.products_detail .top_image img').click(function(){
      current2 = $(this).attr('id').slice(-1);
      if(current2 < l2){
        next2 = Number(current2)+1;
      }else if(current2 == l2){
        next2 = 1;
      }
      $(this).fadeOut("fast",function(){
        $('.products_detail .top_image img#img'+next2).fadeIn(1000);
        current2 = next2;
      });
    });
    $('.products_detail .color_nav ul li').click(function(){
      var next2 = $(this).attr('id').slice(-1);
      $('.products_detail .top_image img#img'+current2).fadeOut("fast",function(){
        $('.products_detail .top_image img#img'+next2).fadeIn(1000);
        current2 = next2;
      });
    });
  }else{
    $('.products_detail .top_image img').css('cursor','default');
  }
  //color series
  $('.products_detail .series_product .image img').hide();
  $('.products_detail .series_product .image img.img1').fadeIn(1000);
  var current3 = new Array();
  var next3 = new Array();
  $('.products_detail .series').each(function(){
    current3[$(this).children().attr('id')] = 1;
    next3[$(this).children().attr('id')] = '';
  });
  $('.products_detail .series_product .image').each(function(){
    var length = $(this).children().length;
    if(length > 1) {
      $(this).children().css('cursor','pointer');
    }
  });
  $('.products_detail .series_product .image img').click(function(){
    var currentPId = $(this).parent().parent().attr('id');
    var l3 = $(this).parent().children().length;
    if(l3 > 1){
      current3[currentPId] = $(this).attr('class').slice(-1);
      if(current3[currentPId] < l3){
        next3[currentPId] = Number(current3[currentPId])+1;
      }else if(current3[currentPId] == l3){
        next3[currentPId] = 1;
      }
      $(this).fadeOut("fast",function(){
        $('.products_detail #'+currentPId+' .image img.img'+next3[currentPId]).fadeIn(1000);
        current3[currentPId] = next3[currentPId];
      });
    }
  });
  $('.products_detail .series_product .color_nav_series ul li').click(function(){
    var currentPId2 = $(this).parent().parent().parent().attr('id');
    var l4 = $(this).parent().children().length;
    if(l4 > 1){
      next3[currentPId2] = $(this).attr('class').slice(-1);
      if(!current3[currentPId2]){
        current3[currentPId2] = 1;
      }
      $('.products_detail #'+currentPId2+' .image img.img'+current3[currentPId2]).fadeOut("fast",function(){
        $('.products_detail #'+currentPId2+' .image img.img'+next3[currentPId2]).fadeIn(1000);
        current3[currentPId2] = next3[currentPId2];
      });
    }
  });

  
  //tab
  $('.products_detail .tab-block').hide();
  $('.products_detail #tab1').fadeIn(1000);
  var thisTab = 'tab1';
  $('.products_detail .tab-nav ul li').click(function(){
    var nextTab = $(this).attr("class");
    $('.products_detail #'+thisTab).fadeOut("fast",function(){
      $('.products_detail #'+nextTab).fadeIn(1000);
      thisTab = nextTab;
    });
  });
});

/* -- for faq index -- */
$(function(){
  $(".faq_index .liststyleB li:nth-child(3n)").css("clear","left");
});



