jQuery(document).ready(function($){
  
  /* Tiptip can't work without maxWidth */
  var IS_CSS_MAXWIDTH_SUPPORTED = (function() {
    var docEl = document.documentElement, s;
    if (docEl && (s = docEl.style)) {
      return typeof s.maxWidth == "string";
    }
    return null;
  })();
  
  /* Search_bar */
  var search_val = $('#search_text').val();
  $('#search_text').focus(function(){
    if($(this).val()==search_val){
      $(this).val('');
    }
  }).blur(function(){
    if($(this).val()==''){
      $(this).val(search_val);
    }
  });
  /* end Search_bar */
  
  /* Tiptip Init */
  if ( IS_CSS_MAXWIDTH_SUPPORTED ) {
    //$('#category_browser li.level-1 > a, #category_browser li.level-2 > a ').tipTip({defaultPosition:'right'});
    //$('#category_browser li.level-3 > a').tipTip({edgeOffset:10});
    $('.tips').tipTip({defaultPosition:'top'});
    $('.tooltips').tipTip({defaultPosition:'top',maxWidth:'400px'});
  }
  /* end Tiptip Init */
  
  /* Category Browser */
  $('#category_browser li.level-2 > a').append(' <span class="toggle">[+]</span>');
  $('#category_browser li.level-2 > ul').hide();
  $('#category_browser li.level-2').each(function(i){
    $(this).children().children().css({'display':'inline'}).not(':last-child').append(',&nbsp;').end().children().css({'display':'inline'});
    $(this).hover(function(){
      //$('#category_browser li.level-2 > ul').hide().eq(i).show();
      $('#category_browser li.level-2 > ul').hide().eq(i).show();
      $('#category_browser li.level-2 > a >.toggle').eq(i).text('[-]');
    },function(){
      //$('#category_browser li.level-2 > ul').hide();
      $('#category_browser li.level-2 > ul').hide();
      $('#category_browser li.level-2 > a >.toggle').eq(i).text('[+]');
    });
  });
  /* end Category Browser */
  
  /* Product Tiptip */
  if ( IS_CSS_MAXWIDTH_SUPPORTED ) {
    var product_tip = new Array();
    $('.hproduct').each(function(x){
      product_tip[x] = $('.hide',this).html();
      $(this).tipTip({minWidth:"400px",maxWidth:"500px",defaultPosition:'right',content:product_tip[x]});
    });
  }
  /* end Product Tiptip */
  
  /* Product Gallery */
  $('#product_gallery .product_image').hide().eq(0).show();
  $('#product_gallery .product_thumb li img').removeClass('selected').eq(0).addClass('selected');
  $('#product_gallery .product_thumb li img').each(function(idx){
    $(this).mouseover(function(){
      $('#product_gallery .product_thumb li img').removeClass('selected').eq(idx).addClass('selected');
      $('#product_gallery .product_image').hide().eq(idx).show();
    });
  });
  /* end Product Gallery */
  
  /* Multiple Checkbox */
  var $checkboxes = $('.multiple_checkbox');
  $checkboxes.each(function(x){
    $(this).find('.select_all').click(function(){
      if($(this,'input.select_all').is(':checked')){
        $checkboxes.eq(x).find('input:checkbox').attr('checked','checked');
      } else {
        $checkboxes.eq(x).find('input:checkbox').removeAttr('checked');
      }
    });
    $(this).find('input:checkbox').not('.select_all').click(function(){
      $checkboxes.eq(x).find('input:checkbox.select_all').removeAttr('checked');
    });
  });
  /* end Multiple Checkbox */
  
  /* Ready-stock Conditional */
  $('#reg_type_1').click(function(){
    $('#reg_production_schedule input:checkbox').removeAttr('checked').attr('disabled','disabled');
  });
  $('#reg_type_0').click(function(){
    $('#reg_production_schedule input:checkbox').removeAttr('disabled');
  });
  /* end Ready-stock Conditional */
  
  /* 
    Generate Chained Combo Box
    Required catJSON
  */
  if(typeof catJSON != "undefined"){
    // Simplify Option Tag
    function options(value,name){
      var result = '<option value="'+value+'">'+name+'</option>';
      return result;
    }
    // First Combo Box
    function create_option_1(){
      var combobox;
        for (i=0; i<catJSON.category.length; i++) {
          combobox += options(catJSON.category[i].catID, catJSON.category[i].catName);
        }
      return combobox;
    }
    // Second Combo Box
    function create_option_2(id){
      var combobox;
      if( typeof catJSON.category[id].catChild == "undefined"){
        combobox = options(0,'Tidak Ada Sub Kategori');
      } else {
        for (i=0; i<catJSON.category[id].catChild.length; i++) {
          combobox += options(catJSON.category[id].catChild[i].catID, catJSON.category[id].catChild[i].catName);
        }
      }
      return combobox;
    }
    // Third Combo Box
    function create_option_3(id1,id2){
      var combobox;
      if( typeof catJSON.category[id1].catChild[id2].catChild == "undefined"){
        combobox = options(0,'Tidak Ada Sub Sub Kategori');
      } else {
        for (i=0; i<catJSON.category[id1].catChild[id2].catChild.length; i++) {
          combobox += options(catJSON.category[id1].catChild[id2].catChild[i].catID, catJSON.category[id1].catChild[id2].catChild[i].catName);
        }
      }
      return combobox;
    }
    // Initiate First Combo Box and On Change Event
    $('#product_category').append(create_option_1()).change(function(){
      /* Remove first option */
      $('#product_category option[value=0]').remove();
      var id1 = $('#product_category option:selected').index();
      var id2 = $('#product_sub_category option:selected').index();
      $('#product_sub_category').html(create_option_2(id1));
      $('#product_sub_sub_category').html(create_option_3(id1,id2));
    });
    // On Change Event on Second Combo Box
    $('#product_sub_category').change(function(){
      var id1 = $('#product_category option:selected').index();
      var id2 = $('#product_sub_category option:selected').index();
      $('#product_sub_sub_category').html(create_option_3(id1,id2));
    });
  } 
  /* end Chained Combo Box */
  /* Video modal box */
  $('#video').click(function(){
    $.modal('<iframe class="youtube-player" type="text/html" width="640" height="385" type="application/x-shockwave-flash" src="'+ video_profile +'" frameborder="0"></iframe>');
  });
  /* end Video modal box */
  /* Support modal box */
  $('#live_support').click(function(){
    $.modal('<div style="width: 300px;"><object id="pingboxil8jbt8865g00" type="application/x-shockwave-flash" data="http://wgweb.msg.yahoo.com/badge/Pingbox.swf" width="300" height="280"><param name="movie" value="http://wgweb.msg.yahoo.com/badge/Pingbox.swf" /><param name="allowScriptAccess" value="always" /><param name="flashvars" value="wid=Ov6zzFGoRma_6jH6.dTj2RXYjVLdARY-" /></object><span>Live Chat dengan customer support kami.<br />Waktu operasional: Senin-Jumat pukul 9.00-17.00 WIB</span><br />Telepon Customer Service: 021 565 8363</div>');
  });
  /* IE6 Fixed */
  if ( !IS_CSS_MAXWIDTH_SUPPORTED ) {
    var $window = $(window);
    var $elem = $('#give_feedback');
    $window.scroll(function() {
        $elem.css({'top': $(this).scrollTop() + 200 + "px", 'position':'absolute'});
    });
  }
  /* end Video modal box */
  /* Voucher Trigger */
  $('.voucher .trigger').click(function(){
    $(this).next().toggle();
    return false;
  });
  /* end Voucher Trigger */
  /* Hide and Show Invoice Detail */
  $('#content table').addClass('level-1');
  $('#content table table').removeClass('level-1').addClass('level-2');
  $('#content table.level-2:not(.unhide)').each(function(idx){
    $(this).hide()
           .before('<a class="toggleinvoice" href="#" title="Lihat Detail Invoice">Lihat Detail Invoice &darr;</a><hr />')
           .siblings('a.toggleinvoice').toggle(function(){
              $(this).html('<a class="toggleinvoice" href="#" title="Tutup Detail Invoice">Tutup Detail Invoice &uarr;</a>');
              $(this).siblings('table').show();
            },function(){
              $(this).html('<a class="toggleinvoice" href="#" title="Lihat Detail Invoice">Lihat Detail Invoice &darr;</a>');
              $(this).siblings('table').hide();
            });
  });
  if($('#content table').is('.level-2')){ // if no invoice details
    $('#content table.level-1').before('<a class="togglehiddeninvoice" href="#" title="Buka Semua Detail Invoice">Buka Semua Detail Invoice [+]</a>');
  }
  $('.togglehiddeninvoice').toggle(function(){
    $(this).html('<a class="togglehiddeninvoice" href="#" title="Tutup Semua Detail Invoice">Tutup Semua Detail Invoice [-]</a>');
    $('a.toggleinvoice').html('<a class="toggleinvoice" href="#" title="Tutup Detail Invoice">Tutup Detail Invoice &uarr;</a>')
    $('#content table.level-2').show();
  },function(){
    $(this).html('<a class="togglehiddeninvoice" href="#" title="Buka Semua Detail Invoice">Buka Semua Detail Invoice [+]</a>');
    $('a.toggleinvoice').html('<a class="toggleinvoice" href="#" title="Lihat Detail Invoice">Lihat Detail Invoice &darr;</a>');
    $('#content table.level-2').hide();
  });
  
  /* lp_introduction */
  /* end lp_introduction */
});
