//이미지 상세보기 레이어
function LayerShowImage(img, e){
	layerPositionSet('layerImageShow', e);
	$('layerImageShow').innerHTML = "<a href='javascript:;' onclick='LayerHideImage();'><img src='/uploaded/shop_good/"+img+"' border=0></a>";
}

//레이어 닫기
function LayerHideImage() {
	$('layerImageShow').hide();
}

//연계재고 옵션 가져오기
function getRelOpt(idx, opt_1){
	if(idx != "" || opt_1){
		new Ajax.Request('/module/shop/ajax_get_rel_opt.php',
		{
			method:'get',
			parameters: {idx: idx, opt_1: opt_1},
			asynchronous: this.asynchronous,
			encoding: 'utf-8',
			contentType: 'application/x-www-form-urlencoded',

			onSuccess: function(transport){
				var response = transport.responseText || "응답된 내역이 없습니다."; 
				//alert(transport.responseText);
				setRelOpt(response);
			},
			
			onFailure: function(){ 
				alert('AJAX 데이터 응답중 오류가 발생하였습니다.') 
			}   
		});
	}

}

function setRelOpt(txt){
	//alert(txt);
	var opt = new Array();
	var arr = new Array();
	var stk;
	var prc;
	opt = txt.split("||");
	//초기화	
	for(i=1; i< $("opt_rel_contents_1").length; i++){
		$("opt_rel_contents_1").options[i] = null;
	}
	for(i=0; i<opt.length; i++){
		arr = opt[i].split("**");

		if(arr[1] > "0"){
			prc = " (+"+addComma(arr[1])+")";
		}else{
			prc = "";
		}

		//재고가 없을경우 값을 -1 로 설정
		if(arr[2]=="0"){
			$("opt_rel_contents_1").options[i+1] = new Option(arr[0]+" (품절)","-1");
		}else{
			$("opt_rel_contents_1").options[i+1] = new Option(arr[0]+prc,arr[0]+"|"+arr[1]);
		}
	}
}

//옵션가격 설정
function ___setOptPrice(){
	var price;
	var point;
	var pointUnit;
	var total_price;
	//var opt_price;	
	var cnt;
	price = parseInt($("#basicPrice").val());
	point = parseInt($("#basicPoint").val());
	pointUnit = $("#basicPointUnit").val();
	cnt = parseInt($("#qty").val());

	if(cnt > 0){
		cnt = cnt;
	}else{
		cnt = 0;
	}

	/*
	//옵션가격 초기화
	var option_price1, option_price2, option_price3, option_price4, option_price5, option_price_rel1, option_price_rel2;
	option_price1 = 0;
	option_price2 = 0;
	option_price3 = 0;
	option_price4 = 0;
	option_price5 = 0;
	option_price_rel1 = 0;
	option_price_rel2 = 0;
	opt_price = option_price1 + option_price2 + option_price3 + option_price4 + option_price5 + option_price_rel1 + option_price_rel2;

	try{
		option_price1 = $("#opt_contents_0").val().split("|")[1];
		if(option_price1 > 0){
			opt_price = opt_price + parseInt(option_price1);
		}
	}catch(e){}
	try{
		option_price2 = $("#opt_contents_1").val().split("|")[1];
		if(option_price2 > 0){
			opt_price = opt_price + parseInt(option_price2);
		}
	}catch(e){}
	try{
		option_price3 = $("#opt_contents_2").val().split("|")[1];
		if(option_price3 > 0){
			opt_price = opt_price + parseInt(option_price3);
		}
	}catch(e){}
	try{
		option_price4 = $("#opt_contents_3").val().split("|")[1];
		if(option_price4 > 0){
			opt_price = opt_price + parseInt(option_price4);
		}
	}catch(e){}
	try{
		option_price5 = $("#opt_contents_4").val().split("|")[1];
		if(option_price5 > 0){
			opt_price = opt_price + parseInt(option_price5);
		}
	}catch(e){}
 	try{
		option_price_rel1 = $("#opt_rel_contents_1").val().split("|")[1];
		if(option_price_rel1 > 0){
			opt_price = opt_price + parseInt(option_price_rel1);
		}
	}catch(e){}
	try{
		option_price_rel2 = $("#opt_rel_contents_2").val().split("|")[1];
		if(option_price_rel2 > 0){
			opt_price = opt_price + parseInt(option_price_rel2);
		}
	}catch(e){}

	total_price = (price*cnt)+opt_price;
	
	if(pointUnit=="P"){
		total_point_price = price*cnt;
		total_point = (point*total_point_price)/100;
	}else{
		total_point = point*cnt;
	}
	*/

	total_price = price*cnt;
	
	if(pointUnit=="P"){
		total_point_price = price*cnt;
		total_point = (point*total_point_price)/100;
	}else{
		total_point = point*cnt;
	}

	try{
	$("#divPrice").html(addComma(total_price));
	}catch(e){}
	try{
	$("#divPoint").html(addComma(total_point));
	}catch(e){}
}



//셀렉트 박스 전체체크
function checkboxCheckAll(status){
	var obj = document.getElementsByName('items[]'); 
	var objlength = obj.length;
	for(i=0; i<objlength; i++){
		if(status==1){
			obj[i].checked=true;
		}else{
			obj[i].checked=false;
		}
	}
}

