var UrlEncode = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
		return string;
	}
};

function getQueryParam(paramName) {
	strQuery = window.location.search.substring(1);
	arrParam = strQuery.split("&");
	for (i=0;i<arrParam.length;i++)
	{
		var paramItem = arrParam[i].split("=");
		if (paramItem[0] == paramName)
		{
			return paramItem[1];
		}
	}
	return '';
}

function getDomainFromUrl(strUrl)
{
	return strUrl.match(/:\/\/(.[^/]+)/)[1];
}

function numberFormat()   
{
	nStr = $(this).val();   
	nStr += '';   
	x = nStr.split('.');   
	x1 = x[0];   
	x2 = x.length > 1 ? '.' + x[1] : '';   
    var rgx = /(\d+)(\d{3})/;   
    while (rgx.test(x1))   
        x1 = x1.replace(rgx, '$1' + ',' + '$2');   
    $(this).val('txt1').value = (x1 + x2);   
}

function showHomeSlideCat(obj, index)
{
	$('#homeSlideCatTab dt').removeClass("tabselected");
	var listCarousel = $('#listCatSlideImage .jcarousel-skin-tango').css('display','none');
	$(obj).addClass('tabselected');
	$(listCarousel[index]).css('display','');
}

function initHomeSlideCat()
{
	$($('#homeSlideCatTab dt')[0]).addClass('tabselected');
	var listCarousel = $('#listCatSlideImage .jcarousel-skin-tango').css('display','none');
	$(listCarousel[0]).css('display','');
}

function frameItMouseOver()
{
	$('#frameItTitle').attr('class','pdetail-frame-it-title-hover');
	$('#frameItArrow').attr('class','hover');
}

function frameItMouseOut()
{
	$('#frameItTitle').attr('class','pdetail-frame-it-title');
	$('#frameItArrow').attr('class','');
}


function ResetHeight4ListProduct()
{
	var arrDiv = $('#list-product div.list-product-image');
	var arrA = $('#list-product div.list-product-image a');
	var divCount = arrDiv.length;
	var maxHeight = 0;
	var maxInnerHeight = 0;
	var arrMarginTop = new Array();
	for(var i=0; i < divCount; i++)
	{
		var tmpHeight = parseInt($(arrDiv[i]).innerHeight(),10);
		maxHeight = (maxHeight < tmpHeight) ? tmpHeight : maxHeight;
	}
	
	for(var i=0; i < divCount; i++)
	{
		var tmpHeight = parseInt($(arrA[i]).innerHeight(),10);
		arrMarginTop[i] = (tmpHeight < maxHeight) ? parseInt((maxHeight - tmpHeight)/2) : 0;
	}
	
	for(var i=0; i < divCount; i++)
	{
		if(arrMarginTop[i] > 0)
		{
			$(arrA[i]).css('margin-top', arrMarginTop[i] + 'px');
		}
		$(arrDiv[i]).css('height', (maxHeight) + 'px');
	}
	
	ResetHeight4ListProduct2();
}
function ResetHeight4ListProduct2()
{
	var arrDivInnerBox = $('#list-product div.lstp-innerbox');
	var divCount = arrDivInnerBox.length;
	var check = false;
	for(var i=0; i < divCount; i++)
	{
		var tmp2 = parseInt($(arrDivInnerBox[i]).innerHeight(),10);
		if(tmp2 == 0)
		{
			check = true;
			break;
		}
	}
	
	if(check)
	{
		ResetHeight4ListProduct2();
	}
	
	var maxInnerHeight = 0;
	for(var i=0; i < divCount; i++)
	{
		var tmp2 = parseInt($(arrDivInnerBox[i]).innerHeight(),10);
		maxInnerHeight = (maxInnerHeight < tmp2) ? tmp2 : maxInnerHeight;
	}
	for(var i=0; i < divCount; i++)
	{
		$(arrDivInnerBox[i]).css('height', maxInnerHeight + 'px');
	}
	
}

function ResetHeight4ListProduct2()
{
	var arrDivInnerBox = $('#list-product div.lstp-innerbox');
	var divCount = arrDivInnerBox.length;
	var maxInnerHeight = 0;
	for(var i=0; i < divCount; i++)
	{
		var tmp2 = parseInt($(arrDivInnerBox[i]).innerHeight(),10);
		maxInnerHeight = (maxInnerHeight < tmp2) ? tmp2 : maxInnerHeight;
	}
	for(var i=0; i < divCount; i++)
	{
		$(arrDivInnerBox[i]).css('height', maxInnerHeight + 'px');
	}
	
}

