/* jQuery add-ons & plugins */
	$.fn.toggleFade = function(settings){
		if(settings==undefined){
			settings={ speedIn:'slow'};
		}
		settings = jQuery.extend(
			{
				speedIn: "normal",
				speedOut: settings.speedIn
			}, settings
		);
		return this.each(function(){
			var isHidden = jQuery(this).is(":hidden");
			jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
		});
	};

	$.fn.delay = function(time,func){
		this.each(function(){ 
			setTimeout(func,time); 
		});
		return this;
	};

	$.fn.clearForm = function(){
		return this.each(function(){
			var type = this.type, tag = this.tagName.toLowerCase();
			if (tag == 'form')
				return $(':input',this).clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	};

/* functions */
	var duration = 3000;
	var transition = 600;
	var setCarousel = '';
	function startSlideshow(settings,el){
		if(settings == 'init'){
			if($('.carousel').length > 0 && $('.carousel .item').size() > 1){
				$('.carousel').append('<div class="nav"><ul style="width:'+ ($('.carousel .item').size()*19) +'px;"></ul></div>');
				$('.carousel .item').each(function(){
					$('.carousel .nav ul').append('<li><a class="'+$(this).attr('class')+'" href=""></a></li>');
				});
				$('.carousel .item').first().addClass('active').fadeIn(transition);
				$('.carousel .nav ul li').first().find('a').addClass('active');
				$('.carousel .nav ul li a').click(function(){
					return false;
				});
				$('.carousel .nav ul li a').hover(function(){
					startSlideshow('stop',$(this));
				},function(){
					startSlideshow('start');
				});
				startSlideshow('start');
			}else{
				$('.carousel .item').fadeIn(transition);
			}
		}else if(settings == 'anim'){
			if($('.carousel div.item.active').next('.item').length > 0){
				$('.carousel div.item.active').fadeOut(transition).removeClass('active').next('.item').addClass('active').delay(transition,function(){
					$('.carousel .nav ul li a.active').removeClass('active').parent().next('li').find('a').addClass('active');
					$('.carousel div.item.active').fadeIn(transition);
				});
			}else{
				$('.carousel div.item.active').fadeOut(transition).removeClass('active');
				$('.carousel div.item').first().addClass('active').delay(transition,function(){
					$('.carousel .nav ul li a.active').removeClass('active');
					$('.carousel .nav ul li').first().find('a').addClass('active');
					$('.carousel div.item.active').fadeIn(transition);
				});
			}
		}else if(settings == 'start'){
			if(setCarousel == ''){
				setCarousel = window.setInterval("startSlideshow('anim');",duration);
			}else{
				startSlideshow('stop');
			}
		}else if(settings == 'stop'){
			if(setCarousel != ''){
				window.clearInterval(setCarousel);
				setCarousel = '';
			}
			if(!$(el).hasClass('active')){
				$('.carousel .active').each(function(){
					$(this).removeClass('active');	
				});
				$(el).addClass('active');
				$('.carousel div.item').removeClass('active').fadeOut(200).delay(300,function(){
					$('.'+$(el).attr('class').replace(' active','').replace('item ','')).addClass('active').fadeIn(200);
				});
			}
		}
	}

	function setPageWidth(){
		var w = 10;
		$('.pages .wrapContent .item').each(function(){
			w = w + $(this).width();
		});
		$('.pages .wrapContent .subWrap').css({'width':w});
	}

/* on DOM ready events */
	$(function(){
		/* basic on load actions */
			$('body').removeClass('noJs');
			$('html').addClass($('html').attr('lang'));
			$('.ext').each(function(){
				$(this).attr('target','_blank');
			});
			setTimeout("startSlideshow('init');",1000);

		/* set active state on nav */
			$('nav ul li a').each(function(){
				if($(this).attr('class') == $('body').attr('class')){
					$(this).addClass('bold');
				}
			});

		/* wrap width set in pages */
			if($('.pages .wrapContent .item').length > 0){
				setPageWidth();
			}
	});
