// JavaScript Document
var banner
var imgBanner = $('<img />').attr('src', 'img/banner.jpg').css('padding-left','10px');
var imgFileTrack = $('<img />').attr('src', 'img/banner-filetrack.jpg').css('padding-left','10px');
var imgMailTrack = $('<img />').attr('src', 'img/banner-mailtrack.jpg').css('padding-left','10px');
var imgMediaTrack = $('<img />').attr('src', 'img/banner-mediatrack.jpg').css('padding-left','10px');
var imgJobTrack = $('<img />').attr('src', 'img/banner-jobtrack.jpg').css('padding-left','10px');
var imgVisitorTrack = $('<img />').attr('src', 'img/banner-visitortrack.jpg').css('padding-left','10px');

/*
$.fn.extend({  

    myMethod2: function()
	{	
	 	 return this.each(function() { alert(this) });
	}  

});  
*/
var time;
$('document').ready(function(){
							 	/*
							 	$('li.menu').mouseover(function(){
															   	$(this).find('ul').show();
															   })
							 	$('li.menu').mouseout(function(){
															   	$(this).find('ul').hide();
															   })
								*/
							 	banner = new bannerScroller();
								var mi = new menuIndicator();
								
								
								
								$('li.menu').mouseenter(function(){
																 	if($(this).find('div').length == 1)
																	{
																		mi.callback = function(){
																				$(t).get(0).slider.start();
																				
																								}
																	}
																	clearTimeout(time);
																	var t = this;
																	mi.startMove($(this))
																	
																	
																	}) 
								$('li.menu').mouseleave(function(){
																 	time = setTimeout(function(){mi.sleep()}, 200)
																	
																	mi.callback = null;
																 	if($(this).find('div').length == 1)
																	{
																		$(this).get(0).slider.hide()
																	}
																	}) 
								
								var LIS = $('li.menu');
								for (var i = 0; i < LIS.length; i++)
								{
									LIS.get(i).slider = new slider({element:$(LIS.get(i)).find('div.submenu')});
								}
								
								
							 })







var slider = function(attributes)
{
	var This = this;
	var defaults = {
			offsetUnit : 10,
			onStatusChanged : new Function
		};
	
	this.count = 0;
    if (attributes == null) attributes = {};
    for (var i in defaults) (attributes[i] == null) ? this[i] = defaults[i] : this[i] = attributes[i];
    for (var i in attributes) (this[i] == null) ? this[i] = attributes[i] : null;

	this.init = function()
	{
		this.height = this.element.outerHeight();
		this.element.find('ul.submenu').css('margin-top', -this.height );
	}
	this.hide = function()
	{
		clearInterval(this.time);
		this.currentOffsetUnit = -this.offsetUnit;
		this.offset = parseInt(this.element.find('ul.submenu').css('margin-top'));
		this.time = setInterval(function(){This.animate()},10)
	}
	this.start = function()
	{
		clearInterval(this.time);
		this.currentOffsetUnit = this.offsetUnit;
		this.offset = parseInt(this.element.find('ul.submenu').css('margin-top'));
		this.element.show();
		this.shadowPos = this.element.parent().position().left-40;
		//$('.menu-shadow').css('background-position',this.shadowPos+'px -330px' )
		this.time = setInterval(function(){This.animate()},10)
	}
	this.animate = function()
	{
		this.count++;
		this.offset += this.currentOffsetUnit;
		this.shadowPercent = Math.round(this.offset/this.height*330)
		if (this.offset>=0 & this.currentOffsetUnit >0)
		{
			clearInterval(this.time);
			//$('.menu-shadow').css('background-position',this.shadowPos+'px 0px' )
			this.element.find('ul.submenu').css('margin-top', 0 );
		}
		else if (this.offset<= -this.height & this.currentOffsetUnit <0)
		{
			clearInterval(this.time);
			this.element.find('ul.submenu').css('margin-top', -this.height );
			//$('.menu-shadow').css('background-position',this.shadowPos+'px -330px' )
			this.element.hide();
		}
		else
		{
			this.element.find('ul.submenu').css('margin-top', this.offset );
			//$('.menu-shadow').css('background-position',this.shadowPos+'px '+this.shadowPercent+'px' )
		}
		
	}
	this.init();
}

