
var slideTimeout = 0,
	switching = false,
	transition = false;
	
$(document).ready(
	function ()
	{
		var slides = $('#slideshow a.slide');
		
		if(slides.length)
		{
		
			for(i=0;i<slides.length;i++)
			{
				var html = $('#pages').html();
				$('#pages').html(html + '<a href="#slide' + (i + 1) + '" id="page' + (i + 1) + '">' + (i + 1) + '</a>');
				/*$(slides[i]).bind('click',
					function(){
						return false;
					}
				);*/
			} // END for
			
			//$('#pages a:first').addClass('active');
			$('#pages a').filter(function (index) { return 0 == index; }).addClass('active');
			
			$('#pages a').bind('click',
				function()
				{
					if(transition)
					{
						return;
					}
					transition = true;
					
					var activeSlide = $('#slideshow #slides a.active');
						newSlide = $('#slide' + $(this).attr('id').replace('page',''));
					
					//alert(activeSlide.attr('id') + ',' + newSlide.attr('id'));
					
					if(activeSlide.attr('id') != newSlide.attr('id'))
					{
						window.clearTimeout(slideTimeout);
						
						/*$('#viewmore').fadeOut(250);*/
						activeSlide.fadeOut(2000,
							function()
							{
								$('#viewmore a').attr('href',$(newSlide).attr('href'));
								$('#viewmore a').html($(newSlide).attr('title') ? '+ ' + $(newSlide).attr('title') : '');
								/*$('#viewmore').fadeIn(500);*/
								activeSlide.removeClass('active');
								newSlide.addClass('active');
							}
						);
						newSlide.fadeIn(2000,
							function ()
							{
								transition = false;
							}
						);
						
						$('#pages a').removeClass('active');
						$(this).addClass('active');
						
						slideTimeout = window.setTimeout(
							function()
							{
								next_slide();
							},
							8000
						);
					
					}
					
					return false;
				}
			); // END bind('click')
			
			$('#tabbar a').bind('click',
				function()
				{
					if(!$(this).hasClass('active'))
					{
						$('#tabbar a.active').removeClass('active');
						$(this).addClass('active');
						
						$('#tabs div.tab').hide();
						$('#tabs img').hide();
						var newid = $(this).attr('id').replace('button','');
						$('#' + newid).show().children('img:first').show();
						replaceH3();
					}
					
					return false;
				}
			); // END bind('click')
			
			
			$("#slideshow").animate(
				{ 
					height: "422px"
				},
				800,
				function()
				{
					$('#slides').fadeIn(1500);
					$('#controls').fadeIn(1500);
					slideTimeout = window.setTimeout(
					function()
					{
						next_slide();
					},
					5000);
				}
			); // END animate
		} // END if(slides.length)
		
		$('.dropdown-arrow', '#sub-left').bind('click',
			function (e)
			{
				var $this = $(this),
					$parent = $this.parent('li');
				
				// Get parent li's class
				var c = '.tertiary';
				var l = 'third';
				if($parent.next('li').hasClass('quaternary'))
				{
					c = '.quaternary';
					l = 'fourth';
				}
				else
				{
					$parent = $this.parent('span').parent('li');
				}
				
				$(c, '#sub-left').filter(
					function (index)
					{
						return 'block' == $(this).css('display');
					}
				).animate(
					{
						height:'hide'
					},
					{
						complete:
							function (e)
							{
								if('third' == l)
								{
									$(c, '#sub-left').prev('li').each(
										function (index, item)
										{
											var $item = $(item),
												hover = '';
											if($item.hasClass('hover'))
											{
												hover = '-hover';
											}
											$item.children('span').children('.dropdown-arrow').css(
												{
													backgroundImage: 'url(/style/images/third-level-hide.gif)'
												}
											);
										}
									);
								}
								else
								{
									$(c, '#sub-left').prev('li').each(
										function (index, item)
										{
											var $item = $(item),
												hover = '';
											if($item.hasClass('hover'))
											{
												hover = '-hover';
											}
											$item.children('.dropdown-arrow').css(
												{
													backgroundImage: 'url(/style/images/fourth-level-hide' + hover + '.gif)'
												}
											);
										}
									);
								}
							},
						duration:400
					}
				);
				$parent.next(c).filter(
					function (index)
					{
						return 'block' != $(this).css('display');
					}
				).animate(
					{
						height:'show'
					},
					{
						complete:
							function (e)
							{
								var hover = '';
								if($parent.hasClass('hover'))
								{
									hover = '-hover';
								}
								
								if('third' == l)
								{
									$this.css(
										{
											backgroundImage: 'url(/style/images/third-level-show.gif)'
										}
									);
								}
								else
								{
									$this.css(
										{
											backgroundImage: 'url(/style/images/fourth-level-show' + hover + '.gif)'
										}
									);
								}
							},
						duration:400
					}
				);
			}
		); // END bind('click')
		$('.subnav').find('li').filter(
			function (index)
			{
				return !($(this).hasClass('quaternary') || $(this).hasClass('tertiary'));
			}
		).hover(
			function (e)
			{
				var $this = $(this),
					border = { borderBottom: '1px solid #1c5ba0' };
				$this.addClass('hover');
				if($this.hasClass('first'))
				{
					border.borderTop = '1px solid #1c5ba0';
				}
				if($this.parent().hasClass('subnav'))
				{
					$this.css(
						{
							backgroundColor: '#1c5ba0'
						}
					).children('span').not('.dropdown-arrow').css(border)
					 .children('a').css(
						{
							color: '#ffffff'
						}
					);
				}
				else
				{
					var $arrow = $this.children('.dropdown-arrow'),
						arrImg = $arrow.css('background-image');
					if(undefined == arrImg)
					{
						arrImg = '';
					}
					arrImg = arrImg.substring(arrImg.lastIndexOf('-') + 1, arrImg.lastIndexOf('.'));
					$this.css(
						{
							backgroundColor: '#1c5ba0'
						}
					).children('a').css(
						{
							color: '#ffffff'
						}
					).siblings('.dropdown-arrow').css(
						{
							background: 'transparent url(/style/images/fourth-level-' + arrImg + '-hover.gif) no-repeat scroll 0 10px'
						}
					);
				}
			},
			function (e)
			{
				var $this = $(this),
					bgcolor = '#ffffff',
					color = '#545454',
					border = { borderBottom: '1px solid #eeeeee' };
				
				$this.removeClass('hover');
				
				if($this.parent().parent().hasClass('quaternary'))
				{
					bgcolor = '#ebebeb';
				}
				else if($this.parent().parent().hasClass('tertiary'))
				{
					bgcolor = '#f6f6f6';
				}
				else if($this.hasClass('first'))
				{
					border.borderTop = '1px solid #eeeeee';
				}
				
				if($this.parent().hasClass('subnav'))
				{
					if($this.hasClass('selected'))
					{
						color = '#1c5ba0';
					}
					$this.css(
						{
							backgroundColor: bgcolor
						}
					).children('span').not('.dropdown-arrow').css(border)
					.children('a').css(
						{
							color: color
						}
					);
				}
				else
				{
					var $arrow = $this.children('.dropdown-arrow'),
						arrImg = $arrow.css('background-image');
					if(undefined == arrImg)
					{
						arrImg = '';
					}
					arrImg = arrImg.substring(arrImg.lastIndexOf('-', arrImg.indexOf('-', arrImg.indexOf('-') + 1)) + 1, arrImg.lastIndexOf('-'));
					$this.css(
						{
							backgroundColor: bgcolor
						}
					).children('a').css(
						{
							color: color
						}
					).siblings('.dropdown-arrow').css(
						{
							background: 'transparent url(/style/images/fourth-level-' + arrImg + '.gif) no-repeat scroll 0 10px'
						}
					);
				}
			}
		);
	} // END function
); // END ready


function next_slide()
{
	var numslides = $('#slides a').length;
	if(1 >= numslides)
	{
		return;
	}
 	var curslide  = 1 + parseInt($('#slides a.active').attr('id').replace('slide',''));
 	if(curslide > numslides)
 	{
 		curslide = 1;
 	}
 	$('#page' + curslide).click();
 	
	slideTimeout = window.setTimeout(
		function()
		{
			next_slide();
		},
		10000
	);
}
