// JavaScript Document

$(document).ready(function(){
				   
	$('a[name=close]').click(function(e){
		e.preventDefault();									  
		ClosePopup();
	});
		 
	$('#mask').click(function(e){
		ClosePopup();
	});
});

function waitPopup(){
	var modal = '.wait-popup';	
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	$('#mask').fadeIn(500);
	$('#mask').fadeTo("normal",0.7);
	
	$(modal).show();
	
	SetPopupPos(modal);
	
}

function CloseWaitPopup(){
	$('#mask').hide();
	$('.wait-popup').hide();
	if (navigator.userAgent.match('MSIE 6.0') && !navigator.userAgent.match('MSIE 7.0')){
		$('select').css('visibility', 'visible');
	}
	if (!navigator.userAgent.match('Firefox')) {
		$('object').css('visibility', 'visible');
	}
}

// show popup input element id (elm = element (#modal-content or id of element show data), mWidth = modal width, maxHeight = modal max height)
function ShowPopup(elm, mWidth, maxHeight){
	$('.wait-popup').hide();
	
	var modal = '#modal-popup';
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	$('#mask').fadeIn(500);
	$('#mask').fadeTo("normal",0.7);
	
	$(modal).fadeIn("slow");
	
	if (mWidth > 0) {
		$(modal).css('width', mWidth);
		$(elm).css('width', 'auto');
	}
	
	maxHeight = (maxHeight == undefined) ? $(window).height() * 0.85 : (maxHeight > $(window).height() * 0.85) ? $(window).height() * 0.85 : maxHeight;
	
//	if (maxHeight > 0){
		// order history pang and show addres in checkout page
		if ($(elm).height() > maxHeight){
			$(elm).css('overflow', 'auto');
			$(elm).height(maxHeight);
			if (navigator.userAgent.match('MSIE') && !navigator.userAgent.match('MSIE 8.0'))
				$(elm + ' > div > table').css('width', '97%');
			else{
				if (navigator.userAgent.match('MSIE') && !navigator.userAgent.match('MSIE 8.0')) 
					$(elm + ' > div > table').css('width', '100%');
			}
		} else {
			$(elm).css('overflow', 'visible');
		}
//	} 
	SetPopupPos(modal);
}

// show popup image for item details
function PopupImage(img, maxHeight){
	//alert(navigator.userAgent);
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	$('.wait-popup').hide();
	
	var modal = '#modal-popup';
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	$('#mask').fadeIn(500);
	$('#mask').fadeTo("normal",0.7);	
	$(modal).fadeIn(1000);

	$(modal).css('width', (isIE) ? img.width() + 60 : img.width() + 30);

	maxHeight = (maxHeight == undefined) ? $(window).height() * 0.85 : (maxHeight > $(window).height() * 0.85) ? $(window).height() * 0.85 : maxHeight;

	if (img.height() > maxHeight) {
		
		$(modal + ' > .content').css('overflow', 'auto');
		$(modal + ' > .content').height(maxHeight);
		
		if(navigator.userAgent.match('MSIE')){
			$(modal).css('width', img.width() + 30 + 48);
			$(modal + ' > .content').css('width', img.width() + 48);
		} else {
			$(modal + ' > .content').css('width', img.width() + 18);
		}
	} else {
		$(modal + ' > .content').css('overflow', 'visible');
		if(navigator.userAgent.match('MSIE'))
			$(modal + ' > .content').css('width', img.width() + 30);
	}
	SetPopupPos(modal);
}

function SetPopupPos(elm){
	var winH = $(window).height();
	var winW = $(window).width();	
	
	$(elm).css('top', winH/2 - $(elm).height()/2);
	$(elm).css('left', winW/2 - $(elm).width()/2);
	
	if ($(elm).css('position') != 'fixed')
		$(elm).css('top', winH/2 - ($(elm).height()/2) + $(window).scrollTop());	
	
	// hide select option
	if (navigator.userAgent.match('MSIE 6.0') && !navigator.userAgent.match('MSIE 7.0')){
		$('select').css('visibility', 'hidden');
	}
	if (!navigator.userAgent.match('Firefox')) {
		$('embed').css('visibility', 'hidden');
	}
}

function ClosePopup(){
	$('#mask').hide();
	$('#modal-popup').hide();
	
	if (navigator.userAgent.match('MSIE 6.0') && !navigator.userAgent.match('MSIE 7.0')){
		$('select').css('visibility', 'visible');
	}
	if (!navigator.userAgent.match('Firefox')) {
		$('embed').css('visibility', 'visible');
	}
	// for order history
//	$('#modal-content').html('');
	$('#modal-popup > .content').css('height', 'auto');
}

function ConfirmDialog(opts){
	
	$('.wait-popup').hide();
	
	var modal = '#modal-popup';
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	
	$("#modal-popup").width(420);
	
	$("#modal-popup .content").html('');
	$("#modal-popup .content").append("<p class=\"confirm\">" + opts.msg + "</p>");
	
	if(opts.btnYes!=null)
		$("#modal-popup .content").append(opts.btnYes);
	if(opts.btnNo!=null)
		$("#modal-popup .content").append(opts.btnNo);
	if(opts.btnOK!=null)
		$("#modal-popup .content").append(opts.btnOK);
	
	$("#modal-popup .content input[type=button]").addClass('button');
	
	$('#mask').fadeIn(500);
	$('#mask').fadeTo("normal",0.7);
	$(modal).fadeIn(1000);
	
	SetPopupPos(modal);
	
	// hide select option
	if (navigator.userAgent.match('MSIE 6.0') && !navigator.userAgent.match('MSIE 7.0')){
		$('select').css('visibility', 'hidden');
	}
	if (!navigator.userAgent.match('Firefox')) {
		$('embed').css('visibility', 'hidden');
	}
}

function CloseConfirmDialog(){
	$('#mask').hide();
	$('#modal-popup').hide();
	$('#modal-popup .content').html('');
	if (navigator.userAgent.match('MSIE 6.0') && !navigator.userAgent.match('MSIE 7.0')){
		$('select').css('visibility', 'visible');
	}
	if (!navigator.userAgent.match('Firefox')) {
		$('embed').css('visibility', 'visible');
	}
}

function RefreshImage(elm) {
	elm.attr('src', elm.attr('src').split('?')[0] + '?x=' + new Date().getTime());
	$('#imgChk').attr('src', '/images/img_false.gif');
}

function AutoClosePopup(elm){
	var waitTime = 30;
	
	if(isThaiURL()){
		msgClose = "ปิดหน้าต่างนี้";
	} else {
		msgClose = "Close this windows";
	}
	
	$(elm).parents(".content").css({
		overflow: 'visible',
		height: 'auto'
	});
	
	var waitTimer = setInterval(function(){
		elm.val(msgClose + ' [' + waitTime + ']');
		waitTime--;
		if (waitTime==0){
			clearInterval(waitTimer);
			ClosePopup();
		}
	}, 1000);
	
	$('#autoclose').click(function(e){
		clearInterval(waitTimer);
		ClosePopup();
	});
}