var menuIndicator= function(attributes)
{
	var This = this;

	var defaults = {
		 callback:new Function(),
		 target:$('<div />'),
		 shadowDirection:'down',
		 shadowOffset:0
		};
	
    if (attributes == null) attributes = {};
    for (var i in defaults) (attributes[i] == null) ? this[i] = defaults[i] : this[i] = attributes[i];
    for (var i in attributes) (this[i] == null) ? this[i] = attributes[i] : null;

	this.init = function()
	{
		this.indicator = $('.indicator');
	}

	this.sleep = function()
	{
		clearTimeout(this.time);
		this.shadowDirection = 'up';
		this.target=$('<div />')
		this.endPos = 50;
		this.endWidth = 520;
		
		this.positionOffset = this.indicator.position().left;
		this.positionUnit = (this.endPos - this.positionOffset) /22;
		
		this.widthOffset = this.indicator.outerWidth();
		this.widthUnit = (this.endWidth - this.widthOffset) /22;
		this.time = setInterval(function(){This.move()}, 10);
		this.count = 0;
		
	}
	this.startMove = function(target)
	{
		this.shadowDirection = 'down';
		if (this.target.get(0) != target.get(0))
		{
			this.target = target;
			clearTimeout(this.time)
			this.endPos = this.target.position().left+50;
			this.endWidth = this.target.outerWidth();
			
			this.positionOffset = this.indicator.position().left;
			this.positionUnit = (this.endPos - this.positionOffset) /22;
			
			this.widthOffset = this.indicator.outerWidth();
			this.widthUnit = (this.endWidth - this.widthOffset) /22;
			this.time = setInterval(function(){This.move()}, 10);
			this.count = 0;
		}
		else
		{
			if (this.callback!= null) this.callback();
		}
		
	}
	this.moveShadow = function()
	{
		if (this.shadowDirection == 'down')
		{
			if (this.shadowOffset < 22)
			{
				this.shadowOffset++ ;
				$('.menu-shadow').css('background-position','0px '+(-(22-this.shadowOffset)*15)+'px' )
			}
		}
		if (this.shadowDirection == 'up')
		{
			this.shadowOffset-- ;
			$('.menu-shadow').css('background-position','0px '+(-(22-this.shadowOffset)*15)+'px' )
		}

				$('.menu-shadow').css('background-position', this.positionOffset-140+'px '+(-(22-this.shadowOffset)*15)+'px' )

		if (this.shadowOffset == 22)
		{
			this.shadowDirection = 'none';
		}
		if (this.shadowOffset == 0)
		{
			this.shadowDirection = 'none';
		}
		
	}
	
	this.move = function()
	{
		this.positionOffset += this.positionUnit;
		this.widthOffset += this.widthUnit;
		this.count++;
		if (this.count<=22)
		{
			this.moveShadow();
			this.indicator.css('left', this.positionOffset);
			this.indicator.css('width', this.widthOffset );

		}
		else
		{
			clearInterval(this.time)
			if (this.callback!= null) this.callback();
			//$('#debug2').append(document.createTextNode(this.count)).append($('<br />'))
		}
		
	}
	this.init();
}

function loadPage(url)
{
	$.ajax({
	  url: url,
	  cache: false,
	  success: function(data) {
		$('#content').html(data);
	  }
	});
}

var bannerScroller = function()
{
	var This = this;
	this.scrollControl = $('.banner-container');
	this.itemWidth = 881+13;
	this.time;
	this.init = function()
	{
		this.startPos = 0;
		this.endPos = -this.itemWidth;
		this.count = 0;
		this.tempContainer = $('<div />').appendTo('body').css('display','none');
		/*
		this.itemCount = $('.testimonial-moving').find('.testimonial-item').length;
		$('.testimonial-moving').find('.testimonial-item:first').clone().appendTo($('.testimonial-moving'));
		
		
		this.scrollControl.find('.banner-container').mouseover(function(){
			clearTimeout(This.time);
			clearInterval(This.time);
		})
		this.scrollControl.find('.testimonial-item').mouseout(function(){
			This.time = setTimeout(function(){This.startMove()}, 3000)
		})
		this.scrollControl.css({cursor:'pointer'})
		*/
	
	}
	this.init();
	
	this.show = function(img)
	{
		if (img != this.currentImage)
		{
			this.scrollControl.append(img);
			this.currentImage = img;
			this.startMove();
		}
	}
	

	this.startMove = function()
	{
		clearTimeout(this.time);
		this.startPos = this.scrollControl.position().left;
		this.time = setInterval(function(){This.move()},10);
		if (this.scrollControl.css('left') == 'auto') this.scrollControl.css('left', '0px');
	}
	this.move = function()
	{
		var currentPos = parseInt( this.scrollControl.css('left'));
		//var newPos = Math.floor(currentPos + ((this.endPos - currentPos)/3))
		var newPos = Math.floor(currentPos + ((-this.itemWidth-1-(this.endPos - currentPos))/1))
		//$('#debug').html(this.scrollControl.css('left') + ' - ' + this.endPos + ' - ' + newPos + ' - ' + this.count);
		if (newPos <= this.endPos) 
		{
			this.tempContainer.append(this.scrollControl.find('img')[0]);
			
			this.scrollControl.css('left', 0+'px');
			this.startPos = 0;
			this.endPos = -this.itemWidth;
			this.count = 0;
			clearInterval(this.time);
			this.count ++;
			
			//this.time = setTimeout(function(){This.startMove()}, 3000)
		}
		else
		{
			this.scrollControl.css('left', newPos+'px');
		}
	}
}
var AS;


function showStudyDetail(sender,name)
{
	$(sender).hide();
	$('#'+name).slideDown('slow');
}


/****************************************************************************************************************/
function in_array(str, arr)
{
	for (i in arr)
	{
		if (arr[i] == str)
		{
			return i;
		}
	}
	return false;
}

