// JavaScript Document
// check all function
$(document).ready(function(){
	// img hover change
	var sourceSwap = function () {
    var $this = $(this);
    var newSource = $this.attr('src2');
    $this.attr('src2', $this.attr('src'));
    $this.attr('src', newSource);
	}
	$(function () {
		$('img.hover-change').hover(sourceSwap, sourceSwap);
	});
// cart submit and cart box
/*
	if(typeof $.fancybox == 'function'){
		$('.btn-add-cart').click(function() {
			$.ajax({
				type		: 'POST',
				cache	: false,
				url		: '../shop_box.php-do=list_items.htm'/*tpa=http://www.rcmultitech.co.th/shop_box.php?do=list_items*/,
				data		: $(this).closest('#form-option-select').serializeArray(),
				success: function(data) {
					$.fancybox(data,{
						padding : 0,
						margin : 3,
						minWidth	: 300,
						maxWidth	: 600,
						Height : '100%',
						width:'100%',
						fitToView	: false,
						autoSize	: false,
						closeClick	: false,
						modal : true,
						beforeShow: function(){
						  $('.fancybox-skin').css('backgroundColor','transparent');
						  $('.fancybox-skin').css('box-shadow','none');
						 }
						});
				}
			});
			return false;
		});
		$('.shop-box').fancybox({
			padding : 0,
			margin : 3,
			minWidth	: 300,
			maxWidth	: 600,
			Height : '100%',
			width:'100%',
			fitToView	: false,
			autoSize	: false,
			closeClick	: false,
			modal : true,
			beforeShow: function(){
			  $('.fancybox-skin').css('backgroundColor','transparent');
			  $('.fancybox-skin').css('box-shadow','none');
			 }
		});
	}
	*/
	//check auto resize img left and right
	$('.content-left,.content-right').find('img').each(function(){
		pageBody = $('.content-left').width();
		if (pageBody==null) pageBody = $('.content-Right').width();
		imgWidth=$(this).width();
		if (pageBody>0) {
			containerWidth=400;
			if (imgWidth > containerWidth) {
				$(this).css({
					height: 'auto',
					width: '100%'
				});
			}
		}
	});
	// check auto resize img,iframe,table if width > container
	$('.content-center').find('img').each(function(){
		contentLeft=$('.content-left').width();
		contentRight=$('.content-right').width();
		pageBody=$('#page-body').width();
		if (pageBody == null) {
			pageBody=$('.content-center').width()+20;
			contentLeft=0;
			contentRight=0;
		}
		imgWidth=$(this).width();
		if (pageBody>0) {
			containerWidth=pageBody-contentLeft-contentRight;
			if (imgWidth > containerWidth) {
				$(this).css({
					height: 'auto',
					width: '100%'
				});
			}
		}
	});
	/*
	$('.box-content-body-t').find('img').each(function(){
		contentLeft=$('.content-left').width();
		contentRight=$('.content-right').width();
		pageBody=$('#page-body').width();
		imgWidth=$(this).width();
		if (pageBody == null) {
			pageBody=$('.box-content-body-t').width();
			contentLeft=0;
			contentRight=0;
		}
		if (pageBody>0) {
			containerWidth=pageBody-contentLeft-contentRight;
			if (imgWidth > containerWidth) {
				$(this).css({
					height: 'auto',
					width: '100%'
				});
			}
		}
	});
	*/
	// for mobile
	$('.mobile-content').find('img,iframe').each(function(){
		imgRatio = $(this).height()/$(this).width();
		imgWidth=$(this).width();
		containerRatio = $(this).parent().height()/$(this).parent().width();
//		containerWidth=$(this).parent().width();
		containerWidth=$('.mobile-content').width();
		if (imgWidth > containerWidth) {
			$(this).css({
				height: 'auto',
				width: '90%'
			});
		}
//		box-content2-body-t
		/*
		if (imgRatio > containerRatio) {
			$(this).css({
				height: 'auto',
				width: '100%'
			});
		} else {
			$(this).css({
				height: 'auto',
				width: '100%'
			});
		}*/
	});// auto resize img
	$.getScript('jqEasyCharCounter/jquery.jqEasyCharCounter.min.js'/*tpa=http://www.rcmultitech.co.th/java/jqEasyCharCounter/jquery.jqEasyCharCounter.min.js*/, function() {
		$('.limit-text255').jqEasyCounter({
			'maxChars': 255,
			'maxCharsWarning': 200
		});
		$('.char-count255').jqEasyCounter({
			'maxChars': 255,
			'maxCharsWarning': 70
		});
		$('.char-count100').jqEasyCounter({
			'maxChars': 100,
			'maxCharsWarning': 70
		});
	});
	$.getScript('jquery.lazyload.js'/*tpa=http://www.rcmultitech.co.th/java/jquery.lazyload.js*/, function() {
		$(".lazyload").lazyload({ 
	//		effect : "fadeIn",
			placeholder : "../images/grey.gif"/*tpa=http://www.rcmultitech.co.th/images/grey.gif*/
		 });
		 /*
		 $(".lazyload").lazyload({
	//		 effect : "fadeIn",
			 event : "sporty",
			placeholder : "../images/grey.gif"/*tpa=http://www.rcmultitech.co.th/images/grey.gif*/
		 });
		 $(window).bind("load", function() { 
			 var timeout = setTimeout(function() { $("img").trigger("sporty") }, 100);
		 });      
		 */
	});
	//jQuery lazyload
