/*
 * for inline thickbox implementation
 */
function change_tb_href()
{
  var qString = $("a.thickbox").attr('rel');
  $("a.thickbox").attr('href', '#TB_inline?' + qString);
}

function ddPNG()
{
  DD_belatedPNG.fix('#page');
  DD_belatedPNG.fix('.call-box');
  DD_belatedPNG.fix('.sec-nav');
  DD_belatedPNG.fix('.zoom, .zool li');
  DD_belatedPNG.fix('.ao a');
  DD_belatedPNG.fix('.ba a');

}

function toggle_tab()
{
  $('[class~=hidden]').hide();
  $('ul.tabs a').click(function(){
    $('ul.tabs li').removeClass('active');
    $(this).parent('li').addClass('active');
    var tabId = $(this).attr('href').replace('#','');
    $('.tabs-content div').hide();
    $('.tabs-content div#' + tabId).show();

    return false;
  });

  $('a.tabs').click(function(){
    $('ul.tabs li').removeClass('active');
    $('ul.tabs li a[href='+$(this).attr('href')+']').parent('li').addClass('active');
    var tabId = $(this).attr('href').replace('#','');
    $('.tabs-content div').hide();
    $('.tabs-content div#' + tabId).show();

    return false;
  });
}


function imageZoom() {
  
  $('ul.zoom li img').each(function() {

    var pc = ($('.zoom').attr('rel') == undefined) ? 1 : $('.zoom').attr('rel') / 100;
    w = parseInt($(this).width());
    h = parseInt($(this).height());
    w1 = w * pc;
    h1 = h *pc;
    $(this).removeAttr('width').width(w1);
    $(this).removeAttr('height').height(h1);
//console.log('W: ' + w + '('+ w1 +') H: ' + h + '('+ h1 +') %: ' +pc);
    $(this).mouseover(function(){
      $(this).animate({width:w,height:h});
    });
    $(this).mouseout(function(){
      $(this).animate({width:w1,height:h1});
    });
  });
}

function hoverAddLink()
{
  $('[class~=hm-prod]').hover(function(){
    $(this).addClass('active');
    var link = $(this).children('p').children('a').attr('href');
    $(this).click(function(){
      if (typeof(link) == 'undefined') {
        return;
      }
      document.location = link;
    });
  }, function(){
    $(this).removeClass('active');
  });

}

function loopScroll()
{
  $("#scroller").simplyScroll({
    autoMode: 'loop'
  });
}

function formMagic()
{
  // remove "required" class on labels
  $('form label.required').removeClass('required');
  // addptional class to input and textarea with value of "optional"
  $("form label.optional ~ input[type=text][value='optional']").addClass('optional');
  $("form label.optional ~ textarea[value='optional']").addClass('optional');
  // look for optional labels that have an empty input field or textarea as sibling
  // and add optional as value to field. remove value of optional on focus
  $("form label.optional ~ input[type=text][value='']").val('optional').addClass('optional').click(function(){
    if (this.value == 'optional') {
      $(this).val('').removeClass('optional');
    }
  }).blur(function(){
    if ($.trim(this.value) == '') {
      $(this).val('optional').addClass('optional');
    }
  });
  // ... and for textarea
  $("form label.optional ~ textarea[value='']").val('optional').addClass('optional').click(function(){
    if (this.value == 'optional') {
      $(this).val('').removeClass('optional');
    }
  }).blur(function(){
    if ($.trim(this.value) == '') {
      $(this).val('optional').addClass('optional');
    }
  });

  // remove field value of optional on submit
  $("form").submit(function(){
    $("form label.optional ~ input[type=text][value='optional']").val('').removeClass('optional');
    $("form label.optional ~ textarea[value='optional']").val('').removeClass('optional');
    return true;
  });

}



/* onDocumentReady Call */
$(function(){
  
  if ($('a.thickbox').hasClass('inline')) {
      change_tb_href();
  }

  if ( typeof(DD_belatedPNG) != 'undefined' ) {
    ddPNG();
  }
  formMagic();
  toggle_tab();
  imageZoom();
  hoverAddLink();
  loopScroll();
//  calculatorBox();
  
  $('a.fancyiframe').fancybox({
    'frameWidth':500,
    'frameHeight':520,
    'hideOnContentClick': false
  });

  $('a.calc').fancybox({
    'frameWidth':640,
    'frameHeight':510,
    'hideOnContentClick': false
  });

  if (typeof($.datepicker) != 'undefined') {
    $('.i-date').datepicker({dateFormat :'dd M yy'});
  }

});