//장바구니에서 1개 상품 주문
function orderCartOne(c_idx){
	new Ajax.Request('/module/shop/ajax_cart_process.php',
	{
		method:'post',
		parameters: {evnMode: "orderOne", c_idx: c_idx},
		asynchronous: this.asynchronous,
		encoding: 'utf-8',
		contentType: 'application/x-www-form-urlencoded',

		onSuccess: function(transport){
			var response = transport.responseText || "응답된 내역이 없습니다."; 
			if(response=="true"){
				document.location.href="/shop.php?goPage=Order";
			}else{
				alert("잠시후 다시 시도해 주세요.");
			}
		},
		
		onFailure: function(){ 
			alert('AJAX 데이터 응답중 오류가 발생하였습니다.') 
		}   
	});

}

//위시리스트 체크한 아이템 삭제
function deleteWishChecked(f){
	//1개이상 체크했는지 검사
	var obj = document.getElementsByName('items[]'); 
	var objlength = obj.length;
	var objchecked = 0;
	for(i=0; i<objlength; i++){
		if(obj[i].checked==true){
			objchecked++;
		}
	}

	if(objchecked < 1){
		alert("선택하신 상품이 없습니다.");
		return;
	}

	var cfm = false;
	cfm = confirm("선택하신 상품들을 위시리스트에서 삭제 하시겠습니까?");
	if(cfm==true){
		f.evnMode.value = "deleteWishChecked";
		f.action = "/module/shop/wish_evn.php";
		f.submit();
	}
}

//위시리스트 체크한 아이템 카트에 담기
function addCartWishChecked(f){
	//1개이상 체크했는지 검사
	var obj = document.getElementsByName('items[]'); 
	var objlength = obj.length;
	var objchecked = 0;
	for(i=0; i<objlength; i++){
		if(obj[i].checked==true){
			objchecked++;
		}
	}

	if(objchecked < 1){
		alert("선택하신 상품이 없습니다.");
		return;
	}

	f.evnMode.value = "addCartWishChecked";
	f.action = "/module/shop/wish_evn.php";
	f.submit();
}

//하위 카테고리 html 로 가져오기 - 좌측카테고리
function getSubCatHtml(cat_no){
	if(cat_no){
		new Ajax.Request('/module/category/ajax_get_cat_html.php',
		{
			method:'get',
			parameters: {cat_no: cat_no},
			asynchronous: this.asynchronous,
			encoding: 'utf-8',
			contentType: 'application/x-www-form-urlencoded',

			onSuccess: function(transport){
				var response = transport.responseText || "하위카테고리가 없습니다."; 
				//alert(transport.responseText);
				showSubCatHtml(response);
			},
			
			onFailure: function(){ 
				alert('AJAX 데이터 응답중 오류가 발생하였습니다.') 
			}   
		});
	}else{
		hideSubCatHtml();
	}
}

function showSubCatHtml(str){
	$("divSubCategory").innerHTML = str;
	$("divSubCategory").show();
}

function hideSubCatHtml(){
	$("divSubCategory").innerHTML = "";
	$("divSubCategory").hide();
}

function getCordinatesByEvent(event){
	var cordinates = {
		x:event.pointerX(),
		y:event.pointerY()
	};
	$("divSubCategory").hide();
	$("divSubCategory").style.left = cordinates.x+10;
	$("divSubCategory").style.top = cordinates.y-5;
}
/* 카테고리 관련 */

//적립금 사용
function calUsingPoint(f, val){
	//사용하려는 적립금이 가진 적립금보다 크면 내 적립금으로 강제조정
	if(parseInt(f.using_point.value) > parseInt(f.hiddenMyPoint.value)){
		f.using_point.value = f.hiddenMyPoint.value;
	}
	//0보다 작으면 0
	if(parseInt(f.using_point.value) < 0){
		f.using_point.value = 0;
	}
	//값이 없어도 0
	if(!f.using_point.value){
		f.using_point.value = 0;
	}
	var payPrice = parseInt(f.hiddenPayAmount.value) - parseInt(f.using_point.value);
	$("showPriceTotal").innerHTML =  addComma(payPrice) + "원";
}

//상품이미지 크게보기
function viewImgPop(g_idx,seq){
	obj = window.open("/module/shop/viewImgPop.php?g_idx="+g_idx+"&seq="+seq,"viewImagePop","width=100,height=100,menubars=0, toolbars=0");
}


//상품 상세페이지에서의 리뷰보기
var review_list = '';
function reveiwClick( review) {
    if( review_list != review ) {
	    if( review_list !='' ) {
		    review_list.style.display = 'none';
		}
	    review.style.display = 'block';
		review_list = review;
    } else {
        review.style.display = 'none';
        review_list = '';
    }
}

//쇼셜커머스
function sendTwitter(title,url) {
	var wp = window.open("http://twitter.com/home?status=" + encodeURIComponent(title) + " " + encodeURIComponent(url), 'twitter', '');
	if ( wp ) {
		wp.focus();
	}
}
function sendMe2Day(title,url,tag) {
	var wp = window.open("http://me2day.net/posts/new?new_post[body]=" + encodeURIComponent(title) + " " + encodeURIComponent(url) + "&new_post[tags]=" + encodeURIComponent(tag), 'me2Day', '');
	if ( wp ) {
		wp.focus();
	}
}
function sendFaceBook(title,url) {
	var wp = window.open("http://www.facebook.com/sharer.php?u=" + encodeURIComponent(url) + "&t=" + encodeURIComponent(title), 'facebook', '');
	if ( wp ) {
		wp.focus();
	}
}
function sendCyWorld(url,title,thumbnail,summary) {
	var wp = window.open("http://csp.cyworld.com/bi/bi_recommend_pop.php?url="+encodeURIComponent(url)+"&title="+encodeURIComponent(title)+"&thumbnail="+encodeURIComponent(thumbnail)+"&summary="+encodeURIComponent(summary),"xu","width=400px,height=364px")
	if ( wp ) {
		wp.focus();
	}
}

