var position = 0;
var url_path = "";
$(document).ready(function() {
	// set up the link
	$(".product-col .types").find('a').each(function(i) {
		var obj = $(this);
		obj.click(function() {
			if($(this).parents('ul').find('.list-name').html() == 'Size:' && $(this).html().indexOf('"') == -1) {
				$(this).parents('.types').find('.active-size').removeClass('active-size');
				$(this).parent('li').addClass('active-size a');
				active_size = $(this).parent('li').find('.active-size a');
			} else if($(this).parents('ul').find('.list-name').html() == 'Memory:' || $(this).parents('ul').find('.list-name').html() == 'Storage:')	{
				$(this).parents('.types').find('.active-size').removeClass('active-size');
				$(this).parent('li').addClass('active-size');
				active_size = $(this).parent('li').find('.active-size a');
			} else if($(this).parents('ul').find('.list-name').html() == 'Type:') {
				$(this).parents('.types').find('.active-type').removeClass('active-type');
				$(this).parent('li').addClass('active-type');		
				active_type = $(this).parent('li').find('.active-type');
			} else {
				$(this).parents('.types').find('.active').removeClass('active');
				$(this).parent('li').addClass('active');
				active = $(this).parent('li').find('.active a');
			}
			position = $(this).attr("pos");
			url_path = $(this).attr("path");
			
			var selected_item = $(this).attr('ref');
			var ref = '';
			$(this).parents('.types').find('ul').each(function(i) {
				if($(this).find('.active-type a').length > 0 && $(this).find('.active-type a').attr('ref') != selected_item) ref = $(this).find('.active-type a').attr('ref');
				if($(this).find('.active-size a').length > 0 && $(this).find('.active-size a').attr('ref') != selected_item) ref = $(this).find('.active-size a').attr('ref');
				if($(this).find('.active a').length > 0 && $(this).find('.active a').attr('ref') != selected_item) ref = $(this).find('.active a').attr('ref');
			});

			var prd_mdl_cd = $(this).attr("ref");
			if(ref != '') {
				selected_item = selected_item.split(',');
				for(var i = 0; i < selected_item.length; i++) {
					if(ref.indexOf(selected_item[i]) != -1) {
						prd_mdl_cd = selected_item[i];
						break;
					}
				}
			}

			$.ajax({
				url: "/us/product/productInfo.do",
				data: {
					prd_ia_url_name_path: url_path,
					b2b_b2c_typ_cd: $(this).attr("b2b"),
					prd_mdl_cd: prd_mdl_cd,
					landing_yn: 'N'
				},
				dataType: "json",
				success: loadFamilyProduct
			});
		});
	});
});

function loadFamilyProduct(data) {
	$(".prd-imgurl" + position).attr("href", data[0].producturl.replace("it-business-products","business"));
	$(".prd-img" + position).attr("src", data[0].file_path + "/" + data[0].file_name);
	$(".prd-name" + position).html(data[0].display_prd_mdl_name);
	$(".prd-name" + position).attr("href", data[0].producturl.replace("it-business-products","business"));
	$(".prd-cd" + position).html(data[0].prd_mdl_name);
	$(".prd-cmp" + position).attr("value", data[0].prd_mdl_cd);
	$(".qv-prd-cd" + position).attr("rel", data[0].prd_mdl_cd);
	$(".prd-rvw" + position).html("(" + data[0].prd_rvw_count + ")");
	$(".prd-rvw" + position).attr("href", data[0].producturl.replace("it-business-products","business") + '-reviews');
	$(".prd-mkt-desc" + position).html(data[0].market_copy_text);
	$(".prd-mkt-title" + position).html(data[0].market_desc_title);
	$(".prd-price" + position).html(data[0].prd_price_inf);	
	$(".prd-promo-price" + position).html('');
	$(".prd-price-desc" + position).html('');	
	if((data[0].prd_promo_price_inf).length <= 0){
		$(".prd-price" + position).removeClass('price-strike-through');
		$(".prd-price" + position).addClass('price');
	}else{
		$(".prd-price" + position).removeClass('price');
		$(".prd-price" + position).addClass('price-strike-through');
		$(".prd-promo-price" + position).html(data[0].prd_promo_price_inf);
		$('#promoPrice'+position).css('display', 'block');
		if((data[0].prd_price_desc).length > 0){
			$(".prd-price-desc" + position).html(data[0].prd_price_desc);	
			$("#priceDesc"+position).css('display','block');
		}
	}
	$(".prd-shop" + position).attr("href", data[0].producturl.replace("it-business-products","business") + '-buy');
	$(".prd-url" + position).attr("href", data[0].producturl.replace("it-business-products","business"));

/*	$('.prd-star' + position).removeClass(function(i, c) {
		var sp = c.split(' ');
		for(p = 0; p < sp.length; p ++) {
			if(sp[p].substring(0,4) == 'star') 
		}
	});
	*/
	var cn = $('.prd-star' + position).attr("className").split(" ");
	for(var p = 0; p < cn.length; p++) {
		if(cn[p].substring(0,4) == 'star') $('.prd-star' + position).removeClass(cn[p]);
	}
	$('.prd-star' + position).addClass("star" + data[0].prd_star_grd);
	
	// Run Quickview after reset
	initProductQuickViews();
}

