/*
 * Homepage scripts for product grid and banner 
 * @author luisat
 */

var defaultLimit = 6; 
var defaultDropDownKey = "featured";

$(document).ready( function() {

	var message1 = "<div align='center'><img src='" + serverURL + "/res/i/featuredprod_loading.gif' alt='Loading'></div>";
	var message2 = "No products found";
	
	function getTitle(key) {

		if(key == "recent") {
			return "Recently Viewed";
		}
		else if (key == "latest") {
			return "Latest Additions";
		}
		else {
			return "Featured Products";
		}
	}

	function populate(limit, key) {
	
		DWRHomepageController.getProductsBy(key, limit, { 
			callback: updateContentHandler,
			errorHandler:function(message) { 			
				$("#dynamicMessage").empty().append(message2);
			 },
			timeout:20000
		});
	}

	// capture the current value selected in the drop down box	
	function captureDropDownValue() {		
		var value =  $("#optionsDropDown").attr("value");
		if ( !value || value.length == 0 ) {		
			value = defaultDropDownKey;
		}
		return value;
	}
			
	// populate content
	function updateContentHandler(productList) {
		
		$("#productsContentTable").empty();
		$("#dynamicMessage").empty();
		
		var cellPosition = 0;
		 
		if ( productList ) {  				
			if (productList.length == 0) { 									
									
				$("#dynamicMessage").append(message2);
			}
			else { 								
				
				// populate product table
				ws.cr.util.addFixedRows("productsContentTable", productList, 3, 3, 
				
					function(productBean) {   
						
						var currentCellPosition = cellPosition++;
						
						if(currentCellPosition == 0 || currentCellPosition == 3) {
							var result = "<div class=\"cr_featuredproduct first\" style=\"padding: 0px 0px 10px 0px\">";
						}
						else {
							var result = "<div class=\"cr_featuredproduct\" style=\"padding: 0px 0px 10px 0px\">";
						}
						
						result += "<img alt='alttext' src='imgsrc' width='100' height='85' /><br/>" + 
							"<a href='" + serverURL + "/partners/" + productBean.partnerKey + "/products/" + productBean.productKey + 								
							        "'>" + productBean.productName + "</a><br/>" + productBean.description;
						
						result = result.replace(/alttext/g, productBean.productName + " Logo");
						
						var server = "http://localhost.citrix.com/ready";
						if(serverURL) {
							server = serverURL; 
						}
						
						result = result.replace(/imgsrc/g, serverURL + productBean.logo);
						return result + "</div>";					
					}, { 
						rowOptions: function() {				    	
					    	return "";
					  	},
					  	
					  	cellOptions: function() {
					    	return "valign='top'";
					  	}
				});
			}
		}
	}	
	
	
	// bind a window.open method to add images links
	$("#bannerEditLink").click(function(){                    	        		
		var url = $(this).attr("href");
		var name= "HomepageBanner";        		        		
		window.open(url,name,'width=740,height=580,alwaysraised=yes,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no')
		return false;	       				
	});
	
	// initialize list
	populate(defaultLimit, captureDropDownValue());
	
	$("#dynamicTitle").append("" + getTitle($("#optionsDropDown").attr("value")));
	 
	// attach event to drop down menu change   			
	$("#optionsDropDown").change(function() {    
		
		var key = $(this).attr("value");  
		$("#dynamicTitle").empty();
		$("#dynamicTitle").append(getTitle(key));
		
		$("#dynamicMessage").empty();
		$("#dynamicMessage").append(message1);
		$("#productsContentTable").empty();
		populate(defaultLimit,key);        		
	});		        	
});
