function clear_login_inputs()
 {
    $('.input_username_wrapper input').val('');
    $('.input_password_wrapper input').val('');
}

function set_login_inputs()
 {
    $('.input_username_wrapper input').val('username');
    $('.input_password_wrapper input').val('password');
}

function handle_inputs()
 {
    $('.input_username_wrapper input')
    .click(function() {
        if ($(this).val() == 'username')
        clear_login_inputs();
    })
    .blur(function() {
        if ($(this).val() == '')
        set_login_inputs();
    });
    $('.input_password_wrapper input')
    .click(function() {
        if ($(this).val() == 'password')
        clear_login_inputs();
    })
    .blur(function() {
        if ($(this).val() == '')
        set_login_inputs();
    });

    $('#id_search_brand')
    .click(function() {
        if ($(this).val() == 'Manufacturer or Serial Number')
        $(this).val('');
    })
    .blur(function() {
        if ($(this).val() == '')
        $(this).val('Manufacturer or Serial Number');
    });
	
    $('#id_search_name')
    .click(function() {
        if ($(this).val() == 'Model')
        $(this).val('');
    })
    .blur(function() {
        if ($(this).val() == '')
        $(this).val('Model');
    });
}

function handle_tabs()
 {
    $('.search_by_tabs li').click(function() {
        $('.search_by_tabs li').removeClass('active');
        $(this).addClass('active');
        $('#id_search_by_model').val($(this).find('a').blur().attr('rel').replace(/tab_search_/, ''));
        return false;
    });
}

function handle_autocomplete()
{
	$('.search_by_fields #id_search_brand').autocomplete('/auto_complete/manufacturer/', { minChars: 3, autoFill: true });
}

function handle_review_stars()
{
	on_image = '/site_media/images/rank_star.png';
	off_image = '/site_media/images/rank_star_off.png';
	
	$('.product_user_can_review a')
		.mouseover(function() {
			orginal_rating = $(this).parent().html();
			user_pointed = $(this).attr('rel');
			alredy_marked = false
			$.each($(this).parent().find('a'), function(index) {
				$img = $(this).find('img');
				if($img.attr('src') == on_image)
					$img.addClass('_star_was_on');
				else
					$img.addClass('_star_was_off');
				
				if(!alredy_marked)
				{
					if(user_pointed == index + 1)
						alredy_marked = true;
					$img.attr('src', on_image);
				}
				else
					$img.attr('src', off_image);
			})
		});
		
	$('.product_user_can_review').mouseout(function() {
		$('img._star_was_on').attr('src', on_image);
		$('img._star_was_off').attr('src', off_image);
	});
}

$(document).ready(function() {
    handle_inputs();
    handle_tabs();
	handle_autocomplete();
	$('.menu ul li a').click(function() { $(this).blur() });
	handle_review_stars();
});