// JavaScript Document

// Check xem trình duyệt là IE6 hay IE7
var isIE		= (navigator.userAgent.toLowerCase().indexOf("msie") == -1 ? false : true);
var isIE6	= (navigator.userAgent.toLowerCase().indexOf("msie 6") == -1 ? false : true);
var isIE7	= (navigator.userAgent.toLowerCase().indexOf("msie 7") == -1 ? false : true);

function addCommas(nStr){
	nStr += ''; x = nStr.split(',');	x1 = x[0]; x2 = ""; x2 = x.length > 1 ? ',' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + '.' + '$2'); } return x1 + x2;
}

function ajaxLink(){
	$("a[rel^='ajax']").click(function(){
		domEle= $(this);
		temp	= domEle.attr("rel").match(/\[(.*?)\]/);
		if(temp !== null){
			sel	= temp[1];
			$.get(domEle.attr("href"), function(data){
				$(sel).html(data);
				if(typeof(domEle.attr("addJS")) != "undefined") eval(domEle.attr("addJS"));
			});
		}
		return false;
	});
}


// Show sản phẩm theo độ phân giải màn hình
function changeDataMatchResolution(sel){
	
	if(sel == "") sel = ".list_product,.list_review,.list_category";
	$(sel).each(function(index){
		
		width		= (typeof($(this).attr("dataWidth")) != "undefined" ? $(this).attr("dataWidth") : (isIE ? 157 : 158));
		col		= (typeof($(this).attr("dataCol")) != "undefined" ? $(this).attr("dataCol") : 8);
		
		numCol	= parseInt($(this).width() / width);
		if(numCol < 3) numCol = 3;
		if(numCol > col) numCol = col;
		colWidth	= 100 / numCol;
		if(isIE6 || isIE7) colWidth = colWidth - 0.1;
		
		$(this).find(".fl").width(colWidth + "%");
		n = $(this).attr("dataLine") * numCol;
		
		$(this).find(".fl:lt(" + n + ")").removeClass("hidden");
		if(n > 0) $(this).find(".fl:gt(" + (n-1) + ")").addClass("hidden");
		
		// Trường hợp page_div nằm bên trong
		$(this).find(".page_div a,.page_div_top a").each(function(index2){
			$(this).attr("href", function(index3, attr){
				return changePageSize(attr, n);
			});
		});
		
		// Trường hợp page_div nằm bên ngoài
		if($(this).hasClass("view_thumbnail_table") == true){
			$(this).parent().find(".page_div a,.page_div_top a").each(function(index2){
				$(this).attr("href", function(index3, attr){
					return changePageSize(attr, n);
				});
			});
		}
		
	});
	
}



function fixImageSize(width, height, maxWidth, maxHeight){
	// Check if the current width is larger than the max
	ratio		= maxWidth / width;   // Get ratio for scaling image
	w			= maxWidth;
	h			= height * ratio;    // Reset height to match scaled image
	// Check if current height is larger than max
	if(h > maxHeight){
		ratio	= maxHeight / height; // Get ratio for scaling image
		w		= width * ratio;    // Reset width to match scaled image
		h		= maxHeight;
	}
	return Array(parseInt(w), parseInt(h));
}



// Nếu ảnh bị lỗi thì thay bằng no_photo.gif
function pictureError(sel){
	$(sel + " .picture_xx_small img").error(function(){
		$(this).attr("src", fs_imagepath + "no_photo_xx_small.gif");
	});
	$(sel + " .picture_x_small img").error(function(){
		$(this).attr("src", fs_imagepath + "no_photo_x_small.gif");
	});
	$(sel + " .picture_small img").error(function(){
		$(this).attr("src", fs_imagepath + "no_photo_small.gif");
	});
	$(sel + " .picture_medium img").error(function(){
		$(this).attr("src", fs_imagepath + "no_photo_medium.gif");
	});
}

// Resize image with maxWidth + maxHeight
function resizeImage(){
	$("img[maxWidth],img[maxHeight]").each(function(){
		var maxWidth	= $(this).attr("maxWidth"); // Max width for the image
		var maxHeight	= $(this).attr("maxHeight"); // Max height for the image
		var width		= $(this).width();    // Current image width
		var height		= $(this).height();  // Current image height
		// Check if the current width is larger than the max
		ratio		= maxWidth / width;   // Get ratio for scaling image
		width		= maxWidth;
		height	= height * ratio;    // Reset height to match scaled image
		// Check if current height is larger than max
		if(height > maxHeight){
			ratio		= maxHeight / height; // Get ratio for scaling image
			height	= maxHeight;
			width		= width * ratio;    // Reset width to match scaled image
		}
		$(this).attr({ width: width, height: height });
	});
}



