var current_path = '/'+window.location.pathname.substr(1);

$(document).ready(function(){
   
    $('#logo').fadeIn("slow");
    if(current_path !== ''){
        $('#banner').cycle();
    }
    $('#slides').cycle();
    
    var search = 'Search Stock';
	$("#search").val(search);
	$("#search").focus(function(){
		var value = $("#search").val();
		if(value == search){
			$("#search").val('');
		}	
	});
	$("#search").blur(function(){
		var value = $("#search").val();
		if(value == ''){
			$("#search").val(search);
		}
	});
    
    active_nav();
    
    $('#nav').superfish();
    $('#search').autocomplete('/search_ajax.php', {onItemSelect:selectItem});
    
});

function selectItem(value){
    window.location.href = '/product.php?view=detail&item='+value;
}

function active_nav(){
    $('#nav li a').each(function(i){ 
        nav_path = $(this).attr('href');
        if(nav_path == '//') nav_path = '';
        if(current_path == '/index.php') current_path = '';
        if(current_path == '/product.php') current_path = '/new_morgans.html';
        if(current_path == '/hire_a_morgan.php') current_path = '/new_morgans.html';
        if(nav_path == current_path){
            $(this).addClass('active'); 
            return false;
        }
    });
}

function product_image(image_id){
    $('#product_thumbs li').each(function(i){ $(this).removeClass('active'); });
    var li_id = '#thumb_'+image_id;
    var li_id = li_id.replace(".","_");
    $(li_id).addClass('active');
    $('#product_image img').attr('src','/images/products/medium/'+image_id);
    $('#product_image_link').attr('href','/images/products/large/'+image_id);
}

function send_message(){
    var fields = new Array('contact_name','contact_email','contact_message');
    var error = "Please complete this field.";
    var error_count = 0;
    jQuery.each(fields, function() {
        if($('#'+this).val() == '' || $('#'+this).val() == error){
            $('#'+this).addClass('error');
            $('#'+this).val(error);
            $('#'+this).focus(function(){
                if($(this).val() == error){
                    $(this).val('');
                }
            });
            error_count++;
        }else{
            $('#'+this).removeClass('error');
        }
    });

    if(error_count == 0){
        document.contact.submit();
        return true;
    }
    return false;
}