$(document).ready(function() {
  				
	$('.module_template .product .types').each(function() {
			
		$('.tmpul1').each(function() {
			
			$('.tmpli1_0').each(function() {
				var optList = $(this).attr('opt').split("|");
				
				for (var j=0; j < optList.length; j++) {
					if (optList[j] == '1' && j%3 == 0) {
						$(this).text(optList[j+1].toUpperCase()+":");	
						break;
					}
				}
			});	
			
			$('.tmpli1_1').each(function() {
								
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '1') {
						
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).addClass('active');
						}
						else if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('active-type');
						}
						else {
							$(this).addClass('active-size');
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
												
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);												
					}	
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});	
						
			$('.tmpli1_2').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '1') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);												
					}
					else {
						$(this).css('display', 'none');
					}
				}
				else {
					$(this).css('display', 'none');
				}
			});	
			
			$('.tmpli1_3').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '1') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);						
					}
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});	
			
			$('.tmpli1_4').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '1') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);						
					}
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});	
			
			$('.tmpli1_5').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '1') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);					
					}
					else {
						$(this).css('display', 'none');
					}
				}
				else {
					$(this).css('display', 'none');
				}
			});		
			
			
		});	
		
		$('.tmpul2').each(function() {
			
			$('.tmpli2_0').each(function() {
				var optList = $(this).attr('opt').split("|");
				
				for (var j=0; j < optList.length; j++) {
					if (optList[j] == '2' && j%3 == 0) {
						$(this).text(optList[j+1].toUpperCase()+":");	
						break;
					}
				}
			});	
			
			$('.tmpli2_1').each(function() {
								
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '2') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						if (optList[2] == 0) {
							if (optList[1].toUpperCase() == 'COLOR') {
								$(this).addClass('active');
							}
							else if (optList[1].toUpperCase() == 'TYPE') {	
								$(this).addClass('active-type');
							}
							else {
								$(this).addClass('active-size');
							}
						}						
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);
						
					}	
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});	
					
			$('.tmpli2_2').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '2') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						if (optList[2] == 1) {
							if (optList[1].toUpperCase() == 'COLOR') {
								$(this).addClass('active');
							}
							else if (optList[1].toUpperCase() == 'TYPE') {	
								$(this).addClass('active-type');
							}
							else {
								$(this).addClass('active-size');
							}
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);
						
					}
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});	
			
			$('.tmpli2_3').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '2') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						if (optList[2] == 2) {
							if (optList[1].toUpperCase() == 'COLOR') {
								$(this).addClass('active');
							}
							else if (optList[1].toUpperCase() == 'TYPE') {	
								$(this).addClass('active-type');
							}
							else {
								$(this).addClass('active-size');
							}
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);
						
					}
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});	
			
			$('.tmpli2_4').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '2') {
						//var prdList = $(this).attr('prd').split(",");
						
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						if (optList[2] == 3) {
							if (optList[1].toUpperCase() == 'COLOR') {
								$(this).addClass('active');
							}
							else if (optList[1].toUpperCase() == 'TYPE') {	
								$(this).addClass('active-type');
							}
							else {
								$(this).addClass('active-size');
							}
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);
						
					}
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});	
			
			$('.tmpli2_5').each(function() {
				if ($(this).attr('opt') != '') {
					
					var optList = $(this).attr('opt').split("|");
					
					if (optList[0] == '2') {
						//var prdList = $(this).attr('prd').split(",");
							
						if (optList[1].toUpperCase() == 'TYPE') {	
							$(this).addClass('normal-type');
						}
						
						if (optList[2] > 3) {
							if (optList[1].toUpperCase() == 'COLOR') {
								$(this).addClass('active');
							}
							else if (optList[1].toUpperCase() == 'TYPE') {	
								$(this).addClass('active-type');
							}
							else {
								$(this).addClass('active-size');
							}
						}
						
						$(this).find('a').text($(this).attr('itm').replace('^','\"'));
						
						if (optList[1].toUpperCase() == 'COLOR') {
							$(this).find('a').attr('class',$(this).attr('itm').toLowerCase());
						}
						else {
							$(this).find('a').attr('class','size');
						}
						
						//$(this).find('a').attr('prd',$(this).attr('prd'));
						$(this).find('a').attr('opt',optList[1]);
						
					}
					else {
						$(this).css('display', 'none');
					}
				}	
				else {
					$(this).css('display', 'none');
				}
			});		
			
		});	
				
		var $links = $(this).find('a');
		
		var $mid = $(this).attr('mid');	
					
		$links.each(function() {
			  						
			$(this).click(function(e) {
				
				var lev = $(this).attr('lev');							
				var active_prd = "";
				
				$links.each(function() {  	
					
					if ($(this).attr('opt') != null) {
						if ($(this).attr('opt').toUpperCase() == 'COLOR') {
							
							if($(this).parent().hasClass('active') == true) {								
								if (lev != $(this).attr('lev')) {
									active_prd = $(this).parent().find('a').attr('prd');
								}								
							} 
							
							if (lev == $(this).attr('lev')) {
								$(this).parent().removeClass('active');		
							}																		
						}
						else if ($(this).attr('opt').toUpperCase() == 'TYPE') {	
							
							if($(this).parent().hasClass('active-type') == true) {								
								if (lev != $(this).attr('lev')) {
									active_prd = $(this).parent().find('a').attr('prd');
								}
							} 
							
							if (lev == $(this).attr('lev')) {
								$(this).parent().removeClass('active-type'); 	
							}								
						}
						else {
							
							if($(this).parent().hasClass('active-size') == true) {								
								if (lev != $(this).attr('lev')) {
									active_prd = $(this).parent().find('a').attr('prd');
								}
							} 
							
							if (lev == $(this).attr('lev')) {
								$(this).parent().removeClass('active-size');
							}							
						}	
					}									
				});				
				
				var seq_ = $(this).attr('seq');
							
				var prdList1 = $(this).attr('prd').split(",");
				var prdList2 = active_prd.split(",");
								
				var prdVar = prdList1[0];
				for(var t=0; t < prdList1.length; t++) {
					for(var z=0; z < prdList2.length; z++) {
						if (prdList1[t] == prdList2[z]) {
							prdVar = prdList1[t];							
							break;
						}
					}
				}
																
				$.ajax({					 
				     type:"GET",
				     contentType: "application/xml; charset=utf-8",
				     url:"/us/product/ProductOption.do",
				     dataType:"xml",
				     data:{prd_mdl_cd:prdVar},
				     
				     success: function(xml) {
				    					    	
				    	if ($(xml).find("result").find("prdinfo").length > 0) {				    	 				    	
					        
				    		$(xml).find("result").find("prdinfo").each(function(idx) {
					    		
						    	$('#prdImg_'+$mid+'_'+seq_).attr('src',$(this).find("file_path").text());						    	
						    	$('#prdLinkName_'+$mid+'_'+seq_).attr('href',$(this).find("producturl").text());								
								$('#prdLinkName_'+$mid+'_'+seq_).html($(this).find("display_prd_mdl_name").text());								
								$('#prdcd_'+$mid+'_'+seq_).text($(this).find("prd_mdl_name").text());
								$('#prdStar_'+$mid+'_'+seq_).attr('class','star'+parseInt($(this).find("prd_star_grd").text()));								
								$('#prdReviews_'+$mid+'_'+seq_).attr('href',$(this).find("reviews_link").text());								
								$('#prdReviews_'+$mid+'_'+seq_).text("("+parseInt($(this).find("prd_rvw_count").text())+")");								
								$('#prdPrice_'+$mid+'_'+seq_).html($(this).find("prd_price_inf").text()+"&nbsp;");
								$('#prdPromoPrice_'+$mid+'_'+seq_).html($(this).find("prd_promo_price_inf").text()+"&nbsp;");
								$('#prdPriceDesc_'+$mid+'_'+seq_).html($(this).find("prd_price_desc").text()+"&nbsp;");
								$('#prdPriceDesc_'+$mid+'_'+seq_).css('display', 'none');
								$('#prdPromoPrice_'+$mid+'_'+seq_).css('display', 'none');
								if($(this).find("prd_promo_price_inf").text().length <= 0){
									$('#prdPrice_'+$mid+'_'+seq_).removeClass('price-strike-through');
									$('#prdPrice_'+$mid+'_'+seq_).addClass('price');
								}else{
									$('#prdPrice_'+$mid+'_'+seq_).removeClass('price');
									$('#prdPrice_'+$mid+'_'+seq_).addClass('price-strike-through');
									$('#prdPromoPrice_'+$mid+'_'+seq_).css('display', 'block');
									if($(this).find("prd_price_desc").text().length > 0){
										$('#prdPriceDesc_'+$mid+'_'+seq_).css('display', 'block');
									}
								}
								$('#prdShop_'+$mid+'_'+seq_).attr('href',$(this).find("shop_link").text());
								$('#prdLearnMore_'+$mid+'_'+seq_).attr('href',$(this).find("producturl").text());								
								$('#prdId_'+$mid+'_'+seq_).attr('rel',$(this).find("prd_mdl_cd").text());	
								
								
						   	});
					    }  				    						    
				     }
				     //, error: function(xhr, status, error) {alert(error); }
				    
				});			
				
				if ($(this).attr('opt') != null) {
					if ($(this).attr('opt').toUpperCase() == 'COLOR') {
						$(this).parent().addClass('active');  	
					}
					else if ($(this).attr('opt').toUpperCase() == 'TYPE') {	
						$(this).parent().addClass('active-type');  	
					}
					else {
						$(this).parent().addClass('active-size');  	
					}	
				}
											
			});
			
		});
	});

});

