var kia = {
	URL: '',
	curPage: '',
	msie: { 'is_browser' : false, 'version' : '0' },
	
	ym_statusDelay: false,
	collectionDelay: false,
	toolsDelay: false,
	homeDelay: false,
	
	initialize: function(object){
		this.URL = '';
		
		for(var key in object){
			this[key] = object[key];
		}
		
		if($.browser.msie){
			this.msie.is_browser = true;
			this.msie.version = $.browser.version;
		}
				
		//Register Ajax Error Event
		$.ajaxSetup({
			error:function(x,e){
				Sexy.error('<h1 style="background:none;height:auto;font-weight:bold;font-size:14px;text-indent:0;">Hmmm....we got trouble here!</h1><p>Something wrong while requesting.</p><p>XHR Status : ' + x.status + '</p>');
			}
		});
		
		this.prepActions();	
	},
	
	prepActions: function(){
		/*----------------------------*/
		this.generalActions();
		
		/*----------------------------*/
		this.yahooActions();
		
		/*----------------------------*/
		this.homeActions();
		
		/*----------------------------*/
		this.collectionActions();
		
		/*----------------------------*/
		this.profileActions();
		
		/*----------------------------*/
		this.faqActions();
		
		/*----------------------------*/
		this.careerActions();
		
		/*----------------------------*/
		this.showroomActions();
		
		/*----------------------------*/
		this.newsActions();
		
		/*----------------------------*/
		this.projectActions();
		
		/*----------------------------*/
		this.virtualroomActions();
		
		/*----------------------------*/
		this.toolsActions();
		
		/*----------------------------*/
		this.contactActions();
	},
	DocSideStyling: function(){
		var sidebar = { 'self': $('#sidebar-middle'), 'height': $('#sidebar-middle').outerHeight() };
		var doc = { 'self': $('#doc'), 'height': $('#doc').outerHeight() };
		
		if( sidebar.height >= doc.height ){
			doc.self.height( ( sidebar.height + 35 ) );
			if( doc.self.find('table.tbl-single').length == 1 )
				doc.self.find('table.tbl-single td').height( doc.self.height() );
		}
		
	},
	footerStyling: function(){
		if( $(document).height() <= $(window).height() )
			$('#footer').addClass('footer-fixed');
		else
			$('#footer').removeClass('footer-fixed');
	},
	generalActions: function(){
		//Cufon
		//Cufon.replace('.cf-helveticaneue', { fontFamily: 'HelveticaNeue' });
		
		//Login Background
		if( $('#header .background').length <= 0 ){
			$('#header').prepend( $('<div />',{ 'class': 'background' }).css({ 'top': '0px', 'left': '0px', 'height': $('#header').height() + 'px', 'opacity': 0.25 }) );
		}
		
		//For doc and sidebar height
		kia.DocSideStyling();
		
		//For bottom container
		kia.footerStyling();
		$(window).resize(function(){
			kia.footerStyling();
		});
		
		//Colorbox
		$('a.colorbox').colorbox(); 
		
		//Virtual Room access denied
		if( $('a.virtual-room-denied').length > 0 ){
			$('#menu').delegate('a.virtual-room-denied', 'click', function(e){
				e.preventDefault();
				
				Sexy.alert('<h1 style="margin-bottom:10px;">Virtual Room Access</h1><p>Sorry, please login or sign up to access this page.</p>');
			});
            
            $('.sitemap').delegate('a.virtual-room-denied', 'click', function(e){
				e.preventDefault();
				
				Sexy.alert('<h1 style="margin-bottom:10px;">Virtual Room Access</h1><p>Sorry, please login or sign up to access this page.</p>');
			});
		}
			
	},
	homeActions: function(){
		if( $('.home').length <= 0 )
			return;
		
		if( $('.image-wrapper').length > 1 ){
			
			$('.headlines').data('cur-image', '1');
			clearTimeout( kia.homeDelay );
			
			//Slide show
			kia.homeDelay = setTimeout("kia.homeSlideshow()", 5000);
		
			//Pagination
			$('.headline-pagination').delegate('a', 'click', function(e){
				e.preventDefault();
				var self = this;
				
				if( $(self).hasClass('active') )
					return;
				if( $('.headline-pagination').hasClass('on-animate') )
					return;
					
				clearTimeout( kia.homeDelay );
				
				$('.headline-pagination a.active').removeClass('active');
				$(self).addClass('active');
				
				$('.headline-pagination').addClass('on-animate');
				var clone = $('.headlines .image-wrapper[id="i-' + self.id.replace(/[^0-9]+/g, '') +'"]').clone().css({ 'opacity': 0 });
					$('.headlines').find('.image-wrapper[id="i-' + self.id.replace(/[^0-9]+/g, '') +'"]').remove();
				
				$('.headlines').
					append( clone ).
					find('.image-wrapper[id="i-' + self.id.replace(/[^0-9]+/g, '') +'"]').
					stop().
					animate({ 'opacity': 1 }, function(){
						$('.headline-pagination').removeClass('on-animate');
						
						$('.headlines').data('cur-image', self.id.replace(/[^0-9]+/g, ''));
						kia.homeDelay = setTimeout("kia.homeSlideshow()", 5000);
					});
			});
		}
	},
	homeSlideshow: function(){
		clearTimeout( kia.homeDelay );
		
		var index = $('.headline-pagination a').index( $('.headline-pagination a.active') );
			index += 1;
			index = index >= ( $('.headline-pagination a').length )? 0 : index;
		
		$('.headline-pagination a:eq(' + index +')').trigger('click');
	},
	collectionActions: function(){
		if( kia.curPage !== 'collections' )
			return;
		
		if( $('.level_0').length > 0 )
			kia.collectionLvL0();
		else if( $('.level_2').length > 0 )
			kia.collectionLvL2();
		else if( $('#download').length > 0 )
			kia.collectionDownload();
	},
	collectionLvL0: function(){
		if( $('.collection-lists a').length > 0 ){
			//Create tooltip DOM
			if( $('.collection-lists-tooltip').length <= 0 ){
				var tooltip = $('<div />', { 'class': 'collection-lists-tooltip' }).append( $('<div />', { 'class': 'vt-top' }) ).append( $('<div />', { 'class': 'vt-right' }).append( $('<div />', { 'class': 'vt-left' }).append( $('<div />', { 'class': 'vt-middle' }).append( $('<span />', { 'id': 'vt-title' }) ) ) ) ).hide();
			}
			//else
			//	var tooltip = $('#variant-tooltip');
			$('body').append( tooltip );
			
			//Remove title attribute
			$('.collection-lists a.with-tooltip').each(function(){
				$(this).data('title', this.title).data('image', this.rel);
				$(this).removeAttr('title').removeAttr('rel');
			});
			
			//Hover events
			$('.collection-lists a.with-tooltip').hover(
				function(){
					var self = this;
					tooltip.find('span#vt-title').html( $(this).data('title') ).closest('.collection-lists-tooltip').css({ 'top': ( $(self).offset().top + $(self).outerHeight() + ( kia.msie.is_browser && kia.msie.version === '7.0'? 0 : 5) ) + 'px', 'left': ( ( $(self).offset().left - ( ( tooltip.outerWidth() / 2 ) ) ) + ( $(self).outerWidth() / 2 ) ) + 'px' }).show();
					//$(self).closest('li').append( tooltip ).find('#variant-tooltip').find('span#vt-title').html( $(this).data('title') ).closest('#variant-tooltip').css({ 'top': parseInt( $(self).css('top').replace('px','') ) + 'px', 'left': parseInt( $(self).css('left').replace('px','') ) + 'px' }).css({ 'display': 'inline-block' });
				}, 
				function(){
					tooltip.hide();
				}
			);

		}
	},
	collectionLvL2: function(){		
		$('.level_2').delegate('#detail a.ipagi', 'click', function(e){
			e.preventDefault();
			var self = this;
			var container = $(self).closest('.container');
			
			if( $(self).hasClass('active') )
				return;
			if( container.find('#image-pagination').hasClass('on-animate') )
				return;
				
			container.find('#image-pagination').find('li a').removeClass('active');
			$(self).addClass('active');
			
			container.find('#image-pagination').addClass('on-animate');
			var clone = container.find( '#images li#il_' + parseInt( self.id.replace('io_', '') ) ).clone().css({ 'opacity': 0 });
			container.find( '#images li#il_' + parseInt( self.id.replace('io_', '') ) ).remove();
			

			container.find('#images ul').
				append( clone ).
				find( 'li#il_' + parseInt( self.id.replace('io_', '') ) ).
				stop().
				animate({ 'opacity': 1 }, function(){
					container.find('#image-pagination').removeClass('on-animate');
				});
		});
		
		$('.level_2').delegate('#detail #var-lists a', 'click', function(e){
			e.preventDefault();
			var self = this;
			var container = $(self).closest('.container');
			
			if( $(self).hasClass('active') )
				return;
			if( container.hasClass('on-animate') )
				return;
			
			$(self).closest('ul').find('a.active').removeClass('active');
			$(self).addClass('active');
			
			container.addClass('on-animate');
			var clone = container.find('#images li#' + $(self).data('image') ).clone().css({ 'opacity': 0 });
			container.find('#images li#' + $(self).data('image') ).remove();
			
			container.find('#description h2').html( $(self).data('title') );
			container.find('#images ul').
				append( clone ).
				find('li#' + $(self).data('image') ).
				stop().
				animate({ 'opacity': 1}, function(){
					container.removeClass('on-animate');
				});
			
		});
		
		if( $('#over-vari-galle').length > 0 ){
			$('#over-vari-galle').delegate('a', 'click', function(e){
				e.preventDefault();
				var tab = this.rel;
				
				if( $(this).hasClass(tab + '-active' ) )
					return;
					
				$('#over-vari-galle a[class*=active]').each(function(){ $(this).removeClass( this.rel + '-active'); });
				$(this).addClass(tab + '-active');
				
				$('#detail div.container:visible').removeClass('cur-container').hide();
				$('#detail div#' + tab).addClass('cur-container').show();
			});
		}
		
		$('#sort').delegate('a.first', 'click', function(e){
			e.preventDefault();
			var self = this;
			
			clearTimeout( kia.collectionDelay );
			if( $(self).siblings('ul.ssort:visible').length > 0 ){
				$(self).siblings('ul.ssort').slideUp();
				return;
			}
			
			if( $(self).closest('ul').find('.ssort:visible').length > 0 ){
				$(self).closest('ul').find('.ssort:visible').slideUp(function(){
					$(self).siblings('ul.ssort').slideDown();
				});
			}else{
				$(self).siblings('ul.ssort').slideDown();
			}
		});
		
		$('#sort').delegate('ul.ssort a, ul.ssort span', 'click', function(e){
			e.preventDefault();
			var self = this;
			
			clearTimeout( kia.collectionDelay );
			$('#sort').find('ul.ssort:visible').slideUp();
			
			if( typeof( $(self).attr('rel') ) === 'undefined' )
				return;
				
			var rel = self.rel.split('|');
			
			if( rel[1] === 'select' ){
				$('#sort').find('form').find('input[name='+ rel[0] +']').remove();
				$(self).closest('ul.ssort').siblings('a.first').html('All ' + ( rel[0] === 'dimension'? 'Size':'Motif' ));
				return;
			}
			
			if( $('#sort').find('form').find('input[name='+ rel[0] +']').length <= 0 ){
				$('#sort').find('form').prepend( $('<input />', { 'type': 'hidden', 'name': rel[0] }).val( rel[1] ) );
			}else
				$('#sort').find('form').find('input[name='+ rel[0] +']').val( rel[1] );
				
			$(self).closest('ul.ssort').siblings('a.first').html( $(self).html() );
		});
		
		$('#sort li.s-parent').hover(
			function(){
				if( $(this).find('ul.ssort:visible').length > 0 )
					clearTimeout( kia.collectionDelay );
			},
			function(){
				kia.collectionDelay = setTimeout("kia.collectionSortTimeout()", 600);
			}
		);
		
		kia.collectionToolTip();

	},
	collectionSortTimeout: function(){
		$('#sort').find('ul.ssort:visible').slideUp();
		clearTimeout( kia.collectionDelay );
	},
	collectionToolTip: function(){
		if( $('#var-lists a').length > 0 ){
			//Create tooltip DOM
			if( $('#variant-tooltip').length <= 0 ){
				var tooltip = $('<div />', { 'id': 'variant-tooltip' }).append( $('<div />', { 'class': 'vt-top' }) ).append( $('<div />', { 'class': 'vt-right' }).append( $('<div />', { 'class': 'vt-left' }).append( $('<div />', { 'class': 'vt-middle' }).append( $('<span />', { 'id': 'vt-title' }) ) ) ) ).hide();
			}
			//else
			//	var tooltip = $('#variant-tooltip');
			$('body').append( tooltip );
			
			//Remove title attribute
			$('#var-lists a').each(function(){
				$(this).data('title', this.title).data('image', this.rel);
				$(this).removeAttr('title').removeAttr('rel');
			});
			
			//Hover events
			$('#var-lists a').hover(
				function(){
					var self = this;
					tooltip.find('span#vt-title').html( $(this).data('title') ).closest('#variant-tooltip').css({ 'top': ( $(self).offset().top + $(self).outerHeight() ) + 'px', 'left': ( ( $(self).offset().left - ( ( tooltip.outerWidth() / 2 ) ) ) + ( $(self).outerHeight() / 2 ) ) + 'px' }).show();
					//$(self).closest('li').append( tooltip ).find('#variant-tooltip').find('span#vt-title').html( $(this).data('title') ).closest('#variant-tooltip').css({ 'top': parseInt( $(self).css('top').replace('px','') ) + 'px', 'left': parseInt( $(self).css('left').replace('px','') ) + 'px' }).css({ 'display': 'inline-block' });
				}, 
				function(){
					tooltip.hide();
				}
			);

		}
	},
	collectionDownload: function(){
		$('#download-lists').find('a').hover(
			function(){
				$(this).find('span.dtitle span:first').hide();
				$(this).find('span.dtitle span:last').show();
			},
			function(){
				$(this).find('span.dtitle span:last').hide();
				$(this).find('span.dtitle span:first').show();	
			}
		);
	},
	profileActions: function(){
		if( $('.profile').length <= 0 )
			return;
		
		$('.doc-imgs li[class!=active]').css({ 'opacity': 0 });
		$('.profile').delegate('.doc-img-pagination a', 'click', function(e){
			e.preventDefault();
			
			if( $('.doc-imgs').hasClass('on-animate') )
				return false;
			
			if( $(this).hasClass('active') )
				return false;
			
			var self = $(this);
			var rel = parseInt( this.id.replace('io_', '') );
			
			$('.doc-imgs').addClass('on-animate');
			
			$('.doc-imgs li.active').animate({ 'opacity': 0 }, function(){
				$(this).removeClass('active');
				$('.doc-imgs li:eq(' + rel +')').animate({ 'opacity': 1 }).addClass('active').closest('.doc-imgs').removeClass('on-animate');
				
				$('.doc-img-pagination a.active').removeClass('active');
				self.addClass('active');
			});
		});
	},
	faqActions: function(){
		if( $('.faq').length <= 0 )
			return;
		
		$('#faq-lists').delegate('a','click', function(e){
			e.preventDefault();
			
			if( $(this).closest('div.flink').find('div.fdetail:visible').length <= 0 ){
				$(this).closest('div.flink').find('div.fdetail').slideDown(function(){ kia.footerStyling(); });
			}else{
				$(this).closest('div.flink').find('div.fdetail').slideUp(function(){ kia.footerStyling(); });
			}
			//$(this).closest('div.flink').find('div.fdetail').animate({ 'height': '550px' }, 1000, '', function(){ kia.footerStyling(); });
		});
	},
	careerActions: function(){
		if( $('.career').length <= 0 )
			return;
		
		/*$('.tbody').find('tr.blist').hover(
			function(){
				$(this).find('.btitle span:first').hide();
				$(this).find('.btitle span:last').show();
			},
			function(){
				$(this).find('.btitle span:first').show();
				$(this).find('.btitle span:last').hide();
			}
		);*/
		$('.tbody').delegate('tr.blist', 'click', function(e){
			window.location = $(this).find('.bseparator a').attr('href');
		});
	},
	showroomActions: function(){
		if( $('div.showroom').length <= 0 )
			return;
			
		$('.showroom').delegate('#detail a.ipagi', 'click', function(e){
			e.preventDefault();
			var self = this;
			var container = $(self).closest('.f-left');
			
			if( $(self).hasClass('active') )
				return;
			if( container.find('#image-pagination').hasClass('on-animate') )
				return;
				
			container.find('#image-pagination').find('li a').removeClass('active');
			$(self).addClass('active');
			
			container.find('#image-pagination').addClass('on-animate');
			var clone = container.find( '#images li#il_' + parseInt( self.id.replace('io_', '') ) ).clone().css({ 'opacity': 0 });
			container.find( '#images li#il_' + parseInt( self.id.replace('io_', '') ) ).remove();
			

			container.find('#images ul').
				append( clone ).
				find( 'li#il_' + parseInt( self.id.replace('io_', '') ) ).
				stop().
				animate({ 'opacity': 1 }, function(){
					container.find('#image-pagination').removeClass('on-animate');
				});
		});
	},
	newsActions: function(){
		if( $('div.news').length <= 0 )
			return;
			
		/*$('#lists').find('li').hover(
			function(){ 
				$(this).addClass('hover'); 
			},
			function(){ 
				$(this).removeClass('hover'); 
			}
		);*/
		
		/*$('#lists').delegate('.ulist a', 'click', function(e){
			e.preventDefault();
			
			$('#lists').find('li.active').removeClass('active').find('span.title').find('span:last').hide().closest('span.title').find('span:first').show();
			$(this).closest('li').addClass('active');
			
			$(this).closest('div.excerpt').find('span.title').find('span:first').hide().closest('span.title').find('span:last').show();
		});*/
		
	},
	projectActions: function(){
		if( $('div.project').length <= 0 )
			return;
			
		$('#images li[class!=active]').css({ 'opacity': 0 });
		$('.project').delegate('#image-pagination a', 'click', function(e){
			e.preventDefault();
			
			if( $('#images').hasClass('on-animate') )
				return false;
			
			if( $(this).hasClass('active') )
				return false;
			
			var self = $(this);
			var rel = parseInt( this.id.replace('io_', '') );
			
			$('#images').addClass('on-animate');
			
			$('#images li.active').animate({ 'opacity': 0 }, function(){
				$(this).removeClass('active');
				$('#images li:eq(' + rel +')').animate({ 'opacity': 1 }).addClass('active').closest('#images').removeClass('on-animate');
				
				$('#image-pagination a.active').removeClass('active');
				self.addClass('active');
			});
		});
	},
	virtualroomActions: function(){
		if( $('div.virtual-room').length <= 0 )
			return;
			
		$('div.virtual-room').delegate('a.arrow-buttons', 'click', function(e){
			e.preventDefault();
			var self = this;
			var container = $(self).closest('.li-parent').siblings('.li-middle').find('ul.ul-child');
			var offset = parseInt( container.attr('id').replace('os_', '') );
				offset = isNaN( offset )? 0:offset;
			var marginLeft = parseInt( container.css('margin-left').replace('px', '') );
				marginLeft = isNaN( marginLeft )? 0:marginLeft;
			
			switch( self.rel ){
				case 'prev' : 
					if( offset <= 0 )
						return;
					
					offset -= 1;
					marginLeft = ( 458 * offset );
					break;
				case 'next' : 
					if( Math.floor( container.find('li').length / 3 ) == offset )
						return;
					
					offset += 1;
					marginLeft = ( 458 * offset ) * -1;
					break;
				default		: return; break;
			}
					
			container.attr('id', 'os_' + offset );
			container.stop().animate({ 'margin-left': marginLeft + 'px' }, 1000);
		});
		
		$('div.virtual-room').find('a.room-lists span.thumb span').css({ 'opacity': 0 }).show();
		$('div.virtual-room').find('a.room-lists').hover(
			function(){
				$(this).find('span.thumb span').stop().animate({ 'opacity': 1 }, 600);
			},
			function(){
				$(this).find('span.thumb span').stop().animate({ 'opacity': 0 }, 600);
			}
		);
		
		/*$('div.virtual-room').delegate('a.img-lists', 'click', function(e){
			e.preventDefault();
			
			var container = $('#detail .images');
			
			if( container.hasClass('loading') )
				return;
			
			var ulClone = $('#detail').find('ul').clone();
			container.addClass('loading').find('ul').remove();
		});*/
	},
	toolsActions: function(){
		if( $('.tools').length <= 0 )
			return;
		
		$('.tools').delegate('span#tile-select', 'click', function(e){
			e.preventDefault();
			var self = this;
			
			clearTimeout( kia.toolsDelay );
			if( $(self).siblings('ul:visible').length > 0 ){
				$(self).siblings('ul').slideUp();
				return;
			}
			
			$(self).siblings('ul').slideDown();
		});
			
		$('.tools').delegate('#selection ul a, selection ul span', 'click', function(e){
			e.preventDefault();
			var self = this;
			
			clearTimeout( kia.toolsDelay );
			$('.tools').find('#selection ul:visible').slideUp();
			
			if( typeof( $(self).attr('rel') ) === 'undefined' )
				return;
				
			$('.tools').find('input#tile-size').val( self.rel );
			$(self).closest('ul').siblings('span').html( $(self).html() );
			
			$('.tools').find('#total-area').val('');
			$('.tools').find('#total-tile').val('');
			$('.tools').find('#total-box').val('');
			
			$('.illustration-wrapper').hide();
			kia.footerStyling();
		});
		
		$('.tools #selection').hover(
			function(){
				if( $(this).find('ul:visible').length > 0 )
					clearTimeout( kia.toolsDelay );
			},
			function(){
				kia.toolsDelay = setTimeout("kia.toolsSelectionTimeout()", 600);
			}
		);
		
		$('.tools').delegate('input[type=reset]', 'click', function(e){
			if( typeof( $('.tools').find('#selection ul a:first') ) == 'undefined' )
				return;
				
			$('.tools').find('input#tile-size').val( $('.tools').find('#selection ul a:first').attr('rel') );
			$('.tools').find('#selection span:first').html( $('.tools').find('#selection ul a:first').html() );
			
			$('.illustration-wrapper').hide().find('#tile-draw').html('');
			kia.footerStyling();
			
		});
		
		//restore all
		$('.tools').find('input[type=reset]').trigger('click');
		
		$('.tools').find('form').submit(function(e){
			e.preventDefault();
			
			var roomWidth = parseFloat( $(this).find('#room-width').val() );
			var roomHeight = parseFloat( $(this).find('#room-height').val() );
			var tile = $(this).find('input#tile-size').val().split('|');
			
			if( isNaN( roomWidth ) || isNaN( roomHeight ) )
				return false;
			
			if( tile.length < 3 )
				return false;
				
			for( obj in tile )
				tile[obj] = parseFloat( tile[obj] );
				
			if( isNaN( tile[0] ) || isNaN( tile[1] ) || isNaN( tile[2] ) )
				return false;
			
			//Begin the calculation
			var tileSize = tile[0] * tile[1];
			var totalArea = ( roomWidth * roomHeight ) * Math.pow(100, 2);
			var totalUnit = Math.round( totalArea / tileSize ); //Math.ceil( totalArea / tileSize );
				totalUnit = Math.round( totalUnit + ( totalUnit * 0.1 ) );
			var totalBox = Math.round( totalUnit / tile[2] );
			
			$(this).find('#total-area').val( Math.round( totalArea / Math.pow(100, 2) ) );
			$(this).find('#total-tile').val( totalUnit );
			$(this).find('#total-box').val( totalBox );
			
			//Begin Drawing
			$('.illustration-wrapper').show();
			kia.footerStyling();
			
			var container = $('#tile-draw').html('');
			var maxLength = roomWidth > roomHeight? roomWidth:roomHeight;
				maxLength = Math.round( 300 / maxLength );
			container.css({ 'width': ( roomWidth > roomHeight? 300:( roomWidth * maxLength ) ) + 'px', 'height': ( roomHeight > roomWidth? 300:( roomHeight * maxLength ) )+ 'px' });
			
			var tileWidth = Math.round( ( $('#tile-draw').width() / ( ( roomWidth * 100 ) / tile[0] ) ) );
			var tileHeight = Math.round( (  $('#tile-draw').height() / ( ( roomHeight * 100 ) / tile[1] ) ) );
			var horzNum = Math.ceil(  $('#tile-draw').width() / tileWidth );
			var vertNum = Math.ceil(  $('#tile-draw').height() / tileHeight );
			
			
			var ulParent = $('<ul />');
			for( var i = 1; i <= vertNum; i++ ){
				var liParent = $('<li />', { 'class': 'parent' }).append( $('<ul />') );
				for( var j = 1; j <= horzNum ; j++ ){
					liParent.find('ul').append( $('<li />').css({ 'height': ( tileHeight - 1 ) + 'px', 'width': ( tileWidth - 1 ) + 'px' }) );
				}
				liParent.append( $('<br />',{ 'class': 'clear' }) );
				ulParent.append( liParent );
			}
			
			container.append( ulParent );
		});
	},
	toolsSelectionTimeout: function(){
		$('.tools').find('#selection ul:visible').slideUp();
		clearTimeout( kia.toolsDelay );
	},
	yahooActions: function(){
		if( $('#yahoo-status').length <= 0 )
			return;
			
		var ym_id = $('#yahoo-status').attr('href').replace('ymsgr:sendIM?', '');
		var uri = 'use "http://kid666.com/yql/ymsg_opi.xml" as ymsg.status; select * from ymsg.status where u = "' + ym_id + '"';
			uri = 'http://query.yahooapis.com/v1/public/yql?q=' + escape( uri ) + '&format=json&callback=?';
			
		if(kia.ym_statusDelay)
			clearTimeout(kia.ym_statusDelay);
			
		$.getJSON(
			uri,
			function(data){
				$('#yahoo-status').find('span').removeClass('ym-offline').removeClass('ym-online').addClass( 'ym-' + data.query.results.messengerstatus.status );
				kia.ym_statusDelay = setTimeout('kia.yahooActions()',5000);
			}
		);
	},
	check_email: function(element){
		var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
		if(!emailfilter.test($(element).val())) return false;
		
		return true;
	},
	contactActions: function(){
		if( $('.contact-us').length <= 0 )
			return;
		
		$('.contact-us').find('input[type=reset]').click(function(){
			$(this).closest('form').find('.error').removeClass('error');
			$(this).closest('div').find('.error-message').remove();
		});
		
		$('.contact-us').find('form').submit(function(){
			var error = new Array();
			var noEmpty = $(this).find('.no-empty');
			
			//reset
			$(this).find('.error').removeClass('error');
			$(this).closest('div').find('.error-message').remove();
			
			noEmpty.each(function(){
				if( $(this).val() === '' ){
					error.push($(this).closest('tr').find('td.ttitle').html() + ' is required.');
					$(this).addClass('error');
				}else{
					if( $(this).hasClass('email') ){
						if( ! kia.check_email($(this)) ){
							error.push('Invalid email address.');
							$(this).addClass('error');
						}
					}
				}
			});
			
			if( error.length > 0 ){
				var error_message = $('<div />', { 'class': 'error-message' });
				
				for(key in error){
					error_message.append($('<p />').html(error[key]));
				}
				
				$(this).closest('div').prepend(error_message);
			}
			
			return error.length <= 0 ? true : false;
		});
	}
}