/* show popup choose size */
function showPopupChooseSize(id, type)
{
	$('div.choose-your-size').css('display', 'none');
	var pos = $('#container').position();
	var boxId = '#divChooseYourSize_' + id;
	var maxPosLeft = pos.left + 780;
	var lstAvailableFrameSizeId = $('#availableFrameSizeID-' + id).val();
	
	if($.trim(lstAvailableFrameSizeId) == "" && type == '1')
	{
		alert('Framing not available for this product');
		return;
	}
	
	var arrAvailableFrameSizeId = lstAvailableFrameSizeId.split(',');
	var numAvailableFrameSizeId = arrAvailableFrameSizeId.length;
	
	var arrRadio  = $(boxId + ' .radioChooseSize');
	var count = arrRadio.length;
	
	// check co 1 size
	if(count < 2)
	{
		if(type == "1")
		{
			var link = $('#link_framedit_' + id + '_' + $(arrRadio[0]).val()).val();
			window.open(link, 'Artvina', 'width=1000,height=780,menubar=no');
			return;
			
		}
		if(type == "2")
		{
			var link = $('#link_add2cart_' + id + '_' + $(arrRadio[0]).val()).val();
			window.location = link;
			return;
		}
	}
	// end check co 1 size
	
	
	
	
	$(boxId).css('display','');
	var pos2 =  $(boxId).position();
			
	if(pos2.left + 452 > maxPosLeft)
	{
		$(boxId).css('left', (pos2.left - (pos2.left + 452 - maxPosLeft + 2)) + 'px');
	}
	$('#hdd-choose-size-type-' + id).val(type);
	
	$(boxId + ' .starNote-' + id).css('display','none');
	$(boxId + ' .radioChooseSize').css('display','');
	$(boxId + ' .starNote-' + id);
	$('#frameNoteText-' + id).css('display','none');
	var arrStarNote = $(boxId + ' .starNote-' + id).css('display','none');
	
	if(type == '1')
	{
		$('#choose-size-button-' + id).html('See It Framed');	
	}
	if(type == '2')
	{
		$('#choose-size-button-' + id).html('Add to cart');
	}
	
	for(var i = 0; i < count; i++)
	{
		$(arrRadio[i]).attr('checked',false);
		if(type == '1')
		{
			var sizeId = $(arrRadio[i]).val();
			var check = false;
			for(var j = 0; j < numAvailableFrameSizeId; j++)
			{
				if(arrAvailableFrameSizeId[j] == sizeId)
				{
					check = true;
					break;
				}
			}
			if(check)
			{
				$(arrRadio[i]).css('display','');
			}
			else
			{
				$(arrRadio[i]).css('display','none');
				$(arrStarNote[i]).css('display','');
				$('#frameNoteText-' + id).css('display','');
			}
		}
	}
	
	$('#listProductSize_' + id + ' p').css('font-weight','normal');
}
function closePopupChooseSize(id)
{
	var boxId = '#divChooseYourSize_' + id;
	$(boxId).css('display','none');
}

function ChoosePopUpSize(pid, id)
{
	var boxId = '#cys_item_' + pid + '_' + id;
	$('#hdd-choose-sizeid-' + pid).val(id);
	$('#listProductSize_' + pid + ' p').css('font-weight','normal');
	$(boxId + ' p').css('font-weight','bold');
	
}

function chooseSizeButton(id)
{
	var type = $('#hdd-choose-size-type-' + id).val();
	var boxId = '#listProductSize_' + id;
	var arrRadio  = $(boxId + ' .radioChooseSize');
	var count = arrRadio.length;
	var check = false;
	for(var i = 0; i < count; i++)
	{
		if($(arrRadio[i]).attr('checked'))
		{
			check = true;
			break;
		}
	}
	if(!check)
	{
		alert('Bạn phải chọn kích thước sản phẩm!');
		return;
	}
	var chooseSizeId = $('#hdd-choose-sizeid-' + id).val();
	if(type == "1")
	{
		var link = $('#link_framedit_' + id + '_' + chooseSizeId).val();
		window.open(link, 'Artvina', 'width=1000,height=780,menubar=no');
		return;
		
	}
	if(type == "2")
	{
		var link = $('#link_add2cart_' + id + '_' + chooseSizeId).val();
		window.location = link;
		return;
	}
}
/* end show popup choose size */

/* product detail */
function pDetailChooseSize(id)
{
	$('#divOtherSize td').css('font-weight','normal');
	$('#' + id + ' td').css('font-weight','bold');
	$('#hdd-choose-sizeid').val(id);
}

function pDetailSizeButton(type)
{
	var boxId = '#divSelectSize';
	if(type == "1")
	{
		var arrNote = $(boxId + ' span.starNote-select');
		var starNote = $(arrNote[0]).html();
		if($.trim(starNote) == '*')
		{
			alert('Kích thước bạn chọn không có thông tin frame. Bạn vui lòng chọn kích thước khác!');
			return;
		}
		else
		{
			var arrlink = $(boxId + ' input.link_framedit');
			var link = $(arrlink[0]).val();
			window.open(link, 'Artvina', 'width=900,height=678,menubar=no');
			return;
		}
		
	}
	if(type == "2")
	{
		var arrlink = $(boxId + ' input.link_add2cart');
		var link = $(arrlink[0]).val();
		window.location = link;
		return;
	}
}

/* end product detail */

function homeEventsSlide()
{
	$('#slider').s3Slider({timeOut: 4000});
}

function updateHeaderNumCart(numItem)
{
	$('#hMyCartNum-toannh').html('(' + numItem + ')');
}


/* product frame it detail */
function frameItDetailFrameIt()
{
	var arrHdd = $('#divFrameIt input');
	window.open($(arrHdd[0]).val(), 'Artvina', 'width=1000,height=780,menubar=no');
}
/* end product frame it detail */
