



var KiaUI = function()
{

	function onflashloaded() {
        $('object').each(function(){
            var that = this;
            // Code to fix Firefox Flash focus problem. (Codah 2009)
            // Append sibling flash container element to invalidate the 
            // Flash.
            if(window.navigator.appName == 'Netscape') {
                this.onmouseover = function() {
                    var parent = that.parentNode;
                    var span   = document.createElement('span');
                    parent.appendChild(span);
                    parent.removeChild(span);
                    that.focus();
                };
             }
        });  
    };
	

	// Fixes the News Display to page
	function _ND(selector, maxItems, pagingParentElement)
	{
		this.selector = selector;
		this.maxItems = maxItems;
		this.numPages = Math.ceil(this.selector.length / this.maxItems);
		this.page = 1;
		this.pageParent = pagingParentElement;
		
		this.init();
	}
	
		_ND.prototype.init = function()
		{
			this.drawPaging();
			this.drawItems();
		}
		
		_ND.prototype.drawPaging = function()
		{
			this.pageParent.append('<div class="paging"></div>')
			
			for(var i=1; i<=this.numPages; i++)
			{
				$('.paging').append('<a href="#" title="Click to view page ' + i + ' of Kia News">' + i + '</a>');
				
				$('.paging a:last').bind('click', {newsDisplay: this, page: i}, function(e)
				{
					return e.data.newsDisplay.gotoPage(e.data.page);
				})
			}
		}
		
		_ND.prototype.drawItems = function()
		{
			for(var i = 0, el; i<this.selector.length; i++)
			{
				el = this.selector[i];
				
				if(i < (this.page * this.maxItems) - this.maxItems 	||		i >= (this.page * this.maxItems))
				{
					$(el).css('display', 'none')
				}
				else
				{
					$(el).css('display', 'block');
				}
			}
			
			var pages = $('.paging a').removeClass('active');
			$('.paging a:nth-child(' + this.page + ')').addClass('active');
		}
		
		_ND.prototype.gotoPage = function(pageNumber)
		{
			if(pageNumber < 1 || pageNumber > this.numPages)
			{
				alert('Specified page number outside of range!');
			}
			else
			{
				this.page = pageNumber;
				this.drawItems();
			}
			
			return false;
		}
	
	
	//	Autoclear form field when user clicks in the field
	function _acff(selector)
	{
		this.selector 		= selector;
		this.defaultStr 	= this.selector.val();

		this.init();
	}
	
		_acff.prototype.init = function()
		{
			this.selector.bind('click', {searchUI: this}, this.clearText);
			this.selector.bind('blur', {searchUI: this}, this.restoreText);
		}
		
		_acff.prototype.clearText = function(e)
		{
			if(e.data.searchUI.selector.val() == e.data.searchUI.defaultStr)
			{
				e.data.searchUI.selector.val("");
			}
		}
		
		_acff.prototype.restoreText = function(e)
		{
			if(e.data.searchUI.selector.val() == "")
			{
				e.data.searchUI.selector.val(e.data.searchUI.defaultStr)
			}
		}
	
	function _addSep(selector, seperator)
	{
		this.selector = selector;
		this.seperator = seperator;
		this.selector.append(seperator);
	}
	
	// Create CSS class hooks for the sitemap
	function _smh(selector)
	{
		this.selector = selector;
		this.selector.children().children(':nth-child(1)').addClass('home');
		this.selector.children().children(':nth-child(2)').addClass('vehicle-root');
		// Make every 5th vehicle clear the rest to make a new row
		$('.vehicle-root > ul > li:nth-child(5)').css('clear', 'both');
		$('.vehicle-root > ul > li:nth-child(9)').css('clear', 'both');
		
		$('.vehicle-root li:last').addClass('last');
		
		// Add class names for the vehicles
		$('.vehicle-root > ul > li').each(function()
		{
			$(this).addClass( $(this).children('a').text().toLowerCase().replace(/ /, '-') );
		});
	}
	
	
	// return our namespace
	return {
		NewsDisplay	: _ND,
		AutoClearFormField	: _acff,
		AddSeperators : _addSep,
		OnFlashLoaded : onflashloaded,
		SiteMapHooks  : _smh
	}
}();


$(document).ready(function()
{
	// Upgrade the news UI.
	// !!! First, remove any empty paragraphs &/or links with no text nodes
	$('.news p:empty').remove();
	$('.news a:empty').parent().remove();
	news = new KiaUI.NewsDisplay($('.news p a'), 5, $('.news'));
	
	// Upgrade the search UI.
	search	= new KiaUI.AutoClearFormField($('.txtsearch'));
	email 	= new KiaUI.AutoClearFormField($('.subscribe input'));
	
	// Add the bread crumbs seperators
	breadcrumbs = new KiaUI.AddSeperators($('.breadcrumbs li:not(:last)'), '<span class="seperator">&gt;</span>');
	
	// Add some sitemap hooks for crappy browsers
	sitemapHooks = new KiaUI.SiteMapHooks($('.kiasitemap'));
});
