// 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 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));
}






// Di chuột vào link show content ở header bar
function headerBarDropdown(){
	if(isIE6 || isIE7) $(".header_bar .hidden").css("margin-top", "-1px");
	$(".header_bar .bar_link, .header_bar .bar_text a").mouseenter(function(){
		$(".header_bar .hidden").hide();
		ob	= $(this).nextAll(".hidden");
		ob.show();
		// Set vị trí bên phải để tránh bị overflow
		if((ob.position().left + ob.width()) > $("body").width()){
			ob.css("right", "0px");
		}
	});
	$(".header_bar .close a").click(function(){
		$(".header_bar .hidden").hide();
	});
	$(".header,.header_search,.header_season,.header_banner,.header_navigate_div,div[class^='container_content_'],#container_content,#container_footer").mouseenter(function(){
		$(".header_bar .hidden").hide();
	});
}




// 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 });
	});
}

function resizeImageSrc(src, width, height, maxWidth, maxHeight){
	// 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
	}
	return '<img src="' + src + '" 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("");
	
}

function showTeaserProduct(){
	$(".view_list_table .teaser,.view_list_table .exclusive_teaser").each(function(index, domEle){
		defHeight	= $(domEle).css("height");
		$(".view_list_table .show_teaser a:eq(" + index + ")").toggle(
			function(){
				$(this).attr("title", "Thu nhỏ");
				$(this).removeClass("expand");
				$(domEle).css("height", "auto");
			},
			function(){
				$(this).attr("title", "Mở rộng");
				$(this).addClass("expand");
				$(domEle).css("height", defHeight);
			}
		);
	});
}

/*** 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") : 300);
					maxHeight	= (typeof($(this).attr("maxHeight"))!= "undefined" ? $(this).attr("maxHeight") : 200);
					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: ""
//	});
//	
//	$(".generate_star").html(function(index, html){
//		return generateVote(html);
//	});
//	
//	headerBarDropdown();
//	
//	$(".logout").attr("href", 'javascript:if(confirm("Bạn có muốn thoát khỏi hệ thống không?")) window.location.href = con_root_path + "act_logout.php";');
//	
//	$("ul.header_navigate li").hover(
//		function(){
//			$(this).addClass("hover");
//			$("ul:first", this).css("visibility", "visible");
//		},
//		function(){
//			$(this).removeClass("hover");
//			$("ul:first", this).css("visibility", "hidden");
//		}
//	);
//	$("ul.header_navigate li ul li:has(ul)").find("a:first").addClass("level_1");
//	
//	$(".left_menu li:last-child,.left_menu_quick_search div:last-child,.left_menu_quick_search li:last-child,.left_banner td:last,.right_banner td:last").addClass("end");
//	
//	defaultValueHeaderSearch();
	
}

// Run a function when the page is fully loaded
function initLoaded(){
	
	resizeImage();
	
	tooltipProduct();
	
//	initColorBox();
//	
//	ajaxLink();
//	
//	fixHeightFooter();
	
	if(screen.width > 1024) $(".center_banner table td div").css("width", "auto");
	
}
/*-- End Initiate On Load --*/