function empty(obj)
	{
		if (obj.attr('disabled')) return false; //if the field is disabled by a switch
		if (obj.attr('tagName') == 'INPUT') 
		{
			if (obj.attr('type') == 'text')
			{
				if (obj.attr('value')=='') {
					return true
				} 
				else {
					if (obj.attr('id') == 'tbEmail' || obj.attr('id') == 'tbConfirmEmail' || obj.attr('id') == 'tbDemoEmail' || obj.attr('id') == 'tbDemoEmailConfirm') {
						var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
						
						if (obj.attr('id') == 'tbDemoEmail')
						{
							if (obj.attr('value') != $('#tbDemoEmailConfirm').attr('value')) return true;
						}
						if (obj.attr('id') == 'tbDemoEmailConfirm')
						{
							if (obj.attr('value') != $('#tbDemoEmail').attr('value')) return true;
						}
						if (obj.attr('id') == 'tbEmail')
						{
							if (obj.attr('value') != $('#tbConfirmEmail').attr('value')) return true;
						}
						if (obj.attr('id') == 'tbConfirmEmail')
						{
							if (obj.attr('value') != $('#tbEmail').attr('value')) return true;
						}
						
						if (!(reg.test(obj.attr('value'))))
						{
							return true;
						}
					}
					return false
				}
			}
		}
		if (obj.attr('tagName') == 'TEXTAREA')
		{
			if (obj.attr('value')=='') {return true} else {return false}
		}
	}

function markError(obj)
	{
		if (!in_array(obj.attr('id'), errmsg))
		{
			errmsg.push(obj.attr('id'));
		}
		if (obj.attr('type') == "checkbox")
		{
			obj.removeClass().addClass('input-error');
		}
		else
		{
			obj.removeClass().addClass('input-error');
		}
	}

function clearError(obj)
	{
		//remove from error array
		if (in_array(obj.attr('id'), errmsg))
		{
			errmsg.splice(in_array(obj.attr('id'), errmsg), 1);
		}
		if (obj.attr('type') == "checkbox")
		{
			obj.removeClass().addClass('input');
		}
		else
		{
			obj.removeClass().addClass('input');
		}
	}

var errmsg = [];

function initDemoCheck()
{
	var items = ['tbDemoName', 'tbDemoEmail', 'tbDemoEmailConfirm', 'tbDemoContact'];
	for (var i = 0; i< items.length; i++)
	{
		var obj = $('#'+items[i]);
		obj.focusout(function(){validateObj($(this))})
	}
}



function validateDemo()
{
	var items = ['tbDemoName', 'tbDemoEmail', 'tbDemoEmailConfirm', 'tbDemoContact'];
	for (var i = 0; i< items.length; i++)
	{
		var obj = $('#'+items[i]);
		validateObj(obj)
	}
	if (errmsg.length > 0)
	{
		alert('Please check the form!')
	}
	else
	{
		$('#demo-btn-submit').hide();
		$('#labelDemoWait').show();
		var d = "tbDemoName=" + $("#tbDemoName").attr('value')
		   +"&tbDemoEmail=" + $("#tbDemoEmail").attr('value')
		   +"&tbDemoContact=" + $("#tbDemoContact").attr('value')
		   +"&radio=" + $('input:radio[name=radio]:checked').val()
		   +"&tbDemoTime=" + $("#tbDemoTime").attr('value');
		  
		$.ajax({ 
		   type: "POST", 
		   url: "pages/demo-submit.php", 
		   data: d, 
		   success: function(msg){ 
		   	
		   loadPage("pages/home1.php");
		   banner.show(imgBanner);
		   } 
		 });
	}
}



function validateForm()
{
	var items = ['tbName', 'tbEmail', 'tbQuery', 'tbConfirmEmail'];
	for (var i = 0; i< items.length; i++)
	{
		var obj = $('#'+items[i]);
		validateObj(obj)
	}
	if (errmsg.length > 0)
	{
		alert('Please check the form!')
	}
	else
	{
		$('#contact-btn-submit').hide();
		$('#contact-img-wait').show()
		var img = $('<img src="img/loading.gif" />');
		img.css('padding-top', '14px');
		img.css('padding-bottom', '13px');
		$('.button').parent().append(img);
		$.ajax({ 
		   type: "POST", 
		   url: "pages/contact-submit.php", 
		   data: "tbName=" + $("#tbName").attr('value')
		   +"&tbEmail=" + $("#tbEmail").attr('value')
		   +"&tbQuery=" + $("#tbQuery").attr('value'), 
		   success: function(msg){ 
			$('.popup-contact-container').hide()
			$('#contact-btn-submit').show();
			$('#contact-img-wait').hide()
			
			var items = ['tbName', 'tbEmail', 'tbQuery', 'tbConfirmEmail'];
			for (var i = 0; i< items.length; i++)
			{
				var obj = $('#'+items[i]);
				obj.attr('value','');
			}

		   } 
		 });
	}
}
function validateObj(obj)
{
	if (empty(obj)) 
	{
		markError(obj);
	}
	else
	{
		clearError(obj);
	}
}