// Resize window về đúng độ phân giải màn hình (Full Screen)
function resizeWindow(){
	
	minWidth	= 994;
	width		= ($("body").width() < minWidth ? minWidth : $("body").width());
	if(typeof(fixBodyWidth) != "undefined") width = fixBodyWidth;
	
	$("#container_body,.header_bar").width(width);
	search_keyword_width	= width - $(".header_search .search_help").width() -
											 $(".header_search .search_in").width() -
											 $(".header_search .search_area").width() -
											 (typeof($(".header_search .search_area").css("margin-right")) != "undefined" ? parseInt($(".header_search .search_area").css("margin-right")) : 0) -
											 $(".header_search .register_estore").width() - 170;
	$(".header_search .search_keyword").width(search_keyword_width);
	$(".header_banner_top").width(width - $(".header_menu").width() - $(".header_logo").width() - 20);
	
	changeDataMatchResolution("");
	
}



/*** Tooltip ***/
function tooltipProduct(){
	$(".list_product pre,.view_list_table pre,.view_collapse_table pre,.compare_table pre,.suggestion_product_content pre,.upload_gallery pre,.other_estore_product_exclusive_table pre,.show_cart_table pre").each(function(index, domEle){
		ob	= $(domEle).parent().find(".tooltip");
		var img	= $(domEle).parent().find(".tooltip img");
		ob.tooltip({
			bodyHandler: function(){
				tooltipWidth	= (typeof(ob.attr("tooltipWidth")) != "undefined" ? ob.attr("tooltipWidth") : 300);
				$("#tooltip").css("width", tooltipWidth + "px");
				$(domEle).find(".picture,.picture_only").html(function(index, html){
					width			= (typeof($(this).attr("width"))	!= "undefined" ? $(this).attr("width") : img.width());
					height		= (typeof($(this).attr("height"))!= "undefined" ? $(this).attr("height") : img.height());
					maxWidth		= (typeof($(this).attr("maxWidth"))	!= "undefined" ? $(this).attr("maxWidth") : 250);
					maxHeight	= (typeof($(this).attr("maxHeight"))!= "undefined" ? $(this).attr("maxHeight") : 350);
					arrFixSize	= fixImageSize(width, height, maxWidth, maxHeight);
					return '<img width="' + arrFixSize[0] + '" height="' + arrFixSize[1] + '" src="' + $(this).attr("src") + '" />';
				});
				return $(domEle).html();
			},
			track: true,
			showURL: false,
			extraClass: "tooltip_product"
		});
	});
}

function tooltipReview(){
	$(".list_review pre").each(function(index, domEle){
		ob	= $(domEle).parent().find(".tooltip");
		ob.tooltip({
			bodyHandler: function(){
				$("#tooltip").css("width", "300px");
				$(domEle).find(".picture,.picture_only").html(function(index, html){
					return '<img src="' + $(this).attr("src") + '" />';
				});
				return $(domEle).html();
			},
			track: true,
			showURL: false,
			extraClass: "tooltip_product"
		});
	});
}

function tooltipPicture(){
	$(".tooltip_picture").tooltip({
		bodyHandler: function(){
			$("#tooltip").css("width", "auto");
			strReturn	= '<div class="picture">' + resizeImageSrc(this.href, $(this).find("img").width(), $(this).find("img").height(), 350, 300) + '</div>';
			if($(this).attr("title") != "" ) strReturn = strReturn + '<div class="comment">' + $(this).attr("title") + '</div>';
			return strReturn;
		},
		track: true,
		showURL: false,
		extraClass: "tooltip_picture"
	});
}
/*-- End Tooltip --*/

/*** Initiate Load ***/
function initLoad(){
	
	if(!isIE6){
		resizeWindow();
		$(window).resize(function(){
			resizeWindow();
		});
	}
	
	$(".tooltip_search_help").tooltip({
		bodyHandler: function(){
			$("#tooltip").css("width","500px");
			return tooltip_search_help;
		},
		track: true,
		showURL: false,
		extraClass: ""
	});
	

	
}

// Run a function when the page is fully loaded
function initLoaded(){	
	resizeImage();	
	tooltipProduct();
	if(screen.width > 1024) $(".center_banner table td div").css("width", "auto");	
}
/*-- End Initiate On Load --*/
