$(document).ready(function(){

	/* initial functions */
	
	if($("body").attr("id") == "home"){
	
		initBrand();
	
	}

	/* feature box */

	var featLoop = 2;
	
	$("#feature div:not(:first-child)").each(function(){
	
		$("#feature ul").append("<li><a href='' id='" + $(this).attr('class') + "' class='button'>" + featLoop + "</a>");
		featLoop ++;
	
	});
	
	$("#feature ul a").live('click', function(event){
	
		
		var clicked = $(this);
		
		if(event.type == "mouseleave"){ clicked= $("#feature ul a:first-child") };
		
		if(!$(this).hasClass('current')){
		
			$("#feature ul a").removeClass('current');
			clicked.addClass("current");
			$("#feature div").fadeOut("slow");
			
			$("#feature div." + clicked.attr('id')).fadeIn("slow");
			
		
		}
	
		return false;
	
	});
	
	
	/* brand flick */
	
	
	var itemWidth = 114;
	var ulWidth;
	
	$("#brands div a").click(function(){
	
		switchBrand($(this));
		return false;
	
	});
	
	function switchBrand(clicked){
	
		
		var moveDirection; 
		if(clicked == "next"){
			
			moveDirection = "next";
		
		}else{
		
			moveDirection = clicked.attr("id");
		}
		
		var currentLogo = $("#brands div li img.current");
		
		if(moveDirection == "next"){
		
			if(currentLogo.parent("li").is(":last-child")){
			
				changeBrand("next", true);	
			
			}else{
			
				changeBrand("next", false);
			
			}
		
		}else{
		
			
			if(currentLogo.parent("li").is(":first-child")){
			
				changeBrand("prev", true);	
			
			}else{
			
				changeBrand("prev", false);
			
			}
		
		
		}
		
		
	
	}
	
	
	function changeBrand(direction, loop){
	
		var currentPos = parseInt($("#brands ul").css("margin-left"));
		var operator;
		var nextActive;
		
		switch(direction){
		
			case 'next':
			operator = Number(currentPos) - Number(itemWidth);
			if(loop){ operator = "0";nextActive = $("#brands ul li:first-child"); }else{ nextActive = $("#brands ul li img.current").parent("li").next("li"); };
			break;
			case 'prev':
			operator = Number(currentPos) + Number(itemWidth);
			if(loop){ operator = 0 - (ulWidth - 114);nextActive = $("#brands ul li:last-child"); }else{ nextActive = $("#brands ul li img.current").parent("li").prev("li"); };
			break;
			default:
			break;
			
		}
		
		$("#brands ul").animate({ "marginLeft" : operator }, 500, function(){
		
			$("#brands ul li img").removeClass("current");
			nextActive.children("img").addClass("current");
			
			var logoID = $("#brands ul li img.current").attr("id");
			$("#brands > img").fadeOut("slow"); 
			$("#brands > img." + logoID).fadeIn("slow ");

		});
	
	
	}
	
	function initBrand(){
	
		ulWidth = $("#brands ul li").length * 114;
		$("#brands ul").css("width", ulWidth);
		$("#brands > img:first").fadeIn("fast");
		var runBrands = setInterval(function() { switchBrand('next'); }, 5000);

		
	}
	
	
	
	/* accordion */
	
	if($('#slides')){
	
		var accord = $('#slides');
	
		accord.children('li').not('.title').hide('fast', function(){ });
		accord.children('li.active').next('li').show('fast');
		
		accord.children('li.title').click(function(){
		
			$(this).next('li').slideToggle();
			$(this).toggleClass('active');

		
		});
	
	
	}
	/*
	if($('form')){
	
		$('form').submit(function(){
		
			$('form input.required').each(function(){
				
				if($(this).text() == ""){
					alert($(this).text());
					$('.error').show();
					return false;
				
				}
				 
			
			});
			
		return false;
		});
		
	}
	*/

});