//	$('.content-center').urlToLink({target:'_blank'});
	// คำนวนหากค่าเมนูด้านบนอยู่ชิดขวาจนเกินไป
	/*
	$(".nav-menu .menu>li").hover(function () {
		var p = $(this);
		var position = p.offset();
		var left = position.left;
		var page_width=$("#page-body").width();
//		alert(page_width);
		if ((page_width - left)<230) {
			 $("ul").css({
			"left": "auto",
			"right": "0px"
			});
		} 
	}, function () {
		 $(".menu ul.dropdown-top").css({
		"left": "-10px",
		"right": "auto"
		});
	});
	*/
	$(window).scroll(function() {
		var win=$(document).height(); 
		var scrollBottom = $(window).scrollTop() + $(window).height();
		var scrollTop = $(window).scrollTop();
		var windowH = $(window).height();
		if ((scrollBottom>=(win-100)) && (scrollTop!=0)) {
			$('#powered-icon').slideDown();

		} else {
			$('#powered-icon').slideUp();
		}
	});
	// tipsy
	// nw | n | ne | w | e | sw | s | se
	$('.tool-tip-form[title]').tipsy({trigger: 'focus', gravity: 'w',html: true,opacity: 1});
	$('.tool-tip-form-s[title]').tipsy({trigger: 'focus', gravity: 's',html: true,opacity: 1});
	$('.tool-tip-nw').tipsy({gravity: 'nw',html: true,opacity: 1});
	$('.tool-tip-n').tipsy({gravity: 'n',html: true,opacity: 1});
	$('.tool-tip-ne').tipsy({gravity: 'ne',html: true,opacity: 1});
	$('.tool-tip-w').tipsy({gravity: 'w',html: true,opacity: 1});
	$('.tool-tip-e').tipsy({gravity: 'e',html: true,opacity: 1});
	$('.tool-tip-sw').tipsy({gravity: 'sw',html: true,opacity: 1});
	$('.tool-tip-s').tipsy({gravity: 's',html: true,opacity: 1});
	$('.tool-tip-se').tipsy({gravity: 'se',html: true,opacity: 1});
