
function changeGoodsControllerDetailImg(no) {

   $('.goodsControllerDetailImg')[0].src = $('.goodsControllerThumnailImg')[no].src;

}

function changeGoodsControllerTotalPrice(price, no) {

  var quantity = parseInt($('#goodsControllerQuantity').val());
  if(typeof(no)!='undefined') quantity += no;

  if(!quantity||(quantity<1)) return false; 

  $('p > .goodsControllerOptions, .goodsControllerOptionsRadio:checked').each(function() { 
	 var optionPrice = this.value.split(';')[2];
	 if((typeof(optionPrice)!='undefined')&&optionPrice) price += parseFloat(optionPrice);
  });

  $('#goodsControllerQuantity').val(quantity);
  $('#goodsControllerTotalPrice').html(number_format(price * quantity, ($('#goodsControllerUseDecimal').val() ? 2 : 0)));

}

function clickGoodsController(obj, mode) {

   var goodsNo = obj.parentNode ? obj.parentNode.rel : obj;
   if(typeof(l)=='undefined') langHref = ''; else langHref = '/' + l;

   if(!goodsNo||!mode) return false;
  
   if(mode=='wish') {

    $('#actionFrame')[0].src = langHref + '/mall/wish-option/mode/add/goodsNo/' + goodsNo;
	return true;

   }

   var title = '';
   var url = langHref + '/mall/goods-controller/mode/' + mode + '/goodsNo/' + goodsNo;

   switch(mode) {
     case 'zoom' : url += '?width=500&top=80'; title = _i('간편보기'); break;
     case 'buy' : url += '?width=380&top=80'; title = _i('즉시구매'); break;
     case 'cart' : url += '?width=380&top=80'; title = _i('장바구니'); break;
   }

   var callback = function() {
	  $('#goodsControllerQuantity').unbind();
      if(mode!='zoom') $('#goodsControllerQuantity').keyup(function() { if(this.value=='0') this.value = 1; changeGoodsControllerTotalPrice(parseFloat($('#goodsControllerPrice').val())); }); 
   };

   modalShow(title, url, 'GET', callback);
   
}

function initGoodsThumnailControll() {

   if(!$('#goodsController').html()) {

	var html = '<div id="goodsController" style="padding-top: 5px; height: 30px; text-align: right; background-color: #000;" class="none">'
	+ ' <a href="javascript:;" class="zoom" onfocus="this.blur();"><img src="/app/views/mall/common/imgs/goods_controller_bt_zoom.gif" alt="' + _i('간편보기') + '" class="imgs" /></a>&nbsp;' 
	+ ' <a href="javascript:;" class="cart" onfocus="this.blur();"><img src="/app/views/mall/common/imgs/goods_controller_bt_cart.gif" alt="' + _i('장바구니') + '" class="imgs" /></a>&nbsp;' 
	+ ' <a href="javascript:;" class="buy" onfocus="this.blur();"><img src="/app/views/mall/common/imgs/goods_controller_bt_buy.gif" alt="' + _i('즉시구매') + '" class="imgs" /></a>&nbsp;'
	+ ' <a href="javascript:;" class="wish" onfocus="this.blur();"><img src="/app/views/mall/common/imgs/goods_controller_bt_wish.gif" alt="' + _i('관심상품') + '" class="imgs" /></a>&nbsp;'
	+ '</div>'
	+ '<div id="goodsControllerLayer"></div>';

    $('body').append(html);

   }

   if(!$('#goodsControllerSmallLayer').html()) {

	var html = '<div id="goodsControllerSmall" style="padding-top: 5px; height: 30px; text-align: right; background-color: #000;" class="none">'
	+ ' <a href="javascript:;" class="zoom" onfocus="this.blur();"><img src="/app/views/mall/common/imgs/goods_controller_bt_zoom.gif" alt="' + _i('간편보기') + '" class="imgs" /></a>&nbsp;' 
	+ ' <a href="javascript:;" class="cart" onfocus="this.blur();"><img src="/app/views/mall/common/imgs/goods_controller_bt_cart.gif" alt="' + _i('장바구니') + '" class="imgs" /></a>&nbsp;' 
	+ ' <a href="javascript:;" class="wish" onfocus="this.blur();"><img src="/app/views/mall/common/imgs/goods_controller_bt_wish.gif" alt="' + _i('관심상품') + '" class="imgs" /></a>&nbsp;'
	+ '</div>'
	+ '<div id="goodsControllerSmallLayer"></div>';

    $('body').append(html);

   }

   $('.goodsController').each(function() {

      var goodsController = (this.className.indexOf('small')!=-1) ? 'goodsControllerSmall' : 'goodsController';
      var goodsControllerLayer = (this.className.indexOf('small')!=-1) ? '#goodsControllerSmallLayer' : '#goodsControllerLayer';

	  $(this).mouseover(function() {

        var This = this;
		var left = getOffsetLeft(This);
		var top = getOffsetTop(This) + ($(this).height()-(parseInt($('#' + goodsController).css('paddingTop'))+parseInt($('#' + goodsController).css('paddingBottom'))+$('#' + goodsController).height())) + parseInt($(this).css('border-top-width'));
        
		$('#' + goodsController).removeClass('none').css({position: 'absolute', zIndex: 1100, top: top, left: left, width: ($(this).width()+parseInt($(this).css('border-right-width'))), opacity: 0.7})
		.mouseout(function(event) {

           if(($(event.toElement).attr('id')!=goodsController)
			  &&($(event.toElement).parent().attr('id')!=goodsController)
			  &&($(event.toElement).parent().parent().attr('id')!=goodsController)) {
	        $('#' + goodsController).addClass('none'); 
		   }

		}).mouseover(function() { 
	        $('#' + goodsController).removeClass('none'); 
		});

   	    if(typeof($('#' + goodsController + '>.zoom')[0])!='undefined') $('#' + goodsController + '>.zoom')[0].onclick = function() { $(goodsControllerLayer).hide(); clickGoodsController(This, 'zoom'); }
   	    if(typeof($('#' + goodsController + '>.cart')[0])!='undefined') $('#' + goodsController + '>.cart')[0].onclick = function() { $(goodsControllerLayer).hide(); clickGoodsController(This, 'cart'); }
   	    if(typeof($('#' + goodsController + '>.buy')[0])!='undefined') $('#' + goodsController + '>.buy')[0].onclick = function() { $(goodsControllerLayer).hide(); clickGoodsController(This, 'buy'); }
   	    if(typeof($('#' + goodsController + '>.wish')[0])!='undefined') $('#' + goodsController + '>.wish')[0].onclick = function() { $(goodsControllerLayer).hide(); clickGoodsController(This, 'wish'); }

	  });
	  
	  $(this).mouseout(function(event) { 

          if(($(event.toElement).attr('id')!=goodsController)
			  &&($(event.toElement).parent().attr('id')!=goodsController)
			  &&($(event.toElement).parent().parent().attr('id')!=goodsController)) {
			$('#' + goodsController).addClass('none'); 
		  }

	  });

   });

}


$(document).ready(function() { initGoodsThumnailControll(); });
