var goodsControllerLayer = '#goodsControllerLayer';

function changeGoodsControllerDetailImg(no) {

   $('.goodsControllerDetailImg')[0].src = $('.goodsControllerThumnailImg')[no].src;

}

// docham.kr start
function changeGoodsControllerTotalPrice0(price, no) {

  var quantity0 = parseInt($('#addGoodsControllerQuantity0').val());
  var quantity1 = parseInt($('#addGoodsControllerQuantity1').val());
  var _quantity = quantity0;
  var _no = 0;

  if(typeof(no)!='undefined') { _quantity += no; _no = no; }
  if(_quantity<0) { $('#addGoodsControllerQuantity0').val(0); return false; }

  var totalQuantity = (quantity0*goodsControllerMax0) + (quantity1*goodsControllerMax1) + (_no*goodsControllerMax0);

  $('#goodsControllerQuantity').val(totalQuantity);
  $('#addGoodsControllerQuantity0').val(_quantity);
  $('#goodsControllerTotalPrice').html(number_format(price * totalQuantity, ($('#goodsControllerUseDecimal').val() ? 2 : 0)));

}

function changeGoodsControllerTotalPrice1(price, no) {

  var quantity0 = parseInt($('#addGoodsControllerQuantity0').val());
  var quantity1 = parseInt($('#addGoodsControllerQuantity1').val());
  var _quantity = quantity1;
  var _no = 0;

  if(typeof(no)!='undefined') { _quantity += no; _no = no; }
  if(_quantity<0) { $('addGoodsControllerQuantity1').val(0); return false; }

  var totalQuantity = (quantity0*goodsControllerMax0) + (quantity1*goodsControllerMax1) + (_no*goodsControllerMax1);

  $('#goodsControllerQuantity').val(totalQuantity);
  $('#addGoodsControllerQuantity1').val(_quantity);
  $('#goodsControllerTotalPrice').html(number_format(price * totalQuantity, ($('#goodsControllerUseDecimal').val() ? 2 : 0)));

}
// docham.kr end

function changeGoodsControllerTotalPrice(price, no) {

  var quantity = parseInt($('#goodsControllerQuantity').val());
  if(typeof(no)!='undefined') quantity += no;

  if(!quantity||(quantity<1)) return false; 

  $('p > .goodsControllerOptions, :input:radio[class=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(!goodsNo||!mode) return false;
  
   if(mode=='wish') {

    $('#actionFrame')[0].src = '/mall/wish-option/mode/add/goodsNo/' + goodsNo;
	return true;

   }

   var title = '';
   var url = '/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();
	  // docham.kr start
	  if(mode!='zoom') {
	   $('#goodsControllerQuantity').keyup(function() { if(this.value=='0') this.value = 1; changeGoodsControllerTotalPrice(parseFloat($('#_goodsControllerPrice').val())); }); 
       $('#addGoodsControllerQuantity0').keyup(function() { if(this.value=='0') this.value = 1; changeGoodsControllerTotalPrice0(parseFloat($('#_goodsControllerPrice').val())); }); 
       $('#addGoodsControllerQuantity1').keyup(function() { changeGoodsControllerTotalPrice1(parseFloat($('#_goodsControllerPrice').val())); }); 
	  }
	  // docham.kr end
   };

   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);

   }

   $('.goodsController').each(function() {

	  $(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'); 
		});
   	    $('#goodsController>.zoom')[0].onclick = function() { $(goodsControllerLayer).hide(); clickGoodsController(This, 'zoom'); }
   	    $('#goodsController>.cart')[0].onclick = function() { $(goodsControllerLayer).hide(); clickGoodsController(This, 'cart'); }
   	    $('#goodsController>.buy')[0].onclick = function() { $(goodsControllerLayer).hide(); clickGoodsController(This, 'buy'); }
   	    $('#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(); });