//	$('.tool-tip-se-l').tipsy({delayOut: 3000,live:true,gravity: 'se',html: true,opacity: 1});
	//fade
	$('.tool-tip-nwf').tipsy({gravity: 'nw',html: true,fade: true});
	$('.tool-tip-nf').tipsy({gravity: 'n',html: true,fade: true});
	$('.tool-tip-nef').tipsy({gravity: 'ne',html: true,fade: true});
	$('.tool-tip-wf').tipsy({gravity: 'w',html: true,fade: true,opacity: 1});
	$('.tool-tip-ef').tipsy({gravity: 'e',html: true,fade: true});
	$('.tool-tip-swf').tipsy({gravity: 'sw',html: true,fade: true});
	$('.tool-tip-sf').tipsy({gravity: 's',html: true,fade: true});
	$('.tool-tip-sef').tipsy({gravity: 'se',html: true,fade: true});
	// tipsy
	$('.nyroModal,.myModal').nm();
	$('.dynamic_select').bind('change', function () {
		var url = $(this).val(); // get selected value
		if (url) { // require a URL
		  window.location = url; // redirect
		}
		return false;
	});
	$("#check-all").click(function() {
		var checked_status = this.checked;
		$(".mycheck").each(function(){
			this.checked = checked_status;
		});
	});
	// load image after html
	 $(function(){
		$.each(document.images, function(){
			   var this_image = this;
			   var src = $(this_image).attr('src') || '' ;
			   if(!src.length > 0){
				   //this_image.src = options.loading; // show loading
				   var lsrc = $(this_image).attr('lsrc') || '' ;
				   if(lsrc.length > 0){
					   var img = new Image();
					   img.src = lsrc;
					   $(img).load(function() {
						   this_image.src = this.src;
					   });
				   }
			   }
		   });
	  });
	// ทำให้ลิงค์เลือนแบบ smooth ในหน้าเดียวกัน
	$('a[href*=#]:not([href=#])').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
			|| location.hostname == this.hostname) {
			var target = $(this.hash);
			target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
			   if (target.length) {
				 $('html,body').animate({
					 scrollTop: target.offset().top
				}, 1000);
				return false;
			}
		}
	});
	// ทำให้กล่องของสินค้ามีขนาดเท่ากันหมด
	var ShopThumbHeight = Math.max.apply(null, $(".shop-product-box").map(function ()
	{
		return $(this).height();
	}).get());
    var cssObj = {
      'height' : 15+ShopThumbHeight+'px'
    }
	//alert(ShopThumbHeight);
    $(".shop-product-box").css(cssObj);
	$(".shop-product-box div.addcart-btn").addClass("shop-addcart-thumb-pos");
	// show hide zoom
	$(".imagePreview").hover(
	  function() { $(this).children("#zoom-hover-box").show(); },
	  function() { $(this).children("#zoom-hover-box").hide(); }
	);
	// check form product option2
	$('.shop_product_option2').blur(function()
	{
		if ($('.shop_product_option2').val()) {
			 $('.shop_product_option2_name').addClass('required');
		} else {
			 $('.shop_product_option2_name').removeClass('required');
		}
	});
	//disable form after submit
	/*
	$("form").validate({
		submitHandler: function(form) {
		 $('input[type="submit"]').attr('disabled','disabled');
		// disable your button here
		form.submit();
	   },
	   invalidHandler: function() {
		 // re-enable the button here as validation has failed
		 $('input[type="submit"]').attr('enable','enable');
	   }
	});
	*/
}); //end $(document).ready(function(){
	
function popupCenter(Location,name,w,h) {   
	var myleft=(screen.width)?(screen.width-w)/2:100;	
	var mytop=(screen.height)?(screen.height-h)/2:100;	
	var options = "width=" + w + ",height=" + h;   options += ",menubar=no,location=no,resizable,scrollbars,top="+mytop+",left="+myleft;    
	var newwin = window.open(Location,name,options);    
	if (newwin == null)   {     // The popup got blocked, notify the user    
	 	return false;   
	 }  newwin.focus(); 
 } 

function showMyDiv (it, box) {
  var vis = (box.checked) ? "block" : "none";
  document.getElementById(it).style.display = vis;
}
// confirm click
function confirm_entry(url,str) {
	input_box=confirm(str);
	if (input_box==true){ 
		// Output when OK is clicked
		window.location.href=url;
	} else {
		// Output when Cancel is clicked
	}
}
// confirm submit
function ConfirmSubmit(str) {
	//OnClick="return ConfirmSubmit('text');"
	var agree = prompt(str+"\nกรุณายืนยันโดยการพิมพ์ \"YES\" เพื่อยืนยัน", "");
//	var agree=confirm(str);
	if (agree !="" && agree != null) var agree = agree.toUpperCase();
	if (agree == "YES") {
//	if (agree)
		return true ;
	} else {
		return false ;
	}
}

function goThere(){
	var list=document.forms[0].goURL;
	var URL=list.options[list.selectedIndex ].value;
		if (URL) {
			location=URL;
		}
}
//url2link function
(function($){
	$.fn.urlToLink = function(options) {
		var options = $.extend({}, $.fn.urlToLink.defaults, options);
		return this.each(function(){
			$(this).html( $(this).html().replace(/\s(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, " <a href='$1' target='"+options.target+"'>$1</a>") );
		}),
			$(this).html( $(this).html().replace(/\s(\b(www)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, " <a href='http://$1/' target='"+options.target+"'>$1</a>") );
	}
	//Default configuration
	$.fn.urlToLink.defaults = {
		target : '_self' // Link target
	}
})(jQuery)