jQuery.noConflict();
window.onerror=function(){
	return false;
}
function numbersonly(e, decimal) {
	var key;
	var keychar;
	
	if (window.event) {
	   key = window.event.keyCode;
	}
	else if (e) {
	   key = e.which;
	}
	else {
	   return true;
	}
	keychar = String.fromCharCode(key);
	
	if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
	   return true;
	}
	else if ((("0123456789").indexOf(keychar) > -1)) {
	   return true;
	}
	else if (decimal && (keychar == ".")) { 
	  return true;
	}
	else
	   return false;
}

function validComment(){	
	var el = document.getElementById("author");
	if(el) {
		if(el.value.trim() == "" || el.value.trim() == "Name *"){
			alert('Please enter your name!');
			el.focus();
			return false;
		}
	}
	
	
	var el = document.getElementById("email");
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;	
	if(	el) {
		if(el.value.trim() == "" || el.value.trim() == "Email Address *"){
			alert('Please enter your email!');
			el.focus();
			return false;
		} else {
			if(reg.test(el.value) == false) {
				alert('Please enter your valid email!');
				el.focus();
				return false;
			}
		}
	}
	var el = document.getElementById("comment");
	if(el.value.trim() == "" || el.value.trim() == "Your Comment *"){
		alert('Please enter your comment!');
		el.focus();
		return false;
	}
	return true;
}
function validContact(){	
	var el = document.getElementById("txtName");
	if(el.value.trim() == "" || el.value.trim() == "Name *"){
		alert('Please enter your name!');
		el.focus();
		return false;
	}
	
	
	
	var el = document.getElementById("txtEmail");
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;		   
	if(el.value.trim() == "" || el.value.trim() == "Email Address *"){
		alert('Please enter your email!');
		el.focus();
		return false;
	} else {
		if(reg.test(el.value) == false) {
			alert('Please enter your valid email!');
			el.focus();
			return false;
		}
	}
	
	var el = document.getElementById("message");
	if(el.value.trim() == ""){
		alert('Please enter your message!');
		el.focus();
		return false;
	}
	return true;
}
function validPostComment(){	
	var el = document.getElementById("author");
	if(el.value.trim() == ""){
		alert('Please enter your name!');
		el.focus();
		return false;
	}
	
	var el = document.getElementById("email");
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;		   
	if(el.value.trim() == ""){
		alert('Please enter your email!');
		el.focus();
		return false;
	} else {
		if(reg.test(el.value) == false) {
			alert('Please enter your valid email!');
			el.focus();
			return false;
		}
	}
	
	var el = document.getElementById("comment");
	if(el.value.trim() == ""){
		alert('Please enter your comment!');
		el.focus();
		return false;
	}
	return true;
}
//zshowpopup(jQuery('#popupChooseList'), {closes: ['btnClose']});
function zshowpopup(sel, options){
	var jpopup = jQuery(sel);
	
	if (jpopup.length) {
		jwindow = jQuery(window),
		jhtml = jQuery('body'),
		inittop = jwindow.scrollTop();

		var opt = {
			zIndex: 1999,
			opacity: 0.9,
			closes: ['close'],
			masktime: 500,
			outsideclose: false,
			layertime: 700,
			onclose: false
		};

		jQuery.extend(opt, options);
		
		var zinstance = 'zsinryu' + new Date().getTime();
		var overlay = jQuery('#zmask');
		
		if (!overlay.length) {
			overlay = jQuery('<div id="zmask"></div>').css({
				position: 'absolute',
				top: 0,
				left: 0,
				width: jhtml.innerWidth(),
				height: jhtml.innerHeight(),
				zIndex: opt.zIndex,
				opacity: 0
			}).appendTo(document.body);
		}

		jpopup.insertAfter(overlay);

		var zposition = function(){
			return { top: Math.max(0, (jwindow.height() - jpopup.outerHeight(true)) / 2), left: Math.max(0, (jwindow.width() - jpopup.outerWidth(true)) / 2) };
		}, scroll = function () {
			if (jwindow.height() < jpopup.outerHeight(true) || jwindow.width() < jpopup.outerWidth(true)) {
				jpopup.css({
					'position': 'absolute',
					'top': inittop
				});
			} else {
				if (jpopup.css('position') != 'fixed') {
					var newpos = zposition();

					jpopup.css({
						'position': 'fixed',
						'top': newpos.top
					});
				}
			}
		}, resize = function () {
			var newpos = zposition();
			jpopup.css({
				'position': ((jwindow.height() < jpopup.outerHeight(true)) || (jwindow.width() < jpopup.outerWidth(true))) ? 'absolute' : 'fixed',
				'top': newpos.top,
				'left': newpos.left
			});

			if (overlay) {
				overlay.css({
					'width': jhtml.innerWidth(),
					'height': jhtml.innerHeight()
				});
			}
		}, close = function(){
			if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) {
				setTimeout(function () {
					jpopup.css({ 'visibility': 'hidden', 'top': -5000 });
				}, opt.layertime / 3);

			} else {
				jpopup.fadeTo(opt.layertime, 0, function(){
					jpopup.css('top', -5000);
				});
			}

			if (overlay) {
				overlay.fadeTo(opt.masktime, 0, function(){
					overlay.remove();
				});
			}

			jwindow.unbind('scroll.' + zinstance, scroll);
			jwindow.unbind('scroll.' + zinstance, resize);
			
			if(opt.onclose){
				opt.onclose.call(this);
			}
			
			return false;
		};

		jwindow.bind('scroll.' + zinstance, scroll);
		jwindow.bind('resize.' + zinstance, resize);
		jQuery(".btnClose").unbind('click').bind('click', close);
		var closebtn = jpopup.find('.' + opt.closes[0]);
		if(closebtn){
			closebtn.unbind('click').bind('click', close);
			closebtn.css({
				'zIndex': opt.zIndex + 2
			});
		}
		
		if(opt.outsideclose && overlay.length){
			overlay.unbind('click.' + zinstance).bind('click.' + zinstance, close);
		}

		var pos = zposition();

		jpopup.css({
			'position': ((jwindow.height() < jpopup.outerHeight(true)) || (jwindow.width() < jpopup.outerWidth(true))) ? 'absolute' : 'fixed',
			'top': pos.top,
			'left': pos.left,
			'zIndex': opt.zIndex + 1
		});

		overlay.fadeTo(opt.masktime, opt.opacity);

		if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) {
			setTimeout(function () {
				jpopup.css('visibility', 'visible');
			}, opt.layertime / 3);
		} else {
			jpopup.css('opacity', 0).fadeTo(opt.layertime, 1);
		}
	}
};
(function($){
	
	var currentBanner = 0;
	var _time = 0;
	var _index = 0;
	var _timeDuration = 1000;
	var _timeInterval = 6000;
	var arrItems = jQuery('#slider').find('a');
	var arrImgs = jQuery('#lstKeyVisual').find('li');
	function initBanner(){
		arrItems.each(function(index, item ){
			jQuery(item).unbind('click').bind('click', function(e){
				if(currentBanner != index) {
					jQuery(arrImgs[currentBanner]).stop(true, true).animate({'opacity': 0},(_timeDuration/2), function(){						
						jQuery(arrItems).removeClass('active');
					});
					jQuery(arrImgs[index]).stop(true, true).animate({'opacity': 1},_timeDuration, function(){
						jQuery(arrItems[index]).addClass('active');				
						
						jQuery(arrImgs[currentBanner]).css('z-index', 1);
						jQuery(arrImgs[index]).css('z-index', 2)
						
						autoBanner();
						currentBanner = index;
						_index = index;
					});
					clearTimeout(_time);									
					
				}
				return false;
			});
		});
	}
	function bannerFade(i){
		//console.log(i);
		if(i == "undefined") i == 0;
		if(arrItems.length <=1) return;
		
		if(arrImgs.length <=1) return;	
		if(currentBanner == i) return;
		if(currentBanner != i) {			
			jQuery(arrImgs[currentBanner]).animate({'opacity': 0},(_timeDuration/2), function(){								
				jQuery(arrItems).removeClass('active');
			});
			jQuery(arrImgs[i]).animate({'opacity': 1},_timeDuration, function(){
				jQuery(arrItems[i]).addClass('active');				
				jQuery(arrImgs[currentBanner]).css('z-index', 1);
				jQuery(arrImgs[i]).css('z-index', 2);
				currentBanner = i;				
			});
			
		} else if(currentBanner == 0) {
			jQuery(arrItems).removeClass('active');
			jQuery(arrImgs[currentBanner]).animate({'opacity': 1},_timeDuration, function(){
				jQuery(arrItems[currentBanner]).addClass('active');				
				jQuery(arrItems[currentBanner]).addClass('active');
				jQuery(arrImgs).css('opacity', 0);
				jQuery(arrImgs).css('z-index', 1);
				jQuery(arrImgs[currentBanner]).css('opacity', 1);
				jQuery(arrImgs[currentBanner]).css('z-index', 2);				
			});
		}
		_index = i;
		autoBanner();
	}
	
	function autoBanner(){
		if(jQuery('.homeSlider').length != 1) {
			_timeInterval = 4000;
		}
		_time = setTimeout(function(){
			if(_index > (jQuery('#lstKeyVisual').find('li').length - 2)) {
				_index = -1;
			}			
			_index++;
			bannerFade(_index);		
		}, _timeInterval );
	}
	var interval_id;
	jQuery(window).focus(function() {
		//console.log("focus"+ _time);
		if(_time == -1) autoBanner();
	});

	jQuery(window).blur(function() {
		//console.log("blur");
		clearTimeout(_time);
		_time = -1;
	});
	
	function brandSlider(){
		var container = jQuery('#sliderBrand');
		if(container.length == 0) return;
		var prev = container.find('.btnSliderPrev');
		var next = container.find('.btnSliderNext');
		var slideObj = jQuery('#sliderBrand > div > ul');
		
		var arrImgs = jQuery('#sliderBrand > div > ul > li');
		
		
		var _WIDTH = 135;
		var _LENGTH = 7;
		var isClick = true;
		var currentIndex = 0;
		var currentText = 0;
		var _opacity = 0.2;
		prev.bind("click", function(evt) {
			showImage(currentImage - 1);
			return false;
		});
		
		next.bind("click", function(evt) {		
			showImage(currentImage + 1);
			return false;
		});
		
		var isMoving = false;
		var currentImage = 0;
		
		function imgFade(img, li){
			jQuery(img).animate({
				'opacity': 0
				},500, function(){
					isClick = true;
					jQuery(img).attr('src', jQuery(li).find('a').attr('href'));
					jQuery(img).load(function(){
						jQuery(img).animate({
							'opacity': 1
						}, 500);
					});
					
			});
			arrImgs.each(function(index, item ){
				jQuery(item).find('a').removeClass('active');
				jQuery(li).find('a').addClass('active');
			});
		}
		
		function showImage(index) {
			if (index < 0 || index >= arrImgs.length - (_LENGTH - 1)) {
				return;
			}
			
			if (isMoving) {
				return;
			}
			isMoving = true;
			
			currentImage = index;
			if (currentImage > 0) {
				prev.css({
					opacity: 1,
					cursor: 'pointer'
				});
			} else {
				prev.css({
					opacity: _opacity,
					cursor: 'default'
				});
			}
			
			if (currentImage <= arrImgs.length - (_LENGTH + 1)) {				
				next.css({
					opacity: 1,
					cursor: 'pointer'
				});
			} else {
				next.css({
					opacity: _opacity,
					cursor: 'default'
				});
			}
			
			slideObj.animate({
				"margin-left": -currentImage * _WIDTH
			}, 500, function(){
				isMoving = false;
			});
		}
		showImage(0);
		
	}
	/*
	*
	* load video list upload latest
	* limited : 5
	*
	*/
	function loadSlideshareList(){
		jQuery.ajax({
			url: "http://www.red.tm/blog/wp-content/plugins/slideshares-kit/slideshare.php",
			contentType: "html",
			cache: false,
			type:"GET",
			error: function(json){
				jQuery('#slideshareBox').css('display', 'none');		
			},
			success: function(html){
				jQuery('#slideshareList').html(html);
					jQuery('#slideshareBox').css('display', 'block');
			}
		});
	}
	/*
	*
	* load video list upload latest
	* limited : 5
	*
	*/
	function loadYoutubeList(){
		var _limited = 3;
		var _str = "";
		jQuery.ajax({
			url: "http://gdata.youtube.com/feeds/api/users/redbrandbuilders/uploads?v=2&alt=jsonc",			
			dataType: "jsonp",
			contentType: "application/json",
			cache: false,
			error: function(){
				jQuery('#youtubeInBox').html("");
			},
			success: function(json){
				if(jQuery(json.data).length > 0) {
					jQuery(json.data.items).each(function(index, video){
						if(index < _limited) {
							var _viewCount = ' views';
							if (video.viewCount < 1 ) {
								_viewCount = ' view';
							}
							_viewCount = video.viewCount + _viewCount;
							var _href = "http://www.youtube.com/watch?v=" + video.id;									
							_str += '<li><a target="_blank" class="videoThumb" href="'+ _href +'" title="'+ video.title +'">\n<img src="'+ video.thumbnail.sqDefault +'" alt="'+ video.title +'" title="'+ video.title +'" />\n</a>\n<a target="_blank" class="videoTitle" href="'+ _href +'" title="'+ video.title +'">'+ video.title +'</a>\n<span class="videoUploadBy">by '+ video.uploader +'</span>\n<span class="videoViews">'+ _viewCount + '</span>\n</li>\n';
						}
					});
					jQuery('#youtubeVideoList').html(_str);
					jQuery('#youtubeInBox').css('display', 'block');
				}
			}
		});
	}
	function shareToolPos(){
		var _height = 200;
		var el = $('.shareBlog');
		var _timer = 100;
		function setPos(){
			_top = jQuery(window).scrollTop();
			el.css('top', _height);
			if(_top > _height) {
				el.css({
					'position': 'fixed',
					'top': 10
				});
			} else {
				el.css({
					'position': 'absolute',
					'top': _height
				});
			}
		}
		jQuery(window).scroll(function () { 			
			setPos();
		});
	}
	/*
	 function start here
	*/
	jQuery(document).ready(function() {
		if(jQuery('#popupContactSuccess').length > 0) {
			zshowpopup(jQuery('#popupContact'), {closes: ['btnClose']});
			function closePopup(){
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) {
					setTimeout(function () {
						jQuery('#popupContact').css({ 'visibility': 'hidden', 'top': -5000 });
					}, 100);

				} else {
					jQuery('#popupContact').fadeTo(700, 0, function(){
						jQuery('#popupContact').css('top', -5000);
					});
				}

				if (jQuery('#zmask').length > 0) {
					jQuery('#zmask').fadeTo(700, 0, function(){
						jQuery('#zmask').remove();
					});
				}			
			}
			jQuery('#zmask').bind('click', function(e){
				closePopup();
			});
			jQuery('#popupContact').bind('click', function(e){
				closePopup();
			});
			setTimeout(function(){
				closePopup();
			}, 8000);
		}
		shareToolPos();
		function randomNumber(){
			var randomNum = Math.floor(Math.random()*4);			
			if(randomNum > 3 || randomNum < 0) {
				randomNumber();
			} else {
				return randomNum;
			}
		}
		initBanner();
		if(jQuery('.homeSlider').length == 1) {
			bannerFade(randomNumber());
		} else {
			bannerFade(0);
		}
		if(jQuery('#slider').find('li').length > 1){
			jQuery(jQuery('#lstKeyVisual').find('li')).css('opacity', 0);
			jQuery(jQuery('#lstKeyVisual').find('li')[0]).css('opacity', 1);
		}
		//autoBanner();
		
		/*auto clear input*/
		$.fn.cleardefault = function() {
			return this.focus(function() {
				if( this.value == this.defaultValue ) {
					this.value = "";
				}
			}).blur(function() {
				if( !this.value.length ) {
					this.value = this.defaultValue;
				}
			});
		};
		$("input[type=text], textarea").cleardefault();
		
		
		if(jQuery('#youtubeVideoList').length == 1) {
			loadYoutubeList();
			loadSlideshareList();
		}
	});
})(jQuery);
